Skip to content

Commit

Permalink
Tag latest release of Docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
jgosmann committed Feb 17, 2024
1 parent 97127af commit 36defc5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/actions/get-latest-release/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Get latest release
description: Gets the latest (non-prerelease) version of a PyPI package.
inputs:
package:
description: Name of the the package.
required: true
outputs:
latest_version:
description: Latest version of the package.
value: ${{ steps.get-latest.outputs.VERSION }}
runs:
using: composite
steps:
- name: Install packaging
run: pip install packaging requests
shell: bash

- name: Get latest version
id: get-latest
run: ./.github/actions/get-latest-release/get_latest_release.py ${{ inputs.package }} >> "$GITHUB_OUTPUT"
shell: bash
24 changes: 24 additions & 0 deletions .github/actions/get-latest-release/get_latest_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3

import argparse

import requests
from packaging.version import parse as parse_version

if __name__ == "__main__":
arg_parser = argparse.ArgumentParser(
description="Get the latest non-prerelease version of a package according to PEP440."
)
arg_parser.add_argument("package")
args = arg_parser.parse_args()

r = requests.get(
f"https://pypi.org/simple/{args.package}",
headers={"Accept": "application/vnd.pypi.simple.v1+json"},
timeout=30,
)
r.raise_for_status()
versions = [parse_version(v) for v in r.json().get("versions", [])]
latest = max(v for v in versions if not v.is_prerelease)
if latest:
print(f"VERSION={latest}")
28 changes: 27 additions & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,40 @@ jobs:
with:
images: ${{ env.IMAGE_NAME }}

- name: Check if prerelease
id: check-prerelease
uses: ./.github/actions/check-prerelease
with:
version: ${{ steps.version.outputs.version }}

- name: Get latest release
id: latest-version
uses: ./.github/actions/get-latest-release
with:
package: dmarc-metrics-exporter

- name: Set tags
id: set-tags
shell: bash
run: |
{
echo 'TAGS<<EOF'
echo ${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
[ ${{ steps.version.outputs.version }} = ${{ steps.latest-version.outputs.latest_version }} ] \
&& [ ${{ steps.check-prerelease }} != true ] \
&& echo ${{ env.IMAGE_NAME }}:latest
echo EOF
} >> "$GITHUB_OUTPUT"
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
tags: ${{ steps.set-tags.outputs.TAGS }}
platforms: linux/amd64,linux/arm64
labels: ${{ steps.meta.outputs.labels }}
build-args: "version=${{ steps.version.outputs.version }}"

0 comments on commit 36defc5

Please sign in to comment.