Build Containers #127
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 Containers | |
on: | |
pull_request: | |
paths: | |
- devops/docker/**/Dockerfile | |
push: | |
branches: | |
- main | |
paths: | |
- devops/docker/**/Dockerfile | |
workflow_dispatch: # TODO: Remove before checkin | |
env: | |
REGISTRY: ghcr.io | |
BUILD_TYPE: Release | |
MOUNT: /azure-osconfig | |
jobs: | |
modified-containers: | |
name: Modified containers | |
runs-on: ubuntu-latest | |
outputs: | |
containers: ${{ steps.matrix.outputs.containers }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Get changed containers | |
id: changed-containers | |
uses: tj-actions/[email protected] | |
with: | |
files: ./devops/docker/**/Dockerfile | |
- name: Create build matrix | |
id: matrix | |
run: | | |
# If this workflow is present in other_changed_files, then rebuild all containers | |
if [[ "${{ steps.changed-containers.outputs.other_changed_files }}" == *".github/workflows/build-containers.yml"* ]]; then | |
echo "Workflow file changed, adding all containers to build matrix..." | |
containers=$(ls -d devops/docker/**/Dockerfile | jq -R . | jq -s .) | |
else | |
containers=$(echo -n ${{ steps.changed-containers.outputs.all_changed_files }} | jq -R -s -c 'split(" ")') | |
fi | |
echo Containers to build: $containers | |
echo containers=$containers >> $GITHUB_OUTPUT | |
docker-build: | |
name: Docker | |
needs: modified-containers | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
strategy: | |
fail-fast: false | |
matrix: | |
container: ${{ fromJson(needs.modified-containers.outputs.containers) }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Setup image | |
id: image | |
run: | | |
dockerfile=${{ matrix.container }} | |
image=${dockerfile#devops/docker/} | |
image=${image%/*} | |
distro=${image%-*} | |
repo=$(echo ${{ github.repository }} | awk '{print tolower($0)}') | |
echo name=$(echo -n $repo/$image) >> $GITHUB_OUTPUT | |
echo path=$(dirname ${dockerfile}) >> $GITHUB_OUTPUT | |
echo distro=$(echo -n $distro) >> $GITHUB_OUTPUT | |
- name: Setup QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Setup Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Docker login (azurecr.io) | |
if: false | |
uses: docker/login-action@v2 | |
with: | |
registry: osconfig.azurecr.io | |
username: ${{ secrets.ACR_CLIENT_ID }} | |
password: ${{ secrets.ACR_CLIENT_SECRET }} | |
- name: Docker login (ghcr.io) | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ github.token }} | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }} | |
tags: | | |
type=raw,value=latest | |
type=raw,value=${{ github.run_number }} | |
type=sha | |
- name: Build and push | |
uses: docker/build-push-action@v3 | |
with: | |
context: ${{ steps.image.outputs.path }} | |
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
- name: Push image to ACR | |
# Only push the image when a pull request is merged to main | |
# if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} | |
# if: false | |
run: | | |
docker buildx imagetools create \ | |
--tag osconfig.azurecr.io/${{ steps.image.outputs.name }}:latest \ | |
--tag osconfig.azurecr.io/${{ steps.image.outputs.name }}:${{ github.run_number }} \ | |
--tag osconfig.azurecr.io/${{ steps.image.outputs.name }}:${{ github.sha }} \ | |
ghcr.io/${{ steps.image.outputs.name }}:latest |