Skip to content

Make tag based

Make tag based #2

Workflow file for this run

name: Build and push Docker image
on:
push:
branches:
- '**'
tags:
- 'v*'
permissions:
contents: read
packages: write
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Compute tags based on whether this is a branch push or tag push
- name: Compute image tags
id: vars
run: |
IMAGE="ghcr.io/${GITHUB_REPOSITORY}"
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
TAG_NAME="${GITHUB_REF_NAME}" # e.g. v1.2.3
echo "tags=${IMAGE}:${TAG_NAME},${IMAGE}:latest" >> $GITHUB_OUTPUT
else
BRANCH="${GITHUB_REF_NAME}" # e.g. feature/foo
SAFE_BRANCH=$(echo "$BRANCH" | tr '/' '-') # feature-foo
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
echo "tags=${IMAGE}:branch-${SAFE_BRANCH},${IMAGE}:sha-${SHORT_SHA}" >> $GITHUB_OUTPUT
fi
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.vars.outputs.tags }}