Build docker/main #32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Build and push the chatmail-relay Docker image (ghcr.io/chatmail/docker). | |
| # | |
| # Triggers: | |
| # 1. Push/PR to this repo (Dockerfile changes) -- builds relay@main | |
| # 2. repository_dispatch from chatmail/relay -- builds the dispatched relay ref | |
| # 3. workflow_dispatch -- manual build of any relay branch/tag | |
| # | |
| # Tags: sha-<relay-7char>, branch name, semver (on release tags) | |
| # PRs: build only, no push | |
| name: Build and push chatmail-relay image | |
| run-name: >- | |
| ${{ github.event_name == 'repository_dispatch' | |
| && format('Build relay/{0} ({1})', | |
| github.event.client_payload.relay_ref, | |
| github.event.client_payload.relay_sha_short) | |
| || github.event_name == 'workflow_dispatch' | |
| && format('Build relay/{0}', | |
| github.event.inputs.relay_ref || 'main') | |
| || format('Build docker/{0}', github.ref_name) }} | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| paths-ignore: | |
| - '**/README.md' | |
| - 'CHANGELOG.md' | |
| - 'LICENSE' | |
| repository_dispatch: | |
| types: [relay-updated] | |
| workflow_dispatch: | |
| inputs: | |
| relay_ref: | |
| description: 'Relay branch or tag to build (default: main)' | |
| default: 'main' | |
| relay_sha: | |
| description: 'Relay commit SHA (leave empty for HEAD of ref)' | |
| default: '' | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: chatmail/docker | |
| jobs: | |
| build: | |
| name: Build Docker image | |
| runs-on: ubuntu-latest | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.tag }} | |
| relay_ref: ${{ steps.meta.outputs.relay_ref }} | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Compute relay ref | |
| id: relay | |
| run: | | |
| # Precedence: repository_dispatch payload > workflow_dispatch input > main | |
| REF="${{ github.event.client_payload.relay_ref || github.event.inputs.relay_ref || 'main' }}" | |
| SHA="${{ github.event.client_payload.relay_sha || github.event.inputs.relay_sha || '' }}" | |
| echo "ref=${REF}" >> "$GITHUB_OUTPUT" | |
| echo "sha=${SHA}" >> "$GITHUB_OUTPUT" | |
| - name: Checkout relay repo as build context | |
| uses: actions/checkout@v6 | |
| with: | |
| repository: chatmail/relay | |
| ref: ${{ steps.relay.outputs.sha || steps.relay.outputs.ref }} | |
| - name: Checkout docker repo | |
| uses: actions/checkout@v6 | |
| with: | |
| path: docker | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v4 | |
| - name: Login to GHCR | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v4 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Compute build metadata | |
| id: meta | |
| run: | | |
| IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" | |
| RELAY_REF="${{ steps.relay.outputs.ref }}" | |
| # Relay SHA comes from the relay checkout at workspace root | |
| RELAY_SHA=$(git rev-parse HEAD) | |
| RELAY_SHA_SHORT="${RELAY_SHA:0:7}" | |
| BUILD_DATE=$(git log -1 --format=%cI) | |
| COMMIT_MSG=$(git log -1 --format=%s) | |
| echo "relay_sha=${RELAY_SHA}" >> "$GITHUB_OUTPUT" | |
| echo "relay_sha_short=${RELAY_SHA_SHORT}" >> "$GITHUB_OUTPUT" | |
| echo "relay_ref=${RELAY_REF}" >> "$GITHUB_OUTPUT" | |
| echo "build_date=${BUILD_DATE}" >> "$GITHUB_OUTPUT" | |
| echo "tag=sha-${RELAY_SHA_SHORT}" >> "$GITHUB_OUTPUT" | |
| # -- Tags -- | |
| # Always: relay SHA tag | |
| TAGS="${IMAGE}:sha-${RELAY_SHA_SHORT}" | |
| if [ "${{ github.event_name }}" = "push" ] || \ | |
| [ "${{ github.event_name }}" = "pull_request" ]; then | |
| # Docker-repo push/PR: add docker branch tag | |
| TAGS="${TAGS}"$'\n'"${IMAGE}:${{ github.ref_name }}" | |
| else | |
| # Dispatch: tags based on relay ref | |
| BRANCH_TAG=$(echo "${RELAY_REF}" | sed 's|/|-|g') | |
| TAGS="${TAGS}"$'\n'"${IMAGE}:${BRANCH_TAG}" | |
| if [[ "${RELAY_REF}" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then | |
| VERSION="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}" | |
| MINOR="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" | |
| TAGS="${TAGS}"$'\n'"${IMAGE}:${VERSION}" | |
| TAGS="${TAGS}"$'\n'"${IMAGE}:${MINOR}" | |
| TAGS="${TAGS}"$'\n'"${IMAGE}:latest" | |
| fi | |
| fi | |
| echo "tags<<EOF" >> "$GITHUB_OUTPUT" | |
| echo "${TAGS}" >> "$GITHUB_OUTPUT" | |
| echo "EOF" >> "$GITHUB_OUTPUT" | |
| # -- Labels -- | |
| # Use relay commit message as description on dispatch builds | |
| if [ "${{ github.event_name }}" = "repository_dispatch" ] || \ | |
| [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
| DESC="relay/${RELAY_REF}: ${COMMIT_MSG}" | |
| else | |
| DESC="Chatmail relay server - optimized email for Delta Chat" | |
| fi | |
| { | |
| echo "labels<<EOF" | |
| echo "org.opencontainers.image.title=chatmail-relay" | |
| echo "org.opencontainers.image.description=${DESC}" | |
| echo "org.opencontainers.image.revision=${RELAY_SHA}" | |
| echo "org.opencontainers.image.source=https://github.com/chatmail/relay" | |
| echo "org.opencontainers.image.created=${BUILD_DATE}" | |
| echo "com.chatmail.source.ref=${RELAY_REF}" | |
| echo "EOF" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Copy .dockerignore to build context | |
| run: cp docker/.dockerignore .dockerignore | |
| - name: Build and push | |
| uses: docker/build-push-action@v7 | |
| with: | |
| context: . | |
| file: docker/chatmail_relay.dockerfile | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| build-args: | | |
| GIT_HASH=${{ steps.meta.outputs.relay_sha }} | |
| SOURCE_REF=${{ steps.meta.outputs.relay_ref }} | |
| BUILD_DATE=${{ steps.meta.outputs.build_date }} | |
| test: | |
| name: Integration test | |
| needs: build | |
| if: github.event_name != 'pull_request' | |
| # TODO: revert to @main once cmlxc docker support is merged | |
| uses: chatmail/cmlxc/.github/workflows/lxc-test.yml@j4n/docker-support | |
| with: | |
| # TODO: revert to main once cmlxc docker support is merged | |
| cmlxc_ref: j4n/docker-support | |
| cmlxc_commands: | | |
| cmlxc init | |
| cmlxc docker deploy dock0 --source ghcr:${{ needs.build.outputs.image_tag }} | |
| cmlxc test-cmdeploy dock0 --relay-ref ${{ needs.build.outputs.relay_ref }} |