-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (124 loc) · 4.66 KB
/
Copy pathpublish-docker.yml
File metadata and controls
137 lines (124 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: publish-docker
on:
push:
branches:
- main
tags:
- "v*.*.*"
workflow_dispatch:
inputs:
docker_tag:
description: "Optional explicit Docker tag (example: v1.1.1)"
required: false
type: string
jobs:
docker:
name: build-and-push-docker
runs-on: ubuntu-latest
if: >-
${{
github.event_name == 'workflow_dispatch' ||
startsWith(github.ref, 'refs/tags/v') ||
(github.event_name == 'push' &&
github.ref == 'refs/heads/main' &&
(contains(github.event.head_commit.message || '', '[publish-docker]') ||
contains(github.event.head_commit.message || '', '[docker]')))
}}
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Resolve Docker image tag
id: version
env:
DOCKERHUB_NAMESPACE: ecrum19
DOCKERHUB_REPO: vcf-rdfizer
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_REF: ${{ github.ref }}
GITHUB_REF_NAME: ${{ github.ref_name }}
HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message || '' }}
INPUT_DOCKER_TAG: ${{ github.event.inputs.docker_tag || '' }}
run: |
python - <<'PY'
import json
import os
import re
import subprocess
import urllib.request
namespace = os.environ["DOCKERHUB_NAMESPACE"]
repo = os.environ["DOCKERHUB_REPO"]
event_name = os.environ.get("GITHUB_EVENT_NAME", "")
github_ref = os.environ.get("GITHUB_REF", "")
ref_name = os.environ.get("GITHUB_REF_NAME", "")
head_message = os.environ.get("HEAD_COMMIT_MESSAGE", "")
manual_tag = os.environ.get("INPUT_DOCKER_TAG", "").strip()
semver = re.compile(r"^v(\d+)\.(\d+)\.(\d+)$")
explicit_from_commit = re.search(r"\[(?:publish-docker|docker)=([A-Za-z0-9_.-]+)\]", head_message)
def parse_max(tags):
best = (-1, -1, -1)
for tag in tags:
m = semver.match(tag)
if m:
version = tuple(int(part) for part in m.groups())
if version > best:
best = version
return best
resolved_tag = ""
if manual_tag:
resolved_tag = manual_tag
elif event_name == "push" and github_ref.startswith("refs/tags/"):
resolved_tag = ref_name
elif explicit_from_commit:
resolved_tag = explicit_from_commit.group(1)
else:
best = (-1, -1, -1)
try:
url = f"https://hub.docker.com/v2/repositories/{namespace}/{repo}/tags?page_size=100"
while url:
with urllib.request.urlopen(url, timeout=20) as resp:
payload = json.load(resp)
best = max(best, parse_max([item.get("name", "") for item in payload.get("results", [])]))
url = payload.get("next")
except Exception:
tags = subprocess.check_output(["git", "tag"], text=True).splitlines()
best = max(best, parse_max(tags))
major, minor, patch = best
if major < 0:
resolved_tag = "v1.0.0"
else:
resolved_tag = f"v{major}.{minor}.{patch + 1}"
if not re.match(r"^[A-Za-z0-9_.-]+$", resolved_tag):
raise SystemExit(f"Invalid Docker tag: {resolved_tag}")
print(f"Resolved Docker tag: {resolved_tag}")
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f:
f.write(f"next_version={resolved_tag}\n")
PY
- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ecrum19/vcf-rdfizer
tags: |
type=raw,value=${{ steps.version.outputs.next_version }}
type=raw,value=latest
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}