前言

我们来讲一下Bob插件的打包和发布过程大概是:

  • 打包文件

  • 改名字和后缀

  • 获取sha256

  • 上传到github

  • 发布release

  • 修改appcast.json文件

  • 上传到github

  • .....

差不多就是这样,是不是感觉好麻烦的?每次都是一样的步骤,身为IT人!!怎么可以每次都做重复工作呢~

然后就有了这篇文章~利用Github 的 workflow工作流来进行自动发布

过程

获取Github Token

https://github.com/settings/tokens

生成一个新的Token,有效期最好设置成无效,权限必须要有repoworkflow

设置仓库Token

  • Key:PAT_TOKEN

  • Value:刚才前面申请的Token

新建workflow

在你的项目的目录.github/workflows 下新建compress.yml

name: build

on:
  push:
    branches:
      - main
    # Ignore the push event if commit message contains "[skip ci]"
    paths-ignore:
      - 'appcast.json'

jobs:
  zip:
    runs-on: ubuntu-latest

    permissions:
      # Give the default GITHUB_TOKEN write permission to commit and push the
      # added or changed files to the repository.
      contents: write

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Get repository name
      id: build
      run: echo "::set-output name=build::$(basename $(pwd))"
      shell: bash

    - name: Zip src directory
      run: |
        cd src && zip -r src.zip . -x ".*" -x "*/.*" -x "*/*/.*" -x "*/*/*/.*" -x "src.zip"
        mv src.zip ../
      working-directory: ${{ github.workspace }}

    - name: Calculate SHA256
      id: sha256
      run: echo "::set-output name=sha256::$(sha256sum src.zip | awk '{print $1}')"
      working-directory: ${{ github.workspace }}

    - name: Read appcast.json
      id: read_appcast
      run: |
        echo ::set-output name=appcast::$(cat appcast.json)
      working-directory: ${{ github.workspace }}

    - name: Determine next version
      id: next_version
      run: |
        versions=$(jq '.versions[].version' appcast.json)
        if [ -z "$versions" ]; then
            versions="0.0.0"
        fi
        max_version=$(echo "$versions" | sort -rV | head -n1)
        max_version=$(echo "$max_version" | sed 's/"//g')
        # 分割版本号为主、次、修订三个部分
        IFS='.' read -r -a version_parts <<< "$max_version"
        major="${version_parts[0]}"
        minor="${version_parts[1]}"
        patch="${version_parts[2]}"

        # 增加修订版本号
        patch=$((patch + 1))

        # 如果修订版本号超过9,则次版本号加一,并将修订版本号重置为0
        if [ "$patch" -gt 9 ]; then
          minor=$((minor + 1))
          patch=0
        fi

        # 拼接新版本号
        new_version="$major.$minor.$patch"

        echo "::set-output name=next_version::$new_version"
      shell: bash
      working-directory: ${{ github.workspace }}
    
    - name: Get Info.json Version
      id: get_info_version
      run: |
        version=$(jq -r '.version' src/info.json)
        echo "Version: $version"
        echo "::set-output name=version::$version"
      shell: bash
      working-directory: ${{ github.workspace }}

    - name: Get commit message
      id: commit_message
      run: echo "::set-output name=message::$(git log -1 --pretty=%B)"
      shell: bash

    - name: Rename zip file
      run: mv src.zip ${{ steps.build.outputs.build }}.bobplugin
      working-directory: ${{ github.workspace }}

    - name: Get current timestamp
      id: timestamp
      run: echo "::set-output name=timestamp::$(date +%s%3N)"
    
    - name: Modify appcast.json
      run: |
        # Generate the new version object to add
        new_version='{
                  "version": "${{ steps.next_version.outputs.next_version }}",
                  "desc": "${{ steps.commit_message.outputs.message }}",
                  "sha256": "${{ steps.sha256.outputs.sha256 }}",
                  "url": "https://github.com/almightyYantao/bob-plugin-deeplx-translator/releases/download/${{ steps.next_version.outputs.next_version }}/${{ steps.build.outputs.build }}.bobplugin",
                  "minBobVersion": "0.5.0",
                  "timestamp": ${{ steps.timestamp.outputs.timestamp }}
                }'

        # Read the current appcast.json
        current_appcast=$(cat appcast.json)

        # Append the new version object to the versions array
        new_appcast=$(jq --argjson new_version "$new_version" '.versions += [$new_version]' <<< "$current_appcast")

        # Update appcast.json with the new versions array
        echo "$new_appcast" > appcast.json
      working-directory: ${{ github.workspace }}
    
    - name: Modify src/info.json
      run: |
        jq --arg new_version "${{ steps.next_version.outputs.next_version }}" '.version = $new_version' src/info.json > tmp.$$.json && mv tmp.$$.json src/info.json
      working-directory: ${{ github.workspace }}
        
    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
      with:
        tag_name: ${{ steps.next_version.outputs.next_version }}
        release_name: ${{ steps.build.outputs.build }}.bobplugin ${{ steps.next_version.outputs.next_version }}
        body: |
          - Added ${{ steps.build.outputs.build }}.bobplugin
          - SHA256: ${{ steps.sha256.outputs.sha256 }}
          - Version: {
                  "version": "${{ steps.next_version.outputs.next_version }}",
                  "desc": "${{ steps.commit_message.outputs.message }}",
                  "sha256": "${{ steps.sha256.outputs.sha256 }}",
                  "url": "https://github.com/almightyYantao/bob-plugin-deeplx-translator/releases/download/${{ steps.next_version.outputs.next_version }}/${{ steps.build.outputs.build }}.bobplugin",
                  "minBobVersion": "0.5.0",
                  "timestamp": ${{ steps.timestamp.outputs.timestamp }}
                }
        draft: false
        prerelease: false

    - name: Upload Release Asset
      id: upload-release-asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./${{ steps.build.outputs.build }}.bobplugin
        asset_name: ${{ steps.build.outputs.build }}.bobplugin
        asset_content_type: application/zip
      
    # - uses: actions/checkout@v2
    - name: Auto commit
      uses: stefanzweifel/git-auto-commit-action@v5
      env:
        GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
      with:
        commit_message: " Automated commit by GitHub Action"
        branch: ${{ github.ref }}  

workflow详解

  1. Checkout repository

    • 使用 actions/checkout@v2 操作签出仓库代码。

  2. Get repository name

    • 运行命令获取仓库名并设置为 build 输出。

  3. Zip src directory

    • src 目录压缩为 src.zip 文件。

  4. Calculate SHA256

    • 计算 src.zip 文件的 SHA256 哈希值并设置为 sha256 输出。

  5. Read appcast.json

    • 读取 src/appcast.json 文件的内容并设置为 appcast 输出。

  6. Determine next version

    • 通过读取 appcast.json 中的版本号,确定下一个版本号。

    • 如果 appcast.json 中没有版本号,则初始版本号为 0.0.0

    • 使用分割版本号,增加修订版本号,如果修订版本号超过9,则次版本号加1,并将修订版本号重置为0。

    • 设置新的版本号为 next_version 输出。

  7. Get commit message

    • 获取最近一次提交的提交信息并设置为 message 输出。

  8. Rename zip file

    • src.zip 文件重命名为 仓库名.bobplugin

  9. Get current timestamp

    • 获取当前时间戳并设置为 timestamp 输出。

  10. Modify appcast.json

    • 生成一个新的版本对象,包含版本号、描述、SHA256 值、URL、最小 Bob 版本号和时间戳。

    • 将新版本对象追加到 appcast.json 文件中的版本数组中。

  11. Create Release

    • 使用 actions/create-release@v1 操作创建一个新的 GitHub Release。

    • 使用 PAT_TOKEN 进行认证。

    • 设置标签名为 v${{ github.run_number }},发布名称为 仓库名.bobplugin 版本号

    • 设置发布内容,包括新增的 .bobplugin 文件、SHA256 值和版本信息。

    • 发布为正式版本 (draft: false, prerelease: false)。

  12. Upload Release Asset

    • 使用 actions/upload-release-asset@v1 操作将 .bobplugin 文件作为 Release 资源上传。

    • 使用 PAT_TOKEN 进行认证。

    • 指定上传 URL 为创建 Release 时返回的 upload_url,资源路径为重命名后的 .bobplugin 文件。

  13. Auto commit

    • 使用 stefanzweifel/git-auto-commit-action@v5 操作自动提交修改后的 appcast.json 文件。

    • 使用 PAT_TOKEN 进行认证。

    • 设置提交信息为 "Automated commit by GitHub Action",提交到当前分支 (${{ github.ref }})。

收工

这时候你可以去提交到Github中了,这时候Actions里面应该就会有工作流在跑了,过一会你就可以发现,Release里面已经发布了一个新的,并且appcast.json文件也被更新了