|
| 1 | +# Common release pipeline that triggers on package tags: |
| 2 | +# - Prepare a release for a package $pkg/vX.X.X, collect commits info and changelog |
| 3 | +# - Create a GitHub release and combine all release notes |
| 4 | +# - Builds a binary for common platforms if package have "cmd" directory |
| 5 | +name: Release Go module |
| 6 | +on: |
| 7 | + push: |
| 8 | + tags: |
| 9 | + - '*/v*.*.*' # Trigger only on tags with the format $package/vX.X.X |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + packages: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + release_binaries: |
| 17 | + strategy: |
| 18 | + fail-fast: false |
| 19 | + matrix: |
| 20 | + platform: ['linux', 'darwin'] |
| 21 | + goarch: ['amd64', 'arm64'] |
| 22 | + name: Release multi-platform |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v3 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + fetch-tags: true |
| 29 | + - name: Extract Package Name from Tag |
| 30 | + id: extract_package_name |
| 31 | + run: | |
| 32 | + TAG_REF="${GITHUB_REF#refs/tags/}" |
| 33 | + PACKAGE_NAME=$(echo "$TAG_REF" | cut -d'/' -f1) |
| 34 | + VERSION=$(echo "$TAG_REF" | cut -d'/' -f2) |
| 35 | + echo "Tag Reference: $TAG_REF" |
| 36 | + echo "Package Name: $PACKAGE_NAME" |
| 37 | + echo "Version: $VERSION" |
| 38 | +
|
| 39 | + echo "PACKAGE_NAME=$PACKAGE_NAME" >> "$GITHUB_ENV" |
| 40 | + echo "VERSION=$VERSION" >> "$GITHUB_ENV" |
| 41 | + - name: Find Last Tag for Package and Generate Release Notes |
| 42 | + id: generate_release_notes |
| 43 | + run: | |
| 44 | + # Extract the package name and version from the tag |
| 45 | + TAG_REF="${GITHUB_REF#refs/tags/}" |
| 46 | + PACKAGE_NAME=$(echo "$TAG_REF" | cut -d'/' -f1) |
| 47 | + VERSION=$(echo "$TAG_REF" | cut -d'/' -f2) |
| 48 | +
|
| 49 | + # Find the latest tag for the same package that is not the current tag |
| 50 | + LAST_TAG=$(git describe --abbrev=0 --match "$PACKAGE_NAME/v*" --tags $(git rev-list --tags --skip=1 --max-count=1)) |
| 51 | + echo "Last tag: ${LAST_TAG}" |
| 52 | +
|
| 53 | + # If no previous tag is found, use the initial commit as the reference |
| 54 | + if [ -z "$LAST_TAG" ]; then |
| 55 | + LAST_TAG=$(git rev-list --max-parents=0 HEAD) |
| 56 | + fi |
| 57 | +
|
| 58 | + # Extract the version part of the last tag |
| 59 | + LAST_TAG_VERSION=$(echo "$LAST_TAG" | cut -d'/' -f2) |
| 60 | + echo "Last tag version: $LAST_TAG_VERSION" |
| 61 | + echo "LAST_TAG_VERSION=${LAST_TAG_VERSION}" >> "$GITHUB_ENV" |
| 62 | +
|
| 63 | + # Get the commits between the last tag and the current tag that are in the directory scope |
| 64 | + COMMITS=$(git log "$LAST_TAG..$PACKAGE_NAME/$VERSION" --pretty=format:"- %s (%h)" -- "$PACKAGE_NAME") |
| 65 | +
|
| 66 | + # Output the release notes |
| 67 | + echo "Commits:" |
| 68 | + echo "$COMMITS" |
| 69 | +
|
| 70 | + # Safely set the release notes as an environment variable using heredoc and EOF |
| 71 | + echo "COMMITS<<EOF" >> "$GITHUB_ENV" |
| 72 | + echo "$COMMITS" >> "$GITHUB_ENV" |
| 73 | + echo "EOF" >> "$GITHUB_ENV" |
| 74 | + - name: Set up Go |
| 75 | + uses: actions/setup-go@v5 |
| 76 | + with: |
| 77 | + go-version: '1.22.6' |
| 78 | + - name: Install gorelease tool |
| 79 | + run: | |
| 80 | + go install golang.org/x/exp/cmd/gorelease@latest |
| 81 | + - name: Run gorelease to check for breaking changes |
| 82 | + working-directory: ${{ env.PACKAGE_NAME }} |
| 83 | + id: check_breaking_changes |
| 84 | + run: | |
| 85 | + set +e # Disable exit on error to capture output even if the command fails |
| 86 | + echo "Last tag version: ${{ env.LAST_TAG_VERSION }}" |
| 87 | + echo "Current tag version: ${VERSION}" |
| 88 | + BREAKING_CHANGES=$(gorelease -base=${{ env.LAST_TAG_VERSION }} -version=${VERSION} 2>&1) |
| 89 | + echo "Breaking changes: ${BREAKING_CHANGES}" |
| 90 | + set -e # Re-enable exit on error for the subsequent steps |
| 91 | + echo "BREAKING_CHANGES<<EOF" >> "$GITHUB_ENV" |
| 92 | + echo "$BREAKING_CHANGES" >> "$GITHUB_ENV" |
| 93 | + echo "EOF" >> "$GITHUB_ENV" |
| 94 | + - name: Read Additional Release Notes from File |
| 95 | + if: always() |
| 96 | + working-directory: ${{ env.PACKAGE_NAME }} |
| 97 | + id: read_additional_notes |
| 98 | + run: | |
| 99 | + # Check if the .changeset directory exists and the file for the current version is present |
| 100 | + if [ -f ".changeset/${{ env.VERSION }}.md" ]; then |
| 101 | + # Read the content of the file |
| 102 | + RELEASE_NOTES=$(cat ".changeset/${{ env.VERSION }}.md") |
| 103 | +
|
| 104 | + # Format the release notes and breaking changes into FULL_RELEASE_NOTES |
| 105 | + echo "FULL_RELEASE_NOTES<<EOF" >> "$GITHUB_ENV" |
| 106 | + echo "## Release notes:" >> "$GITHUB_ENV" |
| 107 | + echo "$RELEASE_NOTES" >> "$GITHUB_ENV" |
| 108 | + echo "" >> "$GITHUB_ENV" |
| 109 | + echo "## Commits:" >> "$GITHUB_ENV" |
| 110 | + echo "${{ env.COMMITS }}" >> "$GITHUB_ENV" |
| 111 | + echo "" >> $GITHUB_ENV |
| 112 | + echo "## Breaking changes:" >> "$GITHUB_ENV" |
| 113 | + echo "${{ env.BREAKING_CHANGES }}" >> "$GITHUB_ENV" |
| 114 | + echo "EOF" >> "$GITHUB_ENV" |
| 115 | + else |
| 116 | + # Print error message and fail the pipeline if the file is not found |
| 117 | + echo "Error: Release notes file '.changeset/${{ env.VERSION }}.md' not found." |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +
|
| 121 | + - name: Create GitHub Release |
| 122 | + env: |
| 123 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + run: | |
| 125 | + sudo apt-get install -y gh |
| 126 | + gh release create "${{ env.PACKAGE_NAME }}-${{ env.VERSION }}" --title "${{ env.PACKAGE_NAME }} ${{ env.VERSION }}" --notes "${{ env.FULL_RELEASE_NOTES }}" || true |
| 127 | + - name: Check if 'cmd' directory exists and set environment variable |
| 128 | + run: | |
| 129 | + if [ -f "$GITHUB_WORKSPACE/${{ env.PACKAGE_NAME }}/cmd/main.go" ]; then |
| 130 | + echo "CMD_ENTRYPOINT_EXISTS=true" >> "$GITHUB_ENV" |
| 131 | + else |
| 132 | + echo "CMD_ENTRYPOINT_EXISTS=false" >> "$GITHUB_ENV" |
| 133 | + fi |
| 134 | + - name: Build binary release |
| 135 | + uses: wangyoucao577/go-release-action@v1 |
| 136 | + if: env.CMD_ENTRYPOINT_EXISTS == 'true' |
| 137 | + with: |
| 138 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 139 | + goversion: '1.22.6' |
| 140 | + goos: ${{ matrix.platform }} |
| 141 | + goarch: ${{ matrix.goarch }} |
| 142 | + binary_name: ${{ env.PACKAGE_NAME }} |
| 143 | + release_name: ${{ env.PACKAGE_NAME }} |
| 144 | + release_tag: ${{ env.PACKAGE_NAME}}-${{ env.VERSION }} |
| 145 | + project_path: ${{ env.PACKAGE_NAME }}/cmd |
| 146 | + asset_name: ${{ env.PACKAGE_NAME }}-${{ env.VERSION }}-${{ matrix.platform }}-${{ matrix.goarch }} |
0 commit comments