Loading... ## 原因 无聊写了一个go的小程序,然后想研究一下GitHub actions,当我release时自动构建程序,并发布在对对应的release里面。 ## 历程 我一开始想用SLSA Go releaser这个工作流程,但是参照他那个文档配了半天没配出来,我就换官方的那个了,然后就开始间距的折磨之旅。 ```yml 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: $<ruby>matrix.goos }} GOARCH<rp> (</rp><rt>${{ matrix.goarch</rt><rp>) </rp></ruby> run: | echo "Building for GOOS=$<ruby>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<rp> (</rp><rt>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</rt><rp>) </rp></ruby> release_name: Release $<ruby>github.ref_name }} draft<rp> (</rp><rt>true prerelease: false body: "Automated draft release from GitHub Actions." env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN</rt><rp>) </rp></ruby> - name: Upload Release Asset uses: actions/upload-release-asset@v1 with: upload_url: $<ruby>steps.create_release.outputs.upload_url }} asset_path<rp> (</rp><rt>$HOME/build/im-System-${{ matrix.goos</rt><rp>) </rp></ruby>_$<ruby>matrix.goarch }} asset_name<rp> (</rp><rt>im-System-${{ matrix.goos</rt><rp>) </rp></ruby>-$<ruby>matrix.goarch }} asset_content_type<rp> (</rp><rt>application/octet-stream - name: Publish Release if: success() run: | curl -X PATCH \ -H "Authorization: token ${{ secrets.GITHUB_TOKEN</rt><rp>) </rp></ruby>" \ -H "Content-Type: application/json" \ -d '{"draft":false}' \ "https://api.github.com/repos/$<ruby>github.repository }}/releases/${{ steps.create_release.outputs.id }}" ``` 我一开始是想在工作目录新建一个build的文件夹生成编译文件的,然后发现找不到目录im-System,然后我就把这个目录路径删掉了,于是构建是完成了,但是新的问题来了,上传的时候,一开始说找不到路径 我寻思,这个路径前面的流程都是能访问的怎么到这就访问不了了,后面我看日志发现,这个$HOME确实不能被识别,于是我直接改成绝对的目录,找不到目录解决了,但是上传又有了新的问题Bad credentials,我搜索出来都是没权限,我看了一眼我开了权限的,然后搜着搜着就出来了这个东西 > [Go Release GitHub Action][1] 我看了一眼这个文档,好简单,然后我试着配了一下,竟然解决了没有权限的问题? ```yml name<rp> (</rp><rt>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</rt><rp>) </rp></ruby> goos: $<ruby>matrix.goos }} goarch<rp> (</rp><rt>${{ matrix.goarch</rt><rp>) </rp></ruby> goversion: 1.22 binary_name: "im-System" extra_files: README.md ``` 因为我不是很了解这个actions的原理,所以我没有研究这个项目,等我研究了发文章吧 [1]: https://github.com/wangyoucao577/go-release-action 最后修改:2024 年 07 月 07 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏