|
| 1 | +name: Publish Docker Images (main) |
| 2 | + |
| 3 | +# Triggers on direct pushes to 'main', which includes PR/merge-commit merges. |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: [ "main" ] |
| 7 | + |
| 8 | +env: |
| 9 | + REGISTRY: ghcr.io |
| 10 | + |
| 11 | +jobs: |
| 12 | + bump-version: |
| 13 | + name: Bump VERSION file |
| 14 | + runs-on: ubuntu-latest |
| 15 | + permissions: |
| 16 | + contents: write # needed to push back VERSION + Git tag |
| 17 | + outputs: |
| 18 | + new_version: ${{ steps.bump.outputs.version }} |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + token: ${{ secrets.PAT_TOKEN }} |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Detect source branch and bump version |
| 28 | + id: bump |
| 29 | + run: | |
| 30 | + # ── Detect source branch from merge commit subject ────────────────── |
| 31 | + # Standard merge commit message: "Merge branch 'feature/foo' into main" |
| 32 | + MERGE_SUBJECT="$(git log --merges -1 --pretty=%s HEAD)" |
| 33 | + echo "Merge subject: ${MERGE_SUBJECT}" |
| 34 | +
|
| 35 | + SOURCE=$(echo "$MERGE_SUBJECT" | sed -n "s/Merge branch '\([^']*\)'.*/\1/p") |
| 36 | + SOURCE="${SOURCE#origin/}" # strip remote prefix if present |
| 37 | +
|
| 38 | + if [[ -z "$SOURCE" ]]; then |
| 39 | + SOURCE="unknown" |
| 40 | + echo "::warning::Could not detect source branch; defaulting to 'unknown' (no bump)." |
| 41 | + fi |
| 42 | + echo "Detected source branch: ${SOURCE}" |
| 43 | +
|
| 44 | + # ── Read and parse current VERSION ────────────────────────────────── |
| 45 | + RAW="$(cat VERSION | tr -d '[:space:]')" |
| 46 | + CLEAN="${RAW#v}" # strip leading v |
| 47 | + CLEAN="${CLEAN%-dev}" # strip any existing -dev suffix |
| 48 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$CLEAN" |
| 49 | +
|
| 50 | + # ── Decide bump strategy ───────────────────────────────────────────── |
| 51 | + # When the source branch is 'dev', the version was already incremented |
| 52 | + # on the dev side — just promote it by stripping '-dev' (no new bump). |
| 53 | + # For any other source branch, apply the normal semver bump. |
| 54 | + if [[ "$SOURCE" != "dev" ]]; then |
| 55 | + PREFIX="${SOURCE%%/*}" |
| 56 | + case "$PREFIX" in |
| 57 | + feature) |
| 58 | + MINOR=$(( MINOR + 1 )); PATCH=0 |
| 59 | + ;; |
| 60 | + bugfix|hotfix) |
| 61 | + PATCH=$(( PATCH + 1 )) |
| 62 | + ;; |
| 63 | + release) |
| 64 | + MAJOR=$(( MAJOR + 1 )); MINOR=0; PATCH=0 |
| 65 | + ;; |
| 66 | + *) |
| 67 | + # unknown prefix → no bump |
| 68 | + ;; |
| 69 | + esac |
| 70 | + fi |
| 71 | +
|
| 72 | + # ── Compose new version (no -dev suffix on main) ───────────────────── |
| 73 | + NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}" |
| 74 | + echo "${NEW_VERSION}" > VERSION |
| 75 | +
|
| 76 | + echo "version=${NEW_VERSION}" >> "$GITHUB_OUTPUT" |
| 77 | + echo "New version: ${NEW_VERSION}" |
| 78 | +
|
| 79 | + - name: Commit and push updated VERSION |
| 80 | + run: | |
| 81 | + git config user.name "github-actions[bot]" |
| 82 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 83 | + git add VERSION |
| 84 | + git commit -m "chore: release ${{ steps.bump.outputs.version }} [skip ci]" |
| 85 | + git push |
| 86 | +
|
| 87 | + - name: Create and push Git release tag |
| 88 | + run: | |
| 89 | + git tag "${{ steps.bump.outputs.version }}" |
| 90 | + git push origin "${{ steps.bump.outputs.version }}" |
| 91 | +
|
| 92 | + build-and-push: |
| 93 | + name: Build & Push ${{ matrix.container }} |
| 94 | + needs: bump-version |
| 95 | + runs-on: ubuntu-latest |
| 96 | + strategy: |
| 97 | + fail-fast: false |
| 98 | + matrix: |
| 99 | + container: |
| 100 | + - detector |
| 101 | + - inspector |
| 102 | + - logcollector |
| 103 | + - logserver |
| 104 | + - prefilter |
| 105 | + - monitoring |
| 106 | + - alerter |
| 107 | + permissions: |
| 108 | + contents: read |
| 109 | + packages: write |
| 110 | + id-token: write |
| 111 | + |
| 112 | + steps: |
| 113 | + - name: Checkout repository |
| 114 | + uses: actions/checkout@v4 |
| 115 | + with: |
| 116 | + ref: main # pick up the commit that includes the updated VERSION |
| 117 | + |
| 118 | + - name: Derive partial version tags |
| 119 | + id: version |
| 120 | + run: | |
| 121 | + VERSION="$(cat VERSION | tr -d '[:space:]')" |
| 122 | + CLEAN="${VERSION#v}" |
| 123 | + MAJOR="${CLEAN%%.*}" |
| 124 | + MINOR_PATCH="${CLEAN#*.}" |
| 125 | + MINOR="${MINOR_PATCH%%.*}" |
| 126 | + echo "major=v${MAJOR}" >> "$GITHUB_OUTPUT" |
| 127 | + echo "major_minor=v${MAJOR}.${MINOR}" >> "$GITHUB_OUTPUT" |
| 128 | +
|
| 129 | + - name: Setup Docker buildx |
| 130 | + uses: docker/setup-buildx-action@v3 |
| 131 | + |
| 132 | + - name: Log into registry ${{ env.REGISTRY }} |
| 133 | + uses: docker/login-action@v3 |
| 134 | + with: |
| 135 | + registry: ${{ env.REGISTRY }} |
| 136 | + username: ${{ github.actor }} |
| 137 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 138 | + |
| 139 | + - name: Extract Docker metadata |
| 140 | + id: meta |
| 141 | + uses: docker/metadata-action@v5 |
| 142 | + with: |
| 143 | + images: ${{ env.REGISTRY }}/${{ github.repository }}-${{ matrix.container }} |
| 144 | + tags: | |
| 145 | + type=raw,value=${{ needs.bump-version.outputs.new_version }} |
| 146 | + type=raw,value=${{ steps.version.outputs.major_minor }} |
| 147 | + type=raw,value=${{ steps.version.outputs.major }} |
| 148 | + type=raw,value=latest |
| 149 | +
|
| 150 | + - name: Build and push Docker image |
| 151 | + uses: docker/build-push-action@v5 |
| 152 | + with: |
| 153 | + context: . |
| 154 | + push: true |
| 155 | + tags: ${{ steps.meta.outputs.tags }} |
| 156 | + labels: ${{ steps.meta.outputs.labels }} |
| 157 | + file: ./docker/dockerfiles/Dockerfile.${{ matrix.container }} |
| 158 | + cache-from: type=gha |
| 159 | + cache-to: type=gha,mode=max |
0 commit comments