原因

无聊写了一个go的小程序,然后想研究一下GitHub actions,当我release时自动构建程序,并发布在对对应的release里面。

历程

我一开始想用SLSA Go releaser这个工作流程,但是参照他那个文档配了半天没配出来,我就换官方的那个了,然后就开始间距的折磨之旅。

YAML
name: Release

on:
  release:
    types: [ created ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      repository-projects: write
    strategy:
      matrix:
        goos: [ linux, darwin, windows ]
        goarch: [ amd64 ]

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

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: '1.22'

      - name: Create build directory
        run: mkdir -p build

      - name: Build
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          echo "Building for GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }}"
          cd im-System
          go mod tidy
          go build -v -ldflags "-s -w" -trimpath -o ../build/im-System-${{ matrix.goos }}_${{ matrix.goarch }} .
          echo "Build completed for GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }}"

      - name: List build directory
        run: ls -alh build

      - name: Create Draft Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ github.ref_name }}
          release_name: Release ${{ github.ref_name }}
          draft: true
          prerelease: false
          body: "Automated draft release from GitHub Actions."
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: $HOME/build/im-System-${{ matrix.goos }}_${{ matrix.goarch }}
          asset_name: im-System-${{ matrix.goos }}-${{ matrix.goarch }}
          asset_content_type: application/octet-stream
      - name: Publish Release
        if: success()
        run: |
          curl -X PATCH \
          -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
          -H "Content-Type: application/json" \
          -d '{"draft":false}' \
          "https://api.github.com/repos/${{ github.repository }}/releases/${{ steps.create_release.outputs.id }}"

我一开始是想在工作目录新建一个build的文件夹生成编译文件的,然后发现找不到目录im-System,然后我就把这个目录路径删掉了,于是构建是完成了,但是新的问题来了,上传的时候,一开始说找不到路径 我寻思,这个路径前面的流程都是能访问的怎么到这就访问不了了,后面我看日志发现,这个$HOME确实不能被识别,于是我直接改成绝对的目录,找不到目录解决了,但是上传又有了新的问题Bad credentials,我搜索出来都是没权限,我看了一眼我开了权限的,然后搜着搜着就出来了这个东西

Go Release GitHub Action

我看了一眼这个文档,好简单,然后我试着配了一下,竟然解决了没有权限的问题?

YAML
name: Release

on:
  release:
    types: [ created ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      repository-projects: write
    strategy:
      matrix:
        goos: [ linux, darwin, windows ]
        goarch: [ amd64, arm64,386 ]
        exclude: # 排除某些平台和架构
          - goarch: arm64
            goos: windows
          - goarch: 386
            goos: darwin

    steps:
      - uses: actions/checkout@v4
      - uses: wangyoucao577/go-release-action@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          goos: ${{ matrix.goos }}
          goarch: ${{ matrix.goarch }}
          goversion: 1.22
          binary_name: "im-System"
          extra_files: README.md

因为我不是很了解这个actions的原理,所以我没有研究这个项目,等我研究了发文章吧