Trước đó mình đã có 1 bài hướng dẫn các bạn tạo cli tool cho riêng mình bằng Golang.
Sau khi code xong chúng ta sẽ public cli.
Nhưng chúng ta không thể build Binary trên từng hệ điều hành rồi mới push chúng lên release được.
chúng ta sẽ dùng 1 lệnh và build chúng trên Multi Architecture hay còn gọi multi-arch
https://github.com/mrnim94/confluence_cli/blob/master/.github/workflows/build-and-release.yml
name: Release on: push: tags: - 'v*' jobs: release: strategy: matrix: go_version: [1.21.x] runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Go 1.21 uses: actions/setup-go@v5 with: go-version: ${{ matrix.go_version }} - name: Build with xgo uses: crazy-max/ghaction-xgo@v3 with: xgo_version: latest go_version: ${{ matrix.go_version }} dest: build prefix: confluence_cli targets: windows/386,windows/amd64,linux/386,linux/amd64,darwin/386,darwin/amd64 v: true x: false ldflags: -s -w - uses: actions/upload-artifact@v4 with: name: confluence_cli path: build/ - name: Create Release uses: ncipollo/release-action@v1 with: token: ${{ secrets.MY_GITHUB_TOKEN }} tag: ${{ github.ref_name }} name: "Release ${{ github.ref_name }}" artifacts: "build/*" draft: false prerelease: false
Here’s an explanation of each section:
- Workflow Name:
name: Release
– The name of the workflow is “Release.”
- Trigger Event:
on: push: tags: - 'v*'
– This workflow is triggered when a new tag is pushed to the repository, specifically tags that start with “v” (commonly used for versioning).
- Jobs:
jobs: release:
– Defines a job named “release.”
- Strategy:
strategy: matrix: go_version: [1.21.x]
– This sets up a strategy to run the job with a matrix of different configurations. Here, it’s used to test against a specific version of Go (1.21.x).
- Run Environment:
runs-on: ubuntu-latest
– The job will run on the latest version of Ubuntu provided by GitHub Actions.
- Steps:
- The job consists of several steps, each performing specific actions:
- Checkout Repository:
- uses: actions/checkout@v4
– Checks out the code in the repository so it can be used in the workflow.
- Set up Go:
- name: Set up Go 1.21
– Sets up a specific Go environment (version 1.21.x as defined in the matrix).
- Build with xgo:
- uses: crazy-max/ghaction-xgo@v3
– Uses the “xgo” action for cross-compiling Go applications. It builds the application for multiple platforms (Windows, Linux, Darwin) and architectures (386, amd64).
- Upload Artifacts:
- uses: actions/upload-artifact@v4
– This step uploads the build artifacts (the compiled binaries) to GitHub, so they can be downloaded after the workflow completes.
- Create Release:
- uses: ncipollo/release-action@v1
– This step creates a release on GitHub. It uses the tag name for the release name and uploads the build artifacts. Thetoken
is a GitHub secret used for authentication.
- Checkout Repository:
- The job consists of several steps, each performing specific actions:
The workflow automates the process of building, packaging, and releasing software for different platforms whenever a new version tag is pushed. This helps in the consistent and efficient management of software release cycles.
Hãy tin mình:
When a Bash Script exceeds 50 lines, I convert it into streamlined Golang code.