Build & Publish #36
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 & Publish | |
on: | |
push: | |
branches: | |
- main | |
tags: | |
- v* | |
pull_request: | |
release: | |
types: | |
- created | |
schedule: | |
- cron: "0 18 * * 5" | |
env: | |
IMAGE_NAME: ubuntu22-ansible-test | |
# Kudos to https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows | |
jobs: | |
build-publish: | |
runs-on: ubuntu-20.04 | |
permissions: | |
packages: write | |
contents: read | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Build image | |
run: docker build . --file Dockerfile --tag $IMAGE_NAME | |
- name: Log in to ghcr.io registry | |
if: github.event_name != 'pull_request' | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Push image to ghcr.io | |
if: github.event_name != 'pull_request' | |
run: | | |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/${IMAGE_NAME} | |
# Change all uppercase to lowercase | |
IMAGE_ID=$(echo ${IMAGE_ID} | tr '[A-Z]' '[a-z]') | |
# Strip git ref prefix from version | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
# Strip "v" prefix from tag name | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo ${VERSION} | sed -e 's/^v//') | |
# Use latest tag if changes originate from main branch | |
[ "$VERSION" == "main" ] && VERSION=latest | |
echo IMAGE_ID=${IMAGE_ID} | |
echo VERSION=${VERSION} | |
echo RUNNUMBER=${GITHUB_RUN_ID} | |
docker tag ${IMAGE_NAME} ${IMAGE_ID}:${VERSION} | |
docker push ${IMAGE_ID}:${VERSION} |