|
| 1 | +name: Build and Publish Docker Image |
| 2 | + |
| 3 | +# Triggers: |
| 4 | +# 1. Push to master whose commit message starts with "chore: sync upstream v..." |
| 5 | +# (i.e. a clean auto-merge from teal-sync-upstream-release.yml) |
| 6 | +# 2. A pull_request from a sync/upstream-v* branch is merged to master |
| 7 | +# (i.e. a conflict resolution PR from teal-sync-upstream-release.yml) |
| 8 | +# 3. Manual dispatch — useful for rebuilding any version on demand. |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: |
| 13 | + - master |
| 14 | + pull_request: |
| 15 | + types: [closed] |
| 16 | + workflow_dispatch: |
| 17 | + inputs: |
| 18 | + version: |
| 19 | + description: "Upstream version tag to label the image (e.g. v0.53.2). Defaults to the current latest upstream release." |
| 20 | + required: false |
| 21 | + |
| 22 | +jobs: |
| 23 | + # ── Resolve whether this run should produce a build, and which version tag ── |
| 24 | + |
| 25 | + resolve: |
| 26 | + name: Resolve version |
| 27 | + runs-on: ubuntu-latest |
| 28 | + outputs: |
| 29 | + should_build: ${{ steps.resolve.outputs.should_build }} |
| 30 | + version: ${{ steps.resolve.outputs.version }} |
| 31 | + steps: |
| 32 | + - name: Determine version and build gate |
| 33 | + id: resolve |
| 34 | + env: |
| 35 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + run: | |
| 37 | + EVENT="${{ github.event_name }}" |
| 38 | +
|
| 39 | + if [[ "$EVENT" == "workflow_dispatch" ]]; then |
| 40 | + VERSION="${{ inputs.version }}" |
| 41 | + if [[ -z "$VERSION" ]]; then |
| 42 | + VERSION=$(gh api repos/metabase/metabase/releases/latest --jq '.tag_name') |
| 43 | + fi |
| 44 | + echo "should_build=true" >> "$GITHUB_OUTPUT" |
| 45 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 46 | + echo "Manual dispatch — building $VERSION" |
| 47 | +
|
| 48 | + elif [[ "$EVENT" == "push" ]]; then |
| 49 | + COMMIT_MSG="${{ github.event.head_commit.message }}" |
| 50 | + if [[ "$COMMIT_MSG" =~ ^chore:\ sync\ upstream\ (v[0-9]+\.[0-9x.]+) ]]; then |
| 51 | + VERSION="${BASH_REMATCH[1]}" |
| 52 | + echo "should_build=true" >> "$GITHUB_OUTPUT" |
| 53 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 54 | + echo "Sync commit detected — building $VERSION" |
| 55 | + else |
| 56 | + echo "should_build=false" >> "$GITHUB_OUTPUT" |
| 57 | + echo "Not a sync commit — skipping." |
| 58 | + fi |
| 59 | +
|
| 60 | + elif [[ "$EVENT" == "pull_request" ]]; then |
| 61 | + MERGED="${{ github.event.pull_request.merged }}" |
| 62 | + BRANCH="${{ github.event.pull_request.head.ref }}" |
| 63 | +
|
| 64 | + if [[ "$MERGED" == "true" ]] && [[ "$BRANCH" =~ ^sync/upstream-v ]]; then |
| 65 | + VERSION=$(echo "$BRANCH" | grep -oP 'v[0-9]+\.[0-9x.]+' || true) |
| 66 | + if [[ -z "$VERSION" ]]; then |
| 67 | + VERSION=$(gh api repos/metabase/metabase/releases/latest --jq '.tag_name') |
| 68 | + fi |
| 69 | + echo "should_build=true" >> "$GITHUB_OUTPUT" |
| 70 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 71 | + echo "Upstream-sync PR merged — building $VERSION" |
| 72 | + else |
| 73 | + echo "should_build=false" >> "$GITHUB_OUTPUT" |
| 74 | + echo "PR is not a merged upstream-sync — skipping." |
| 75 | + fi |
| 76 | + fi |
| 77 | +
|
| 78 | + # ── Build the Docker image and push to GHCR ── |
| 79 | + |
| 80 | + build-and-push: |
| 81 | + name: Build and push to GHCR |
| 82 | + needs: resolve |
| 83 | + if: needs.resolve.outputs.should_build == 'true' |
| 84 | + runs-on: ubuntu-latest |
| 85 | + timeout-minutes: 90 |
| 86 | + permissions: |
| 87 | + contents: read |
| 88 | + packages: write |
| 89 | + |
| 90 | + steps: |
| 91 | + - name: Checkout |
| 92 | + uses: actions/checkout@v6 |
| 93 | + |
| 94 | + # Docker image names must be lowercase; github.repository preserves case. |
| 95 | + - name: Normalise image name |
| 96 | + id: image |
| 97 | + run: | |
| 98 | + echo "name=ghcr.io/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" \ |
| 99 | + >> "$GITHUB_OUTPUT" |
| 100 | +
|
| 101 | + - name: Set up Docker Buildx |
| 102 | + uses: docker/setup-buildx-action@v4 |
| 103 | + |
| 104 | + - name: Log in to GHCR |
| 105 | + uses: docker/login-action@v4 |
| 106 | + with: |
| 107 | + registry: ghcr.io |
| 108 | + username: ${{ github.actor }} |
| 109 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 110 | + |
| 111 | + - name: Build and push |
| 112 | + uses: docker/build-push-action@v7 |
| 113 | + with: |
| 114 | + context: . |
| 115 | + push: true |
| 116 | + build-args: | |
| 117 | + MB_EDITION=oss |
| 118 | + VERSION=${{ needs.resolve.outputs.version }} |
| 119 | + tags: | |
| 120 | + ${{ steps.image.outputs.name }}:${{ needs.resolve.outputs.version }} |
| 121 | + ${{ steps.image.outputs.name }}:latest |
| 122 | + # Layer cache stored in GitHub Actions cache — speeds up subsequent builds |
| 123 | + # significantly since the apt/JDK/Clojure install layers rarely change. |
| 124 | + cache-from: type=gha |
| 125 | + cache-to: type=gha,mode=max |
| 126 | + |
| 127 | + - name: Summary |
| 128 | + run: | |
| 129 | + IMAGE="${{ steps.image.outputs.name }}" |
| 130 | + VERSION="${{ needs.resolve.outputs.version }}" |
| 131 | + { |
| 132 | + echo "### 🐳 Docker image published" |
| 133 | + echo "" |
| 134 | + echo "| Tag | Image |" |
| 135 | + echo "|-----|-------|" |
| 136 | + echo "| \`$VERSION\` | \`$IMAGE:$VERSION\` |" |
| 137 | + echo "| \`latest\` | \`$IMAGE:latest\` |" |
| 138 | + echo "" |
| 139 | + echo "\`\`\`bash" |
| 140 | + echo "docker pull $IMAGE:$VERSION" |
| 141 | + echo "\`\`\`" |
| 142 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments