diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 5468e8c..43da0f4 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -2,17 +2,59 @@ name: Docker Image CI on: push: - branches: [ "main" ] + branches: ['main'] pull_request: - branches: [ "main" ] -jobs: +concurrency: + group: '${{ github.workflow }} @ ${{ github.ref }}' + cancel-in-progress: true - build: +env: + REGISTRY: ghcr.io + BASE_IMAGE_NAME: ${{ github.repository }} +jobs: + build-and-push-base-image: runs-on: ubuntu-latest + permissions: + contents: read + packages: write steps: - - uses: actions/checkout@v3 - - name: Build the Docker image - run: docker build . --file Containerfile --tag ha-discord-mute-sensor:$(date +%s) + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to the Container registry + uses: docker/login-action@v3 + if: github.event_name != 'pull_request' + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }} + + - name: Build and push Docker image + if: github.event_name != 'pull_request' + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64 + file: Containerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Build Docker image + if: github.event_name == 'pull_request' + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64 + push: false + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}