|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + packages: write |
| 13 | + id-token: write |
| 14 | + security-events: write |
| 15 | + attestations: write |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 19 | + cancel-in-progress: ${{ !startsWith(github.ref, 'refs/tags/') }} |
| 20 | + |
| 21 | +env: |
| 22 | + REGISTRY: ghcr.io |
| 23 | + IMAGE_NAME: ${{ github.repository }} |
| 24 | + |
| 25 | +jobs: |
| 26 | + semantic-version: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + if: github.event_name == 'push' |
| 29 | + outputs: |
| 30 | + new_tag: ${{ steps.tag.outputs.new_tag }} |
| 31 | + changelog: ${{ steps.tag.outputs.changelog }} |
| 32 | + steps: |
| 33 | + - name: Checkout code |
| 34 | + uses: actions/checkout@v6 |
| 35 | + with: |
| 36 | + fetch-depth: 0 |
| 37 | + |
| 38 | + - name: Get latest tag |
| 39 | + id: get_tag |
| 40 | + run: | |
| 41 | + # Get the latest tag, or default to v0.0.0 |
| 42 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 43 | + echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 44 | + echo "Latest tag: $LATEST_TAG" |
| 45 | +
|
| 46 | + - name: Determine version bump |
| 47 | + id: bump |
| 48 | + run: | |
| 49 | + # Get the commit message |
| 50 | + COMMIT_MSG=$(git log -1 --pretty=%B) |
| 51 | + echo "Commit message: $COMMIT_MSG" |
| 52 | +
|
| 53 | + # Determine bump type based on conventional commits |
| 54 | + if echo "$COMMIT_MSG" | grep -qiE '^(feat|fix|perf|refactor|style|test|docs|chore)!:' || echo "$COMMIT_MSG" | grep -qi 'BREAKING CHANGE'; then |
| 55 | + BUMP_TYPE="major" |
| 56 | + elif echo "$COMMIT_MSG" | grep -qiE '^feat(\(.+\))?:'; then |
| 57 | + BUMP_TYPE="minor" |
| 58 | + elif echo "$COMMIT_MSG" | grep -qiE '^(fix|perf)(\(.+\))?:'; then |
| 59 | + BUMP_TYPE="patch" |
| 60 | + else |
| 61 | + BUMP_TYPE="none" |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "bump_type=$BUMP_TYPE" >> $GITHUB_OUTPUT |
| 65 | + echo "Bump type: $BUMP_TYPE" |
| 66 | +
|
| 67 | + - name: Calculate new version |
| 68 | + id: tag |
| 69 | + run: | |
| 70 | + # Only version on main branch pushes |
| 71 | + if [ "${GITHUB_REF}" != "refs/heads/main" ]; then |
| 72 | + echo "Not on main; skipping versioning" |
| 73 | + echo "new_tag=" >> $GITHUB_OUTPUT |
| 74 | + echo "changelog=" >> $GITHUB_OUTPUT |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | +
|
| 78 | + LATEST_TAG="${{ steps.get_tag.outputs.latest_tag }}" |
| 79 | + BUMP_TYPE="${{ steps.bump.outputs.bump_type }}" |
| 80 | +
|
| 81 | + if [ "$BUMP_TYPE" = "none" ]; then |
| 82 | + echo "No semantic commit detected, skipping versioning" |
| 83 | + echo "new_tag=" >> $GITHUB_OUTPUT |
| 84 | + echo "changelog=" >> $GITHUB_OUTPUT |
| 85 | + exit 0 |
| 86 | + fi |
| 87 | +
|
| 88 | + # Remove 'v' prefix and split version |
| 89 | + VERSION=${LATEST_TAG#v} |
| 90 | + IFS='.' read -r -a VERSION_PARTS <<< "$VERSION" |
| 91 | + MAJOR=${VERSION_PARTS[0]:-0} |
| 92 | + MINOR=${VERSION_PARTS[1]:-0} |
| 93 | + PATCH=${VERSION_PARTS[2]:-0} |
| 94 | +
|
| 95 | + # Bump version based on type |
| 96 | + case $BUMP_TYPE in |
| 97 | + major) |
| 98 | + MAJOR=$((MAJOR + 1)) |
| 99 | + MINOR=0 |
| 100 | + PATCH=0 |
| 101 | + ;; |
| 102 | + minor) |
| 103 | + MINOR=$((MINOR + 1)) |
| 104 | + PATCH=0 |
| 105 | + ;; |
| 106 | + patch) |
| 107 | + PATCH=$((PATCH + 1)) |
| 108 | + ;; |
| 109 | + esac |
| 110 | +
|
| 111 | + NEW_TAG="v${MAJOR}.${MINOR}.${PATCH}" |
| 112 | + echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT |
| 113 | + echo "New tag: $NEW_TAG" |
| 114 | +
|
| 115 | + # Get commit message for changelog |
| 116 | + COMMIT_MSG=$(git log -1 --pretty=%B) |
| 117 | + echo "changelog=$COMMIT_MSG" >> $GITHUB_OUTPUT |
| 118 | +
|
| 119 | + - name: Create and push tag |
| 120 | + if: steps.tag.outputs.new_tag != '' |
| 121 | + run: | |
| 122 | + NEW_TAG="${{ steps.tag.outputs.new_tag }}" |
| 123 | + git config user.name "github-actions[bot]" |
| 124 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 125 | + git tag -a "$NEW_TAG" -m "Release $NEW_TAG" |
| 126 | + git push origin "$NEW_TAG" |
| 127 | + echo "✓ Created and pushed tag: $NEW_TAG" |
| 128 | +
|
| 129 | + build-and-push: |
| 130 | + runs-on: ubuntu-latest |
| 131 | + needs: [semantic-version] |
| 132 | + if: github.event_name != 'pull_request' |
| 133 | + steps: |
| 134 | + - name: Checkout code |
| 135 | + uses: actions/checkout@v6 |
| 136 | + |
| 137 | + - name: Set up Docker Buildx |
| 138 | + uses: docker/setup-buildx-action@v3 |
| 139 | + |
| 140 | + - name: Log in to Container Registry |
| 141 | + uses: docker/login-action@v3 |
| 142 | + with: |
| 143 | + registry: ${{ env.REGISTRY }} |
| 144 | + username: ${{ github.actor }} |
| 145 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 146 | + |
| 147 | + - name: Extract metadata |
| 148 | + id: meta |
| 149 | + uses: docker/metadata-action@v5 |
| 150 | + with: |
| 151 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 152 | + tags: | |
| 153 | + type=ref,event=branch |
| 154 | + type=semver,pattern={{version}} |
| 155 | + type=sha,prefix={{branch}}-,enable={{is_default_branch}} |
| 156 | + type=raw,value=latest,enable={{is_default_branch}} |
| 157 | + type=raw,value=${{ needs.semantic-version.outputs.new_tag }},enable=${{ needs.semantic-version.outputs.new_tag != '' }} |
| 158 | +
|
| 159 | + - name: Build and push Docker image |
| 160 | + id: build |
| 161 | + uses: docker/build-push-action@v6 |
| 162 | + with: |
| 163 | + context: . |
| 164 | + platforms: linux/amd64,linux/arm64 |
| 165 | + push: true |
| 166 | + tags: ${{ steps.meta.outputs.tags }} |
| 167 | + labels: ${{ steps.meta.outputs.labels }} |
| 168 | + cache-from: type=gha |
| 169 | + cache-to: type=gha,mode=max |
| 170 | + provenance: true |
| 171 | + sbom: true |
| 172 | + |
| 173 | + - name: Generate artifact attestation |
| 174 | + uses: actions/attest-build-provenance@v3 |
| 175 | + with: |
| 176 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 177 | + subject-digest: ${{ steps.build.outputs.digest }} |
| 178 | + push-to-registry: true |
| 179 | + |
| 180 | + - name: Run Trivy vulnerability scanner (SARIF) |
| 181 | + uses: aquasecurity/trivy-action@master |
| 182 | + with: |
| 183 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build.outputs.digest }} |
| 184 | + format: "sarif" |
| 185 | + output: "trivy-results.sarif" |
| 186 | + severity: "CRITICAL,HIGH" |
| 187 | + |
| 188 | + - name: Upload Trivy results to GitHub Security |
| 189 | + uses: github/codeql-action/upload-sarif@v4 |
| 190 | + if: always() |
| 191 | + with: |
| 192 | + sarif_file: "trivy-results.sarif" |
| 193 | + |
| 194 | + - name: Run Trivy vulnerability scanner (table) |
| 195 | + uses: aquasecurity/trivy-action@master |
| 196 | + with: |
| 197 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build.outputs.digest }} |
| 198 | + format: "table" |
| 199 | + severity: "CRITICAL,HIGH" |
| 200 | + |
| 201 | + validate-docker: |
| 202 | + runs-on: ubuntu-latest |
| 203 | + if: github.event_name == 'pull_request' |
| 204 | + steps: |
| 205 | + - name: Checkout code |
| 206 | + uses: actions/checkout@v6 |
| 207 | + |
| 208 | + - name: Set up Docker Buildx |
| 209 | + uses: docker/setup-buildx-action@v3 |
| 210 | + |
| 211 | + - name: Build Docker image (validation only) |
| 212 | + id: build |
| 213 | + uses: docker/build-push-action@v6 |
| 214 | + with: |
| 215 | + context: . |
| 216 | + push: false |
| 217 | + load: true |
| 218 | + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pr-${{ github.event.pull_request.number }} |
| 219 | + cache-from: type=gha |
| 220 | + cache-to: type=gha,mode=max |
| 221 | + |
| 222 | + - name: Run Trivy vulnerability scanner on PR |
| 223 | + uses: aquasecurity/trivy-action@master |
| 224 | + with: |
| 225 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pr-${{ github.event.pull_request.number }} |
| 226 | + format: "table" |
| 227 | + severity: "CRITICAL,HIGH" |
| 228 | + exit-code: "1" |
| 229 | + |
| 230 | + create-release: |
| 231 | + runs-on: ubuntu-latest |
| 232 | + needs: [semantic-version, build-and-push] |
| 233 | + if: | |
| 234 | + github.event_name == 'push' && |
| 235 | + github.ref == 'refs/heads/main' && |
| 236 | + needs.semantic-version.outputs.new_tag != '' |
| 237 | + steps: |
| 238 | + - name: Checkout code |
| 239 | + uses: actions/checkout@v6 |
| 240 | + |
| 241 | + - name: Create GitHub Release |
| 242 | + uses: softprops/action-gh-release@v2 |
| 243 | + with: |
| 244 | + tag_name: ${{ needs.semantic-version.outputs.new_tag }} |
| 245 | + name: Release ${{ needs.semantic-version.outputs.new_tag }} |
| 246 | + body: | |
| 247 | + ## Changes |
| 248 | + ${{ needs.semantic-version.outputs.changelog }} |
| 249 | +
|
| 250 | + ## Docker Image |
| 251 | + ```bash |
| 252 | + docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.semantic-version.outputs.new_tag }} |
| 253 | + ``` |
| 254 | + ## What's Changed |
| 255 | + **Full Changelog**: https://github.com/${{ github.repository }}/compare/${{ needs.semantic-version.outputs.new_tag }}...main |
| 256 | + draft: false |
| 257 | + prerelease: false |
| 258 | + generate_release_notes: true |
0 commit comments