Skip to content

Add Initial Docker Images #3

Add Initial Docker Images

Add Initial Docker Images #3

Workflow file for this run

name: Build & Publish Docker Images
on:
push:
branches: [ 'main' ]
pull_request:
env:
REGISTRY: ghcr.io
jobs:
calculate-images-to-build:
name: Calculate Images To Build
runs-on: ubuntu-latest
outputs:
changed_dirs: ${{ steps.filter_config_directories.outputs.changed_dirs }}
steps:
- name: "Checkout repository"
uses: actions/checkout@v4
- name: "Calculate changed files directories"
id: calculate_changed_files
uses: tj-actions/changed-files@v44
with:
dir_names: true
dir_names_exclude_current_dir: true
json: true
- name: "Filter out config directories"
id: filter_config_directories
run: |
CHANGED_DIRS=$(echo "${{ steps.calculate_changed_files.outputs.all_changed_files }}" | jq -c '[.[] | select(. | contains(".") | not)'])
echo "changed_dirs=$CHANGED_DIRS" >> "$GITHUB_OUTPUT"
build-and-push-image:
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
attestations: write
id-token: write
needs:
- calculate-images-to-build
strategy:
matrix:
changed_dir: ${{ fromJSON(needs.calculate-images-to-build.outputs.changed_dirs ) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container Registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Details From config.yml
id: extract_from_config_yaml
run: |
echo "desired_version=$(cat ${{ github.workspace }}/${{ matrix.changed_dir }} | yq '.version')" >> "$GITHUB_OUTPUT"
echo "platforms=$(echo ${{ github.workspace }}/${{ matrix.changed_dir }} | yq '.platforms | join(",")')" >> "$GITHUB_OUTPUT"
- name: Check Image to Build Does Not Already Exist
run: |
if docker manifest inspect ${{ env.REGISTRY }}/${{ matrix.changed_dir }}:${{ steps.extract_from_config_yaml.outputs.desired_version }} > /dev/null; then
echo "The tag "${{ env.REGISTRY }}/${{ matrix.changed_dir }}:${{ steps.extract_from_config_yaml.outputs.desired_version }}" already exists in the repository. Do you need to bump the version in the config.yml?"
exit 1
fi
- name: Calculate Version
id: calculate_version
run: |
VERSION=${{ github.event_name == 'pull_request' && format('{0}-PR{1}.{2}', steps.extract_from_config_yaml.outputs.desired_version, github.event.number, github.event.pull_request.head.sha) || steps.extract_from_config_yaml.outputs.desired_version}}
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get Docker Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ matrix.changed_dir }}
tags: |
type=semver,pattern={{version}},value=v${{ steps.calculate_version.outputs.version }}
type=sha
- name: Build and Push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: ${{ github.workspace }}/${{ matrix.changed_dir }}
file: ${{ github.workspace }}/${{ matrix.changed_dir }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ steps.extract_from_config_yaml.outputs.platforms }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ matrix.changed_dir }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
- name: Create Git Tag
if: ${{ github.event_name != 'pull_request' }}
uses: mathieudutour/github-tag-action@v6.2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: false
default_prerelease_bump: false
custom_tag: ${{ matrix.changed_dir }}/v${{ steps.calculate_version.outputs.version }}
- name: Create GitHub Releas
if: ${{ github.event_name != 'pull_request' }}
uses: comnoco/create-release-action@v2.0.5
with:
tag_name: ${{ matrix.changed_dir }}/v${{ steps.calculate_version.outputs.version }}
release_name: ${{ matrix.changed_dir }} - v${{ steps.calculate_version.outputs.version }}