fix: Properly collect Python dependencies during image build. Next attempt at build cache #240
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: main | |
on: | |
pull_request: | |
release: | |
types: [published] | |
push: | |
branches: | |
- main | |
- master | |
jobs: | |
tests: | |
name: Tests | |
strategy: | |
fail-fast: false | |
matrix: | |
include: | |
- os: ubuntu-latest | |
python: 3.8 | |
toxenv: py | |
- os: ubuntu-latest | |
python: 3.9 | |
toxenv: py | |
- os: ubuntu-latest | |
python: '3.10' | |
toxenv: py | |
- os: ubuntu-latest | |
python: '3.11' | |
toxenv: py | |
- os: ubuntu-latest | |
python: '3.12' | |
toxenv: py | |
runs-on: ${{ matrix.os }} | |
outputs: | |
version: ${{ steps.package-version.outputs.VALUE }} | |
steps: | |
- name: Checkout the code | |
uses: actions/checkout@v4 | |
with: | |
# Disable shallow clone for Sonar scanner, as it needs access to the | |
# history | |
fetch-depth: 0 | |
- name: Set Python up | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ matrix.python }} | |
- name: Install testing tools | |
run: >- | |
python -m pip install --upgrade \ | |
setuptools setuptools_scm pip tox virtualenv coverage | |
- name: Run the tests | |
run: tox -e ${{ matrix.toxenv }} | |
- name: Generage Coverage combined XML report | |
run: coverage xml | |
- name: Determine package version | |
id: package-version | |
run: | | |
package_version=`python -m setuptools_scm --format plain` | |
echo "VALUE=$package_version" >> $GITHUB_OUTPUT | |
- name: SonarCloud scanning | |
uses: sonarsource/sonarcloud-github-action@master | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
with: | |
# yamllint disable rule:line-length | |
args: >- | |
-Dsonar.projectKey=${{ github.repository_owner }}_${{ github.event.repository.name }} | |
-Dsonar.organization=${{ github.repository_owner }} | |
-Dsonar.projectVersion=${{ steps.package-version.outputs.VALUE }} | |
# yamllint enable rule:line-length | |
docker-publish: | |
name: Build and publish Docker images | |
strategy: | |
fail-fast: false | |
matrix: | |
include: | |
- platform_id: linux/arm/v7 | |
platform_name: linux-arm-v7 | |
- platform_id: linux/arm/v6 | |
platform_name: linux-arm-v6 | |
- platform_id: linux/arm64 | |
platform_name: linux-arm64 | |
- platform_id: linux/amd64 | |
platform_name: linux-amd64 | |
runs-on: ubuntu-latest | |
needs: [tests] | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout the code | |
uses: actions/checkout@v4 | |
- name: Set up QEMU for more platforms supported by Buildx | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Prepare Docker metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ghcr.io/${{ github.repository }} | |
flavor: | | |
suffix=-${{ matrix.platform_id }} | |
tags: | | |
type=pep440,pattern={{raw}},value=${{ needs.tests.outputs.version }} | |
type=raw,value=latest,enable=${{ | |
github.event_name == 'release' | |
&& github.event.action == 'published' | |
&& (github.event.release.target_commitish == 'main' | |
|| github.event.release.target_commitish == 'master') | |
}} | |
type=ref,event=pr | |
type=edge | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push images | |
uses: docker/build-push-action@v6 | |
id: build | |
with: | |
# No explicit context used, since that makes cache misses most of the | |
# time. | |
# See https://github.com/docker/build-push-action/issues/286 for more | |
# details | |
platforms: ${{ matrix.platform_id }} | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
annotations: ${{ steps.meta.outputs.annotations }} | |
# Implicit context points to working copy, not Git respository, so | |
# `setuptools_scm` needs to receive the version explicitly | |
build-args: | | |
VERSION=${{ needs.tests.outputs.version }} | |
# Cache the buildx cache between builds using GitHub registry. `gha` | |
# cache has been mentioned to introduce cache misses for | |
# multi-platform builds, see https://github.com/docker/buildx/discussions/1382 | |
# for potential hints | |
cache-from: | | |
type=registry,ref=ghcr.io/${{ github.repository }}/buildcache:${{ matrix.platform_name }} | |
cache-to: | | |
type=registry,ref=ghcr.io/${{ github.repository }}/buildcache:${{ matrix.platform_name }},mode=max | |
- name: Store image information | |
uses: GoCodeAlone/github-action-matrix-outputs-write@v1 | |
id: out | |
with: | |
matrix-step-name: ${{ github.job }} | |
matrix-key: ${{ matrix.platform_name }} | |
outputs: |- | |
image_version: ${{ steps.meta.outputs.version }} | |
image_digest: ${{ steps.build.outputs.digest }} | |
image_id: ${{ steps.build.outputs.imageid }} | |
docker-test: | |
name: Test Docker images | |
runs-on: ubuntu-latest | |
needs: [docker-publish] | |
strategy: | |
fail-fast: false | |
matrix: | |
include: | |
- platform_id: linux/arm/v7 | |
platform_name: linux-arm-v7 | |
- platform_id: linux/arm/v6 | |
platform_name: linux-arm-v6 | |
- platform_id: linux/arm64 | |
platform_name: linux-arm64 | |
- platform_id: linux/amd64 | |
platform_name: linux-amd64 | |
steps: | |
- name: Read image information from publish job | |
uses: GoCodeAlone/github-action-matrix-outputs-read@v1 | |
id: read | |
with: | |
matrix-step-name: docker-publish | |
- name: Set up QEMU for more platforms supported by Buildx | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: ${{ matrix.platform_id }} | |
- name: Test images | |
run: >- | |
docker run --rm | |
--platform ${{ matrix.platform_id }} | |
ghcr.io/${{ github.repository }}:${{ fromJson(steps.read.outputs.result).image_version[matrix.platform_name] }} | |
--help |