Build Docker images for Antragsgrün #201
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 images for Antragsgrün" | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- "master" | |
schedule: | |
- cron: '11 3 * * 1' | |
env: | |
FIRST_COMMIT_REFLECTED: "a911b33a" | |
REGISTRY_URL: "docker.io/" | |
IMAGE_NAME: "devopsansiblede/antragsgruen" | |
jobs: | |
get_tags_to_build: | |
runs-on: "ubuntu-latest" | |
outputs: | |
tags: "${{ steps.antragsgruen_tags.outputs.tags }}" | |
retagging: "${{ steps.antragsgruen_tags.outputs.retagging }}" | |
steps: | |
- name: "Checkout this repository" | |
uses: "actions/checkout@v4" | |
with: | |
ref: "${{ inputs.username }}" | |
path: "docker" | |
- name: "Checkout CatoTH Antragsgruen" | |
uses: "actions/checkout@v4" | |
with: | |
repository: "CatoTH/antragsgruen" | |
path: "docker/app" | |
- name: "prepare tags" | |
run: | | |
cd ./docker/app | |
git fetch --all --tags | |
- name: "get actual app tags" | |
id: "antragsgruen_tags" | |
# jq -c '{ "tags": map({"tag": .}) }' | |
run: | | |
tags_reflected=$( cd ./docker/app && git tag --contains "${{ env.FIRST_COMMIT_REFLECTED }}" ) | |
# get latest version | |
versionTags=$( echo "${tags_reflected}" | sed -E '/^v?[0-9]+\.[0-9]+\.[0-9]+$/!d' | uniq ) | |
versionHelper=$( echo "${versionTags}" | sed -E 's/^v?(.*)$/\1/g' | sort --version-sort ) | |
latest=$( echo "${versionTags}" | grep "$( echo "${versionHelper}" | tail -n 1 )" ) | |
# get all tags to build | |
built_tags="$( | |
cat ./docker/built_tags.json | | |
jq 'keys | { "built": . }' | |
)" | |
tags_json="$( | |
jq --raw-input --slurp 'split("\n") | .[0:-1] | { "all" : . }' <<< $( echo "${tags_reflected}" ) | |
)" | |
tags_json="$( echo "${tags_json}" | | |
jq --argjson builtJson "${built_tags}" '. + $builtJson' | | |
jq '. as $d | .all | del( .[ indices($d.built[])[] ] )' | | |
jq --arg latest "${latest}" '. + [ $latest ]' | | |
jq -c '{ "tags": . | unique }' | |
)" | |
# get all tags that should be retagged | |
version_retagging=$( | |
jq --null-input --arg latest "${latest}" '{ "latest": $latest }' | |
) | |
v2=$( echo "${versionHelper}" | sed -E 's/^v?([0-9]+\.[0-9]+)\.[0-9]+$/\1/g' | uniq ) | |
IFS=$'\n' | |
for v in ${v2}; do | |
vv=$( echo "${versionHelper}" | grep "${v}" | tail -n 1 ) | |
if [ ! -z "${vv}" ]; then | |
version_retagging=$( | |
echo "${version_retagging}" | | |
jq --arg key "v${v}" --arg val "$( echo "${versionTags}" | grep "${vv}" )" '.[$key] = $val' | |
) | |
fi | |
done | |
v1=$( echo "${versionHelper}" | sed -E 's/^v?([0-9]+)\.[0-9]+\.[0-9]+$/\1/g' | uniq ) | |
for v in ${v1}; do | |
vv=$( echo "${versionHelper}" | grep "${v}" | tail -n 1 ) | |
if [ ! -z "${vv}" ]; then | |
version_retagging=$( | |
echo "${version_retagging}" | | |
jq --arg key "v${v}" --arg val "$( echo "${versionTags}" | grep "${vv}" )" '.[$key] = $val' | |
) | |
fi | |
done | |
version_retagging=$( echo "${version_retagging}" | jq -c '.' ) | |
# print debugging information to log | |
echo "Tags being built:" | |
echo "${tags_json}" | |
echo | |
echo "Tags potentially being retagged:" | |
echo "${version_retagging}" | |
# use github workflow output to transfer information | |
echo "retagging=${version_retagging}" >> $GITHUB_OUTPUT | |
echo "tags=${tags_json}" >> $GITHUB_OUTPUT | |
build_master_as_dev_image: | |
needs: "get_tags_to_build" | |
runs-on: "ubuntu-latest" | |
steps: | |
- name: "Checkout this repository" | |
uses: "actions/checkout@v4" | |
with: | |
ref: "${{ inputs.username }}" | |
path: "docker" | |
- name: "Checkout CatoTH Antragsgruen" | |
uses: "actions/checkout@v4" | |
with: | |
repository: "CatoTH/antragsgruen" | |
path: "docker/app" | |
- name: "Login to DockerHub" | |
uses: "docker/login-action@v2" | |
with: | |
username: "${{ secrets.DOCKERHUB_USER }}" | |
password: "${{ secrets.DOCKERHUB_TOKEN }}" | |
- name: "Set up QEMU" | |
uses: "docker/setup-qemu-action@v2" | |
- name: "Set up Docker Buildx" | |
uses: "docker/setup-buildx-action@v2" | |
- name: "Build and push development" | |
id: "docker_build" | |
uses: "docker/build-push-action@v3" | |
with: | |
push: true | |
tags: "${{ env.IMAGE_NAME }}:development" | |
no-cache: true | |
context: "./docker/" | |
file: "./docker/Dockerfile" | |
platforms: linux/amd64,linux/arm64 | |
- name: "prepare alternative tags" | |
id: "dev_retag" | |
run: | | |
dt=$( date '+%Y%m%d-%H%M' ) | |
retag=$( | |
echo "${{ env.REGISTRY_URL }}${{ env.IMAGE_NAME }}:dev_${dt}" | |
) | |
echo "${retag}" | |
echo "retag=${retag}" >> $GITHUB_OUTPUT | |
- name: "Push image as dated dev image" | |
uses: "akhilerm/tag-push-action@v2.1.0" | |
with: | |
src: "${{ env.REGISTRY_URL }}${{ env.IMAGE_NAME }}:development" | |
dst: | | |
${{ steps.dev_retag.outputs.retag }} | |
build_missing_images: | |
needs: "get_tags_to_build" | |
runs-on: "ubuntu-latest" | |
strategy: | |
fail-fast: true | |
matrix: "${{ fromJSON( needs.get_tags_to_build.outputs.tags ) }}" | |
steps: | |
- name: "Login to DockerHub" | |
uses: "docker/login-action@v2" | |
with: | |
username: "${{ secrets.DOCKERHUB_USER }}" | |
password: "${{ secrets.DOCKERHUB_TOKEN }}" | |
- name: "Checkout this repository" | |
uses: "actions/checkout@v4" | |
with: | |
ref: "master" | |
path: "docker" | |
- name: "Checkout CatoTH Antragsgruen" | |
uses: "actions/checkout@v4" | |
with: | |
repository: "CatoTH/antragsgruen" | |
ref: "${{ matrix.tags }}" | |
path: "docker/app" | |
- name: "Set up QEMU" | |
uses: "docker/setup-qemu-action@v2" | |
- name: "Set up Docker Buildx" | |
uses: "docker/setup-buildx-action@v2" | |
- name: "Build and push ${{ matrix.tags }}" | |
id: "docker_build" | |
uses: "docker/build-push-action@v3" | |
with: | |
push: true | |
tags: "${{ env.IMAGE_NAME }}:${{ matrix.tags }}" | |
no-cache: true | |
context: "./docker/" | |
file: "./docker/Dockerfile" | |
platforms: linux/amd64,linux/arm64 | |
- name: "prepare alternative tags if applicable" | |
id: "alt_tags" | |
run: | | |
# fetch all alternative tags | |
alternativeTags=$( | |
echo '${{ needs.get_tags_to_build.outputs.retagging }}' | | |
jq 'keys[] as $k | if ( .[$k] == "${{ matrix.tags }}" ) then [ $k ] else empty end' | | |
jq -s add | |
) | |
retag='' | |
if [[ $( echo "${alternativeTags}" | jq 'length' ) -gt 0 ]]; then | |
echo "Tag “${{ matrix.tags }}” will also be pushed as those tags:" | |
alternativeTags=$( | |
echo "${alternativeTags}" | | |
jq -r '.[]' | |
) | |
IFS=$'\n' | |
for tag in $( echo "${alternativeTags}" ); do | |
echo " ◆ ${tag}" | |
if [ -z "${retag}" ]; then | |
retag="${{ env.REGISTRY_URL }}${{ env.IMAGE_NAME }}:${tag}" | |
else | |
retag="${retag}"$'\n'"${{ env.REGISTRY_URL }}${{ env.IMAGE_NAME }}:${tag}" | |
fi | |
done | |
else | |
echo 'No alternative tags to push for tag “${{ matrix.tags }}”' | |
fi | |
echo 'retag<<EOD' >> $GITHUB_OUTPUT | |
echo "${retag}" >> $GITHUB_OUTPUT | |
echo 'EOD' >> $GITHUB_OUTPUT | |
- name: "Push re-tagged images " | |
uses: "akhilerm/tag-push-action@v2.1.0" | |
with: | |
src: "${{ env.REGISTRY_URL }}${{ env.IMAGE_NAME }}:${{ matrix.tags }}" | |
dst: | | |
${{ steps.alt_tags.outputs.retag }} | |
if: "${{ steps.alt_tags.outputs.retag != '' }}" | |
documentation: | |
needs: | |
- "get_tags_to_build" | |
- "build_missing_images" | |
runs-on: "ubuntu-latest" | |
steps: | |
- name: "Checkout this repository" | |
uses: "actions/checkout@v4" | |
with: | |
ref: "master" | |
path: "docker" | |
- name: "Add build date to JSON" | |
run: | | |
dt=$( date '+%Y-%m-%d %H:%M (%Z)' ) | |
fileName="./docker/built_tags.json" | |
tagList=$( echo '${{ needs.get_tags_to_build.outputs.tags }}' | jq -r '.tags[]' ) | |
IFS=$'\n' | |
for tag in $( echo "${tagList}" ); do | |
echo "$( cat "${fileName}" | jq -S --arg key "${tag}" --arg val "${dt}" '.[ $key ] = $val' )" > "${fileName}" | |
done | |
- name: "Commit last built information" | |
uses: "stefanzweifel/git-auto-commit-action@v4" | |
with: | |
commit_message: "Last built tags" | |
commit_user_name: "GitHub Actions" | |
commit_user_email: "dev@winter-martin.de" | |
repository: "./docker/" | |
... |