name: Publish Docker image on: push: branches: [ "main" ] tags: - "v*" workflow_dispatch: permissions: contents: read packages: write env: REGISTRY: docker.io IMAGE_NAME: ${{ github.repository }} jobs: build-and-push: name: Build and push image runs-on: ubuntu-latest concurrency: group: docker-${{ github.ref }} cancel-in-progress: false steps: - name: Checkout uses: actions/checkout@v4 - name: Set up QEMU (for multi-arch builds) uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Cache Docker layers uses: actions/cache@v4 with: path: /tmp/.buildx-cache key: ${{ runner.os }}-buildx-${{ hashFiles('Dockerfile', 'pubspec.lock') }} restore-keys: | ${{ runner.os }}-buildx- # Docker Hub login (active) - name: Log in to Docker Hub uses: docker/login-action@v3 with: registry: docker.io username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} # GHCR login (commented out for future usage) # - name: Log in to GitHub Container Registry # uses: docker/login-action@v3 # with: # registry: ghcr.io # username: ${{ github.actor }} # password: ${{ secrets.GITHUB_TOKEN }} - name: Extract Docker metadata (tags, labels) id: meta uses: docker/metadata-action@v5 with: images: | ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} flavor: | latest=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} token: ${{ secrets.GITHUB_TOKEN }} - name: Build and push uses: docker/build-push-action@v6 with: context: . file: ./Dockerfile platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max - name: Move cache if: always() run: | if [ -d /tmp/.buildx-cache-new ]; then rm -rf /tmp/.buildx-cache mv /tmp/.buildx-cache-new /tmp/.buildx-cache else echo "No new cache to move" fi