|
| 1 | +--- |
| 2 | +name: Release to GHCR |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - master |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + commit_sha: |
| 11 | + description: 'Git commit SHA to build and deploy' |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: write # Needed to create new releases |
| 17 | + packages: write # Needed to push to GHCR |
| 18 | + id-token: write # Needed to create an ephemeral cross-repo token |
| 19 | + |
| 20 | +jobs: |
| 21 | + get-context: |
| 22 | + name: Generate release context |
| 23 | + runs-on: ubuntu-latest |
| 24 | + outputs: |
| 25 | + new-version: ${{ steps.compute-context.outputs.new-version }} |
| 26 | + current-version: ${{ steps.compute-context.outputs.current-version }} |
| 27 | + version-changed: ${{ steps.compute-context.outputs.version-changed }} |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.commit_sha || github.sha }} |
| 33 | + fetch-depth: 0 # Fetch all history for git describe to work |
| 34 | + |
| 35 | + - name: Compute the context for this release |
| 36 | + id: compute-context |
| 37 | + run: | |
| 38 | + current_version=$(cat package.json | jq -r .version) |
| 39 | +
|
| 40 | + # Check if the version in package.json is using semantic versioning |
| 41 | + if [[ "$current_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 42 | + echo "Current version is a valid semantic version: $current_version" |
| 43 | + else |
| 44 | + echo "Current version format is not a standard semantic version: $current_version" |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | +
|
| 48 | + echo "current-version=$current_version" >> "$GITHUB_OUTPUT" |
| 49 | +
|
| 50 | + # Check if the version in package.json was changed in the last commit |
| 51 | + previous_commit=$(git rev-parse HEAD~1) |
| 52 | + previous_version=$(git show $previous_commit:package.json 2>/dev/null | jq -r .version || echo "") |
| 53 | +
|
| 54 | + echo "Previous version: $previous_version" |
| 55 | + echo "Current version: $current_version" |
| 56 | +
|
| 57 | + if [ "$current_version" != "$previous_version" ] && [[ "$current_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 58 | + echo "Version changed from $previous_version to $current_version in the last commit" |
| 59 | + echo "version-changed=true" >> $GITHUB_OUTPUT |
| 60 | + echo "new-version=$current_version" >> $GITHUB_OUTPUT |
| 61 | + else |
| 62 | + echo "Version unchanged or not following semantic versioning format" |
| 63 | + echo "version-changed=false" >> $GITHUB_OUTPUT |
| 64 | + echo "new-version=$current_version" >> $GITHUB_OUTPUT |
| 65 | + fi |
| 66 | +
|
| 67 | + create-release: |
| 68 | + name: Create GitHub release |
| 69 | + needs: get-context |
| 70 | + if: ${{ needs.get-context.outputs.version-changed == 'true' }} |
| 71 | + runs-on: ubuntu-latest |
| 72 | + outputs: |
| 73 | + release-id: ${{ steps.create-release.outputs.id }} |
| 74 | + release-url: ${{ steps.create-release.outputs.html_url }} |
| 75 | + steps: |
| 76 | + - name: Create release |
| 77 | + id: create-release |
| 78 | + uses: actions/github-script@v7 |
| 79 | + with: |
| 80 | + script: | |
| 81 | + const release = await github.rest.repos.createRelease({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + tag_name: `v${process.env.VERSION}`, |
| 85 | + name: `v${process.env.VERSION}`, |
| 86 | + body: 'Automated release created by GitHub Actions', |
| 87 | + draft: false, |
| 88 | + prerelease: false, |
| 89 | + generate_release_notes: true |
| 90 | + }); |
| 91 | + return release.data; |
| 92 | + env: |
| 93 | + VERSION: ${{ needs.get-context.outputs.new-version }} |
| 94 | + |
| 95 | + build-and-push: |
| 96 | + name: Build and push image to GHCR |
| 97 | + needs: [get-context, create-release] |
| 98 | + if: ${{ needs.get-context.outputs.version-changed == 'true' }} |
| 99 | + runs-on: ubuntu-latest |
| 100 | + steps: |
| 101 | + - name: Checkout |
| 102 | + uses: actions/checkout@v4 |
| 103 | + with: |
| 104 | + ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.commit_sha || github.sha }} |
| 105 | + |
| 106 | + - name: Set up Docker Buildx |
| 107 | + uses: docker/setup-buildx-action@v3 |
| 108 | + |
| 109 | + - name: Login to GitHub Container Registry |
| 110 | + uses: docker/login-action@v3 |
| 111 | + with: |
| 112 | + registry: ghcr.io |
| 113 | + username: ${{ github.actor }} |
| 114 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 115 | + |
| 116 | + - name: Build and push Docker image |
| 117 | + uses: docker/build-push-action@v6 |
| 118 | + with: |
| 119 | + context: . |
| 120 | + push: true |
| 121 | + tags: "${{ needs.get-context.outputs.new-version }},latest" |
| 122 | + build-args: | |
| 123 | + BUILD_VERSION=${{ needs.get-context.outputs.new-version }} |
| 124 | + BUILD_DATE=${{ github.event.repository.updated_at }} |
| 125 | + VCS_REF=${{ github.sha }} |
| 126 | + cache-from: type=gha |
| 127 | + cache-to: type=gha,mode=max |
0 commit comments