Skip to content

Commit

Permalink
* Github Actions: generating package version and
Browse files Browse the repository at this point in the history
  Docker image metadata has been moved to separate jobs,
  since those don't need to be matrix ones
  • Loading branch information
hostcc committed Oct 7, 2024
1 parent e0c748a commit 14ee250
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ on:
- master

jobs:
version:
name: Generate package version
runs-on: ubuntu-latest
outputs:
value: ${{ steps.package-version.outputs.value }}
steps:
- name: Checkout the code
uses: actions/checkout@v4
- name: Set Python up
uses: actions/setup-python@v5
- name: Install dependencies
run: >-
python -m pip install --upgrade setuptools_scm
- name: Determine package version
id: package-version
run: |
package_version=`python -m setuptools_scm --format plain`
echo "value=$package_version" >> $GITHUB_OUTPUT
tests:
name: Tests
strategy:
Expand All @@ -33,8 +52,7 @@ jobs:
python: '3.12'
toxenv: py
runs-on: ${{ matrix.os }}
outputs:
version: ${{ steps.package-version.outputs.VALUE }}
needs: [version]
steps:
- name: Checkout the code
uses: actions/checkout@v4
Expand All @@ -54,11 +72,6 @@ jobs:
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:
Expand All @@ -69,9 +82,35 @@ jobs:
args: >-
-Dsonar.projectKey=${{ github.repository_owner }}_${{ github.event.repository.name }}
-Dsonar.organization=${{ github.repository_owner }}
-Dsonar.projectVersion=${{ steps.package-version.outputs.VALUE }}
-Dsonar.projectVersion=${{ needs.version.outputs.value }}
# yamllint enable rule:line-length

docker-metadata:
name: Generata metadata for container images
runs-on: ubuntu-latest
needs: [version]
outputs:
version: ${{ steps.meta.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
annotations_json: ${{ toJson(fromJson(steps.meta.outputs.json).annotations) }}
tags_json: ${{ toJson(fromJson(steps.meta.outputs.json).tags) }}
steps:
- name: Prepare Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=pep440,pattern={{raw}},value=${{ needs.version.outputs.value }}
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
docker-publish:
name: Build and publish Docker images
strategy:
Expand All @@ -87,7 +126,7 @@ jobs:
- platform_id: linux/amd64
platform_name: linux-amd64
runs-on: ubuntu-latest
needs: [tests]
needs: [version, tests, docker-metadata]
permissions:
contents: read
packages: write
Expand All @@ -101,22 +140,6 @@ jobs:
- 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 }}
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:
Expand All @@ -133,11 +156,11 @@ jobs:
# See https://github.com/docker/build-push-action/issues/286 for more
# details
platforms: ${{ matrix.platform_id }}
labels: ${{ steps.meta.outputs.labels }}
labels: ${{ needs.docker-metadata.outputs.labels }}
# 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 }}
VERSION=${{ needs.version.outputs.value }}
# Push by digest only, manifest will be added later
outputs: >-
type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=true
Expand All @@ -157,15 +180,12 @@ jobs:
matrix-step-name: ${{ github.job }}
matrix-key: ${{ matrix.platform_name }}
outputs: |-
image_version: ${{ steps.meta.outputs.version }}
image_annotations: ${{ toJson(fromJson(steps.meta.outputs.json).annotations) }}
image_tags: ${{ toJson(fromJson(steps.meta.outputs.json).tags) }}
image_digest: ${{ steps.build.outputs.digest }}
docker-manifest:
name: Create and push Docker manifest
runs-on: ubuntu-latest
needs: [docker-publish]
needs: [docker-metadata, docker-publish]
steps:
- name: Read image information from publish job
uses: GoCodeAlone/github-action-matrix-outputs-read@v1
Expand All @@ -179,14 +199,14 @@ jobs:
- name: Create and push Docker manifest
run: >-
docker buildx imagetools create
--annotation ${{ fromJson(fromJson(steps.read.outputs.result).image_annotations)[0] }}
${{ join(fromJson(fromJson(steps.read.outputs.result).image_tags)[0], '--tags') }}
${{ join(fromJson(fromJson(steps.read.outputs.result).image_digest), ' ') }}
${{ join(fromJson(needs.docker-metadata.outputs.annotations_json), '--annotation ') }}
${{ join(fromJson(needs.docker-metadata.outputs.tags_json), '--tags ') }}
${{ join(fromJson(fromJson(steps.read.outputs.result).image_digest.*), ' ') }}
docker-test:
name: Test Docker images
runs-on: ubuntu-latest
needs: [docker-manifest]
needs: [docker-metadata, docker-manifest]
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -215,5 +235,5 @@ jobs:
run: >-
docker run --rm
--platform ${{ matrix.platform_id }}
ghcr.io/${{ github.repository }}:${{ fromJson(steps.read.outputs.result).image_version[0] }}
ghcr.io/${{ github.repository }}:${{ needs.docker-metadata.outputs.version }}
--help

0 comments on commit 14ee250

Please sign in to comment.