Improve image tagging #49
Workflow file for this run
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: | |
push: | |
branches: | |
- main | |
tags: | |
- v* | |
pull_request: | |
env: | |
IMAGE_NAME: netbox-operator | |
DOCKER_METADATA_PR_HEAD_SHA: true | |
jobs: | |
push: | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
contents: read | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Generate docker image tag | |
id: meta | |
uses: docker/[email protected] | |
with: | |
images: ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }} | |
tags: | | |
# for commits on main branch, the tag name would be latest | |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} | |
# for commits on PRs, the tag name would be sha-[short sha value] | |
type=sha,enable=true | |
- name: Build and push image | |
run: | | |
echo IMAGE_TAG="${{ steps.meta.outputs.tags }}" | |
echo DOCKER_LABELS="${{ steps.meta.outputs.labels }}" | |
echo IS_MAIN_BRANCH="${{ github.ref == format('refs/heads/{0}', 'main') }}" | |
# build and push | |
if [ "${IS_MAIN_BRANCH}" = "true" ] | |
then | |
echo "docker buildx build --platform linux/amd64,linux/arm64 --push --tag ${IMAGE_TAG} --label ${{ steps.meta.outputs.labels }} ." | |
docker buildx build --platform linux/amd64,linux/arm64 --push --tag "${IMAGE_TAG}" --label "${{ steps.meta.outputs.labels }}" . | |
else | |
echo "docker buildx build --platform linux/amd64,linux/arm64 --tag ${{ steps.meta.outputs.tags }} --label ${{ steps.meta.outputs.labels }} ." | |
docker buildx build --platform linux/amd64,linux/arm64 --tag "${{ steps.meta.outputs.tags }}" --label "${{ steps.meta.outputs.labels }}" . | |
fi | |