Merge pull request #71 from Xento/main #66
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
| name: Build and publish Docker image | |
| on: | |
| release: | |
| types: [published] | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: 'Git tag (e.g. v0.1.9). If set, a versioned image will be built from this tag.' | |
| required: false | |
| type: string | |
| mark_as_latest: | |
| description: 'Also tag the built image as "latest" (only for manual runs with tag).' | |
| required: false | |
| type: boolean | |
| default: false | |
| workflow_call: | |
| jobs: | |
| lint: | |
| name: Lint Python project | |
| uses: ./.github/workflows/python-lint.yml | |
| with: | |
| python-version: "3.13" | |
| # Use the same ref as the image build: | |
| # - release: use the release tag | |
| # - workflow_dispatch with tag: use that tag | |
| # - other events: empty => default ref (branch) | |
| ref: ${{ github.event_name == 'release' && github.event.release.tag_name || (github.event_name == 'workflow_dispatch' && github.event.inputs.tag || '') }} | |
| build-and-push: | |
| name: Build image and push | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| if: ${{ github.event_name != 'pull_request' }} | |
| needs: [lint] | |
| steps: | |
| # Checkout for release events (use the release tag) | |
| - name: Checkout code for release | |
| if: ${{ github.event_name == 'release' }} | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| ref: ${{ github.event.release.tag_name }} | |
| # Checkout for manual runs with explicit tag | |
| - name: Checkout code for manual tag build | |
| if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.tag != '' }} | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| ref: ${{ github.event.inputs.tag }} | |
| # Default checkout (e.g. push on main, workflow_call, manual run without tag) | |
| - name: Checkout code (default) | |
| if: ${{ !(github.event_name == 'release') && !(github.event_name == 'workflow_dispatch' && github.event.inputs.tag != '') }} | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Determine VERSION and repository name | |
| run: | | |
| VERSION="" | |
| if [[ "${{ github.event_name }}" == "release" ]]; then | |
| VERSION="${{ github.event.release.tag_name }}" | |
| elif [[ "${{ github.event_name }}" == "workflow_dispatch" && -n "${{ github.event.inputs.tag }}" ]]; then | |
| VERSION="${{ github.event.inputs.tag }}" | |
| fi | |
| if [[ -n "$VERSION" ]]; then | |
| # Strip refs/tags/ and a leading "v" | |
| VERSION="${VERSION#refs/tags/}" | |
| VERSION="${VERSION#v}" | |
| echo "Version is $VERSION" | |
| echo "VERSION=$VERSION" >> "$GITHUB_ENV" | |
| fi | |
| REPO_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') | |
| echo "REPO_NAME=$REPO_NAME" >> "$GITHUB_ENV" | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Determine tags | |
| run: | | |
| if [[ -n "${VERSION:-}" ]]; then | |
| # Versioned image for release or manual tag run | |
| TAGS="ghcr.io/${{ env.REPO_NAME }}:${VERSION}" | |
| # Always mark releases as latest | |
| if [[ "${{ github.event_name }}" == "release" ]]; then | |
| TAGS="$TAGS,ghcr.io/${{ env.REPO_NAME }}:latest" | |
| # For manual runs: mark as latest only if requested | |
| elif [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.mark_as_latest }}" == 'true' ]]; then | |
| TAGS="$TAGS,ghcr.io/${{ env.REPO_NAME }}:latest" | |
| fi | |
| echo "TAGS=$TAGS" >> "$GITHUB_ENV" | |
| elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then | |
| # Dev image for pushes to main (and equivalent contexts) | |
| echo "TAGS=ghcr.io/${{ env.REPO_NAME }}:dev" >> "$GITHUB_ENV" | |
| fi | |
| - name: Build and push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: ${{ env.TAGS }} |