.github: Refactor to improve speed #84
Workflow file for this run
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: Test, Build and Deploy Images | |
env: | |
PROJECT: blueos | |
DOCKER: base | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
IMAGE_LIMIT_SIZE_MB: 600 | |
ARTIFACTS_PATH: /tmp/artifacts | |
DIGESTS_PATH: /tmp/digests | |
on: | |
workflow_dispatch: | |
pull_request: | |
push: | |
schedule: | |
# Run every 6 days to keep our caches alive | |
- cron: '0 0 */6 * *' | |
jobs: | |
build: | |
runs-on: ubuntu-22.04 | |
strategy: | |
matrix: | |
platform: | |
- linux/arm/v7 | |
- linux/arm64 | |
- linux/amd64 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Prepare | |
id: prepare | |
run: | | |
echo "buildx_args=VUE_APP_GIT_DESCRIBE=$(git describe --long --always --dirty --all)" >> $GITHUB_OUTPUT | |
echo "DOCKER_IMAGE=${DOCKER_USERNAME:-bluerobotics}/${PROJECT}-${DOCKER}" >> $GITHUB_ENV | |
mkdir -p "${ARTIFACTS_PATH}" | |
mkdir -p "${DIGESTS_PATH}" | |
platform=${{ matrix.platform }} | |
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.DOCKER_IMAGE }} | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: all | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
with: | |
version: latest | |
- name: Cache | |
uses: actions/cache@v4 | |
id: cache | |
with: | |
path: | | |
var-cache-apt | |
var-lib-apt | |
var-ccache | |
key: ${{ env.PLATFORM_PAIR }}-cache-${{ hashFiles('Dockerfile') }} | |
restore-keys: ${{ env.PLATFORM_PAIR }}-cache- | |
- name: Inject cache into Docker | |
uses: reproducible-containers/buildkit-cache-dance@v3 | |
with: | |
cache-map: | | |
{ | |
"var-cache-apt": "/var/cache/apt", | |
"var-lib-apt": "/var/lib/apt", | |
"var-ccache": "/root/.cache" | |
} | |
skip-extraction: ${{ steps.cache.outputs.cache-hit }} | |
- name: Login to DockerHub | |
if: success() && github.event_name != 'pull_request' | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push | |
id: build | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: Dockerfile | |
platforms: ${{ matrix.platform }} | |
labels: ${{ steps.meta.outputs.labels }} | |
push: ${{ github.event_name != 'pull_request' }} | |
outputs: type=docker,dest=${{ env.ARTIFACTS_PATH }}/${{ env.PLATFORM_PAIR }}.tar | |
build-args: ${{ steps.prepare.outputs.buildx_args }} | |
tags: ${{ steps.meta.outputs.tags }} | |
cache-from: type=gha,scope=${{ env.PLATFORM_PAIR }} | |
cache-to: type=gha,scope=${{ env.PLATFORM_PAIR }},mode=max | |
- name: Check size | |
run: | | |
IMAGE=${{ env.ARTIFACTS_PATH }}/${{ env.PLATFORM_PAIR }}.tar | |
IMAGE_SIZE_MB=$(( $(docker inspect ${IMAGE} --format '{{.Size}}')/(2**20) )) | |
echo "Core size is: ${IMAGE_SIZE_MB} MB" | |
echo "Core size limit: ${IMAGE_LIMIT_SIZE_MB} MB" | |
if (( IMAGE_SIZE_MB >= IMAGE_LIMIT_SIZE_MB )); then | |
echo "Image size is larger than the limit" | |
exit 1 | |
fi | |
- name: Export digest | |
run: | | |
digest="${{ steps.build.outputs.digest }}" | |
touch "${{ env.DIGESTS_PATH }}/${digest#sha256:}" | |
- name: Share digest | |
uses: actions/cache/save@v4 | |
id: digest | |
with: | |
path: ${{ env.DIGESTS_PATH }} | |
key: digest-${{ github.sha }} | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.PROJECT }}-${{ env.DOCKER }}-${{ env.PLATFORM_PAIR }} | |
path: ${{ env.ARTIFACTS_PATH }}/*.tar | |
if-no-files-found: error | |
merge: | |
runs-on: ubuntu-22.04 | |
needs: | |
- build | |
steps: | |
- name: Get digests | |
uses: actions/cache/restore@v4 | |
id: digest | |
with: | |
path: ${{ env.DIGESTS_PATH }} | |
key: digest-${{ github.sha }} | |
fail-on-cache-miss: true | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Prepare | |
id: prepare | |
run: echo "DOCKER_IMAGE=${DOCKER_USERNAME:-bluerobotics}/${PROJECT}-${DOCKER}" >> $GITHUB_ENV | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.DOCKER_IMAGE }} | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Create manifest list and push | |
working-directory: ${{ env.DIGESTS_PATH }} | |
run: docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") $(printf '${{ env.DOCKER_IMAGE }}@sha256:%s ' *) | |
- name: Inspect image | |
run: docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.meta.outputs.version }} |