images workflow (iterative/cml)
The images workflow from iterative/cml, explained and optimized by Latchkey.
CI health: B - good
Point runs-on at Latchkey and get job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the images workflow from the iterative/cml repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
on:
workflow_call:
inputs:
release:
required: true
type: boolean
version:
required: false
type: string
jobs:
images:
runs-on: ubuntu-latest
strategy:
matrix:
dvc: [1, 2, 3]
base: [0, 1]
gpu: [false, true]
include:
- base: 0
ubuntu: 18.04
python: 2.7
cuda: 11.2.2
cudnn: 8
- base: 1
ubuntu: 20.04
python: 3.8
cuda: 11.2.2
cudnn: 8
- latest: true # update the values below after introducing a new major version
base: 1
dvc: 3
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha || github.ref }}
fetch-depth: 0
- name: Metadata
id: metadata
env:
CML_VERSION: ${{ inputs.version }}
run: |
latest_tag=$(git describe --tags | cut -d- -f1)
test -n "$CML_VERSION" && latest_tag="$CML_VERSION"
cml_version=${latest_tag##v}
dvc_version=$(python3 -c '
from distutils.version import StrictVersion as Ver
from urllib.request import urlopen
from json import load
data = load(urlopen("https://pypi.org/pypi/dvc/json"))
ver_pre = "${{ matrix.dvc }}".rstrip(".") + "."
print(
max(
(i.strip() for i in data["releases"] if i.startswith(ver_pre)),
default="${{ matrix.dvc }}",
key=Ver
)
)')
echo cache_tag=${cml_version}-${dvc_version}-${{ matrix.base }}-${{ matrix.gpu }} >> $GITHUB_OUTPUT
echo cml_version=$cml_version >> $GITHUB_OUTPUT
tag=${cml_version//.*/}-dvc${{ matrix.dvc }}-base${{ matrix.base }}
if [[ ${{ matrix.gpu }} == true ]]; then
echo base=nvidia/cuda:${{ matrix.cuda }}-cudnn${{ matrix.cudnn }}-runtime-ubuntu${{ matrix.ubuntu }} >> $GITHUB_OUTPUT
tag=${tag}-gpu
else
echo base=ubuntu:${{ matrix.ubuntu }} >> $GITHUB_OUTPUT
fi
TAGS="$(
for registry in docker.io/{dvcorg,iterativeai} ghcr.io/iterative; do
if [[ "${{ matrix.latest }}" == "true" ]]; then
if [[ "${{ matrix.gpu }}" == "true" ]]; then
echo "${registry}/cml:latest-gpu"
else
echo "${registry}/cml:latest"
fi
fi
echo "${registry}/cml:${tag}"
done | head -c-1
)"
# https://github.com/orgs/community/discussions/26288#discussioncomment-3876281
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
delim="$(openssl rand -hex 8)"
echo "tags<<${delim}" >> $GITHUB_OUTPUT
echo "${TAGS}" >> $GITHUB_OUTPUT
echo "${delim}" >> $GITHUB_OUTPUT
- uses: docker/setup-buildx-action@v2
- uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key:
${{ runner.os }}-buildx-${{ steps.metadata.outputs.cache_tag }}-${{
github.sha }}
restore-keys:
${{ runner.os }}-buildx-${{ steps.metadata.outputs.cache_tag }}-
- uses: docker/login-action@v2
with:
registry: docker.io
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- uses: docker/build-push-action@v3
with:
push:
${{ inputs.release || github.event_name == 'push' ||
github.event_name == 'schedule' || github.event_name ==
'workflow_dispatch' }}
context: ./
file: ./Dockerfile
tags: |
${{ steps.metadata.outputs.tags }}
build-args: |
CML_VERSION=${{ steps.metadata.outputs.cml_version }}
DVC_VERSION=${{ matrix.dvc }}
PYTHON_VERSION=${{ matrix.python }}
BASE_IMAGE=${{ steps.metadata.outputs.base }}
pull: true
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Move cache
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
on: workflow_call: inputs: release: required: true type: boolean version: required: false type: string jobs: images: timeout-minutes: 30 runs-on: latchkey-small strategy: matrix: dvc: [1, 2, 3] base: [0, 1] gpu: [false, true] include: - base: 0 ubuntu: 18.04 python: 2.7 cuda: 11.2.2 cudnn: 8 - base: 1 ubuntu: 20.04 python: 3.8 cuda: 11.2.2 cudnn: 8 - latest: true # update the values below after introducing a new major version base: 1 dvc: 3 steps: - uses: actions/checkout@v3 with: ref: ${{ github.event.pull_request.head.sha || github.ref }} fetch-depth: 0 - name: Metadata id: metadata env: CML_VERSION: ${{ inputs.version }} run: | latest_tag=$(git describe --tags | cut -d- -f1) test -n "$CML_VERSION" && latest_tag="$CML_VERSION" cml_version=${latest_tag##v} dvc_version=$(python3 -c ' from distutils.version import StrictVersion as Ver from urllib.request import urlopen from json import load data = load(urlopen("https://pypi.org/pypi/dvc/json")) ver_pre = "${{ matrix.dvc }}".rstrip(".") + "." print( max( (i.strip() for i in data["releases"] if i.startswith(ver_pre)), default="${{ matrix.dvc }}", key=Ver ) )') echo cache_tag=${cml_version}-${dvc_version}-${{ matrix.base }}-${{ matrix.gpu }} >> $GITHUB_OUTPUT echo cml_version=$cml_version >> $GITHUB_OUTPUT tag=${cml_version//.*/}-dvc${{ matrix.dvc }}-base${{ matrix.base }} if [[ ${{ matrix.gpu }} == true ]]; then echo base=nvidia/cuda:${{ matrix.cuda }}-cudnn${{ matrix.cudnn }}-runtime-ubuntu${{ matrix.ubuntu }} >> $GITHUB_OUTPUT tag=${tag}-gpu else echo base=ubuntu:${{ matrix.ubuntu }} >> $GITHUB_OUTPUT fi TAGS="$( for registry in docker.io/{dvcorg,iterativeai} ghcr.io/iterative; do if [[ "${{ matrix.latest }}" == "true" ]]; then if [[ "${{ matrix.gpu }}" == "true" ]]; then echo "${registry}/cml:latest-gpu" else echo "${registry}/cml:latest" fi fi echo "${registry}/cml:${tag}" done | head -c-1 )" # https://github.com/orgs/community/discussions/26288#discussioncomment-3876281 # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings delim="$(openssl rand -hex 8)" echo "tags<<${delim}" >> $GITHUB_OUTPUT echo "${TAGS}" >> $GITHUB_OUTPUT echo "${delim}" >> $GITHUB_OUTPUT - uses: docker/setup-buildx-action@v2 - uses: actions/cache@v3 with: path: /tmp/.buildx-cache key: ${{ runner.os }}-buildx-${{ steps.metadata.outputs.cache_tag }}-${{ github.sha }} restore-keys: ${{ runner.os }}-buildx-${{ steps.metadata.outputs.cache_tag }}- - uses: docker/login-action@v2 with: registry: docker.io username: ${{ vars.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_PASSWORD }} - uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ github.token }} - uses: docker/build-push-action@v3 with: push: ${{ inputs.release || github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }} context: ./ file: ./Dockerfile tags: | ${{ steps.metadata.outputs.tags }} build-args: | CML_VERSION=${{ steps.metadata.outputs.cml_version }} DVC_VERSION=${{ matrix.dvc }} PYTHON_VERSION=${{ matrix.python }} BASE_IMAGE=${{ steps.metadata.outputs.base }} pull: true cache-from: type=local,src=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache-new - name: Move cache # https://github.com/docker/build-push-action/issues/252 # https://github.com/moby/buildkit/issues/1896 run: | rm -rf /tmp/.buildx-cache mv /tmp/.buildx-cache-new /tmp/.buildx-cache
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Add a job timeout so a hung step cannot burn hours of runner time.
3 third-party actions are referenced by a movable tag. Pin them to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.
What Latchkey heals here
This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:
- Container pulls and builds
This workflow runs 1 job (12 with the matrix expanded) per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.