Try a different approach to building the docker image in GitHub workf… #6
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
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'build/dockerfile' | ||
- 'build/requirements.txt' | ||
- 'build/docker-compose.yml' | ||
- '.github/workflows/build-docker-image.yml' | ||
jobs: | ||
build-and-push: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Set image name | ||
run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | ||
- name: Set Docker metadata | ||
id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: ${{ env.IMAGE }} | ||
tags: | | ||
type=raw,value=latest,enable={{is_default_branch}} | ||
type=sha | ||
- name: Set up Docker buildx | ||
uses: docker/setup-buildx-action@v3 | ||
- name: Log in to GitHub Container Registry | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Build and push via Docker compose | ||
uses: docker/bake-action@v5 | ||
with: | ||
files: build/docker-compose.yml | ||
targets: app | ||
set: | | ||
*.tags=${{ steps.meta.outputs.tags }} | ||
*.labels=${{ steps.meta.outputs.labels }} | ||
push: true | ||
name: Build and Push Docker Image | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'build/dockerfile' | ||
- 'build/requirements.txt' | ||
- 'build/docker-compose.yml' | ||
- '.github/workflows/build.yml' | ||
jobs: | ||
build-and-push: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
env: | ||
IMAGE_NAME: ghcr.io/${{ github.repository }} | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Lowercase the image name | ||
run: echo "IMAGE_NAME=${IMAGE_NAME,,}" >> $GITHUB_ENV | ||
- name: Log in to GitHub Container Registry | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Build Docker image with Compose | ||
run: docker compose -f build/docker-compose.yml build | ||
- name: Tag Docker images | ||
run: | | ||
docker tag "$IMAGE_NAME:latest" "$IMAGE_NAME:latest" | ||
docker tag "$IMAGE_NAME:latest" "$IMAGE_NAME:git-${GITHUB_SHA::7}" | ||
- name: Push Docker images | ||
run: | | ||
docker push "$IMAGE_NAME:latest" | ||
docker push "$IMAGE_NAME:git-${GITHUB_SHA::7}" | ||