mirror of
https://git.yoctoproject.org/meta-raspberrypi
synced 2026-10-22 18:08:11 +00:00
cancel-redundant-workflows.yml had some overhead as it was spawning an extra workflow_run job per workflow. With concurrency groups, a new push cancels the PR's previous run directly. A cancelled step can leave its docker container running against the shared sstate. Label containers with the runner name and remove them in the cleanup step. Rename docker-prune-images to docker-cleanup accordingly. Signed-off-by: Andrei Gherzan <andrei@gherzan.com>
63 lines
2.2 KiB
YAML
63 lines
2.2 KiB
YAML
# SPDX-FileCopyrightText: Andrei Gherzan <andrei.gherzan@huawei.com>
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
name: "Build a docker image"
|
|
|
|
inputs:
|
|
docker_image:
|
|
required: true
|
|
description: "The name of the docker image"
|
|
|
|
outputs:
|
|
image:
|
|
description: "Name and tag of the docker image to run"
|
|
value: ${{ steps.build.outputs.image }}
|
|
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Build the ${{ inputs.docker_image }} docker image
|
|
id: build
|
|
shell: bash
|
|
# The image is tagged with the git tree hash of the build context, so a
|
|
# tag always maps to the same Dockerfile and scripts. This lets jobs and
|
|
# PRs on the same host share the image.
|
|
run: |
|
|
# Images older than max_age_days are rebuilt so that base image and
|
|
# package updates are picked up.
|
|
# Keep docker-cleanup at twice this.
|
|
max_age_days=7
|
|
tag="$(git rev-parse --short=12 HEAD:.github/workflows/docker-images)"
|
|
image="${{ inputs.docker_image }}:$tag"
|
|
echo "image=$image" >> "$GITHUB_OUTPUT"
|
|
|
|
if created="$(docker image inspect -f '{{.Created}}' "$image" 2>/dev/null)"; then
|
|
age_days=$(( ($(date +%s) - $(date -d "$created" +%s)) / 86400 ))
|
|
if [ "$age_days" -lt "$max_age_days" ]; then
|
|
echo "Reusing $image (built $age_days days ago)."
|
|
exit 0
|
|
fi
|
|
echo "$image is $age_days days old, rebuilding..."
|
|
fi
|
|
|
|
cd .github/workflows/docker-images/
|
|
tries=3
|
|
n=1
|
|
until [ "$n" -gt "$tries" ]; do
|
|
echo "Building the docker image $image... try $n..."
|
|
if docker build --pull --no-cache . -f "${{ inputs.docker_image }}/Dockerfile" \
|
|
--label meta-raspberrypi-ci=1 -t "$image"; then
|
|
# This can fail if a dangling images cleaning job runs in
|
|
# parallel. So we try this a couple of times to minimize
|
|
# conflict. This is because while building, docker creates a
|
|
# untagged image first (dangling) before tagging it at the end.
|
|
# If between these two operations a dangling cleanup happens,
|
|
# build fails.
|
|
break
|
|
fi
|
|
n=$((n+1))
|
|
done
|
|
[ "$n" -le "$tries" ]
|
|
echo "Image $image built."
|