Build and Push Docker Image #93
This file contains 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 Docker Image | |
on: | |
workflow_dispatch: | |
push: | |
branches: [main] | |
tags: 'v*.*.*' | |
jobs: | |
build: | |
name: 'Build and Push Docker Image' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Prepare Tags | |
id: prep | |
run: | | |
DOCKER_IMAGE=${{ secrets.DOCKER_USERNAME }}/${GITHUB_REPOSITORY#*/} | |
VERSION=${GITHUB_REF##*/} | |
# If this is the main branch, use 'latest' instead | |
if [[ $GITHUB_REF == refs/heads/main ]]; then | |
VERSION=latest | |
fi | |
TAGS="${DOCKER_IMAGE}:${VERSION}" | |
# If the version is a version number, also tag with 'latest' | |
if [[ $VERSION =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
TAGS="$TAGS,${DOCKER_IMAGE}:latest" | |
fi | |
# Tag with the GitHub run id to create a unique tag for this run | |
TAGS="$TAGS,${DOCKER_IMAGE}:${GITHUB_RUN_ID}" | |
echo "tags=${TAGS}" >> $GITHUB_OUTPUT | |
echo "docker_image=${DOCKER_IMAGE}" >> $GITHUB_OUTPUT | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
with: | |
platforms: all | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to Docker | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and Push | |
uses: docker/build-push-action@v4 | |
with: | |
builder: ${{ steps.buildx.outputs.name }} | |
context: . | |
file: ./Dockerfile | |
platforms: linux/amd64 | |
push: true | |
tags: ${{ steps.prep.outputs.tags }} |