Skip to content

Update README.md

Update README.md #97

---
name: Build and Deploy
on:
push:
branches:
- main
jobs:
define_images:
# Define a build matrix based on all changed *.hcl files so we can run packer only for image definitions that have actually changed
# So if e.g. ubuntu/focal/variables.auto.pkrvars.hcl or ubuntu/focal/src-ubuntu.pkr.hcl changes, add "ubuntu/focal" to the matrix
# Also ensure that if an image template (for instance, the general template for ubuntu images 'ubuntu/src-ubuntu.pkr.hcl') changes,
# an entry is added to the matrix for each image definition that relies on (i.e. symlinks) it.
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: "${{ github.repository }}"
- name: Get changed image definitions
# Find changed .hcl files in all <os>/<version> directories
id: changed-images-yaml
uses: tj-actions/changed-files@v41
with:
path: "${{ github.repository }}"
dir_names: true
dir_names_max_depth: 3
quotepath: false
files_yaml: |
images:
- '*/*/*.hcl'
- name: Get changed image definition templates
# Find all changed base templates in all <os>/ directories
id: changed-symlink-yaml
uses: tj-actions/changed-files@v41
with:
path: "${{ github.repository }}"
quotepath: false
files_yaml: |
symlinks:
- '*/*.hcl'
- name: Get image directories based on changed symlinks
# For every changed template in an <os> directory, collect all <os>/<version> directories that contain a symlink to the template.
# For instance, if ubuntu/src-ubunut.pkr.hcl changes, find all subdirectories of 'ubuntu' that contain links to that file
# -> the result would be e.g. 'ubuntu/focal ubuntu/jammy ubuntu/focal_desktop'
id: get-symlink-dirs
working-directory: "${{ github.repository }}"
run: |
# Loop over each changed <os>/*.hcl file
for file in ${{ steps.changed-symlink-yaml.outputs.symlinks_all_changed_files }}; do
dir=$(dirname $file)
links=$(find "$dir" -type l) # Find all symlinks under the current $file's directory
for link in $links; do
# If this link has $file as a target, add the link's directory to the array of changed template directories
if [[ $(readlink -f "$link") == $(realpath "$file") ]]; then
CHANGED="$CHANGED $(dirname $link)"
fi
done
done
echo "Debug: " $CHANGED
# Store the results
echo changed_dirs="$CHANGED" >> "$GITHUB_OUTPUT"
- id: set-images-matrix
run: |
# First add together the directories found in the changed-images-yaml and changed-templates-yaml steps into a single array
ALL_DIRS="${{ steps.changed-images-yaml.outputs.images_all_changed_files }} ${{ steps.get-symlink-dirs.outputs.changed_dirs }}"
# Now construct a unique array out of ALL_DIRS
IMGS=($(echo "$ALL_DIRS" | xargs -n 1 | sort -u))
# Next create a valid json array out of the bash array using jq
IMGS=$(printf '%s\n' "${MATRIX[@]}" | jq -R . | jq -c -s .)
# Store the results
echo images=$IMGS >> $GITHUB_OUTPUT
# Create a matrix variable containing the images array and update_base_component array
echo matrix={\"update_base_components\": [true, false], \"images\": $IMGS} >> $GITHUB_OUTPUT
outputs:
images: ${{ steps.set-images-matrix.outputs.images }}
images_matrix: ${{ steps.set-images-matrix.outputs.matrix }}
build_and_deploy:
needs: define_images
runs-on: ubuntu-latest
if: ${{ needs.define_images.outputs.images != '[""]' && needs.define_images.outputs.images_matrix != '' }}
env:
ANSIBLE_VERSION: 2.15.5
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.define_images.outputs.images_matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Install specific ansible-core version
run: pip install ansible-core==${{ env.ANSIBLE_VERSION }}
- name: Install ansible dependencies
run: ansible-galaxy collection install -r requirements.yml
- name: Debug ansible version
run: ansible-playbook --version
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build image
id: build-image
run: IMG=${{ matrix.images }} UPDATE_BASE_COMPONENTS=${{ matrix.update_base_components }} ./pack.sh docker
- name: Get image name and tag # grep in the image directory's variables file to find the image name/tag to push to
id: get-image-name
run: |
echo "docker_repo=`grep docker_repo ${{ matrix.images }}/variables.auto.pkrvars.hcl | awk '{print $3}'`" >> "$GITHUB_OUTPUT"
- name: Push image
run: docker push ${{ steps.get-image-name.outputs.docker_repo }}${{ matrix.update_base_components && '-pilot' || '' }}