Publish Images #6
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: Publish Images | |
on: | |
push: | |
branches: | |
- main | |
tags: | |
- 'v*' | |
paths: | |
- 'images/**/Dockerfile' | |
workflow_dispatch: | |
inputs: | |
tag: | |
description: 'The tag to use for images. Defaults to :latest if not specified.' | |
required: false | |
default: 'latest' | |
jobs: | |
prepare: | |
runs-on: ubuntu-20.04 | |
outputs: | |
tag: ${{ steps.set-tag.outputs.tag }} | |
images: ${{ steps.set-images.outputs.images }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Tag from dispatch | |
if: github.event_name == 'workflow_dispatch' | |
run: echo "tag=${{ github.event.inputs.tag || 'latest' }}" >> $GITHUB_ENV | |
- name: Tag from ref | |
if: startsWith(github.ref, 'refs/tags/') | |
run: echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV | |
- name: Default tag | |
if: github.event_name != 'workflow_dispatch' && !startsWith(github.ref, 'refs/tags/') | |
run: echo "tag=latest" >> $GITHUB_ENV | |
- name: Set tag | |
id: set-tag | |
run: echo "tag=${tag}" >> $GITHUB_OUTPUT | |
- name: List all images | |
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/') | |
run: | | |
image_dirs=$(find images -name 'Dockerfile' -exec dirname {} \; | xargs -n 1 basename | tr '\n' ' ') | |
echo "image_dirs=$image_dirs" >> $GITHUB_ENV | |
- name: List changed images | |
if: github.event_name != 'workflow_dispatch' && !startsWith(github.ref, 'refs/tags/') | |
run: | | |
image_dirs=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep 'images/.*/Dockerfile' | awk -F/ '{print $(NF-1)} | tr '\n' ' ' | sed 's/ $//') | |
echo "image_dirs=$image_dirs" >> $GITHUB_ENV | |
- name: Set image list | |
id: set-images | |
run: | | |
image_dirs_json=$(echo "$image_dirs" | jq -R -c 'split(" ") | map(select(. != ""))') | |
echo "images=${image_dirs_json}}" >> $GITHUB_OUTPUT | |
publish_images: | |
needs: prepare | |
runs-on: ubuntu-20.04 | |
strategy: | |
matrix: | |
image: ${{fromJson(needs.prepare.outputs.images)}} | |
fail-fast: false | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: List image platforms | |
id: list-platforms | |
run: | | |
platforms=$(make -s -C images "${{ matrix.image }}-platforms") | |
echo "platforms=${platforms}" >> $GITHUB_OUTPUT | |
- name: Login to Quay | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.QUAY_USERNAME }} | |
password: ${{ secrets.QUAY_PASSWORD }} | |
registry: quay.io | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: ${{ steps.list-platforms.outputs.platforms }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Cache Buildx Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx | |
- name: Build ${{ matrix.image }} | |
uses: docker/build-push-action@v5 | |
with: | |
platforms: ${{ steps.list-platforms.outputs.platforms }} | |
push: true | |
tags: | | |
quay.io/k0sproject/bootloose-${{ matrix.image }}:${{ needs.prepare.outputs.tag }} | |
context: images/${{ matrix.image }} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache |