Skip to content

Commit 84a698f

Browse files
authored
Merge pull request #7 from babysnakes/build-docker-images
Create docker package on github
2 parents b860193 + b261e13 commit 84a698f

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

.github/workflows/docker.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
name: Create and publish a Docker image
3+
4+
on:
5+
push:
6+
tags:
7+
- "*"
8+
workflow_dispatch:
9+
10+
env:
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME: ${{ github.repository }}
13+
14+
jobs:
15+
build-and-push-image:
16+
runs-on: ubuntu-latest
17+
permissions:
18+
contents: read
19+
packages: write
20+
#
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
- name: Log in to the Container registry
25+
uses: docker/login-action@v3
26+
with:
27+
registry: ${{ env.REGISTRY }}
28+
username: ${{ github.actor }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
- name: Extract metadata (tags, labels) for Docker
31+
id: meta
32+
uses: docker/metadata-action@v5
33+
with:
34+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
35+
flavor: |
36+
latest=false
37+
prefix=
38+
suffix=
39+
- name: Build and push Docker image
40+
uses: docker/build-push-action@v5
41+
with:
42+
context: .
43+
push: true
44+
tags: ${{ steps.meta.outputs.tags }}
45+
labels: ${{ steps.meta.outputs.labels }}

release.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
5+
DEFAULT_NOTES = 'release-notes.md.tmpl'
6+
7+
8+
class SemanticVersion:
9+
"""
10+
A class that handles semantic version. Just instantiate with valid string version.
11+
"""
12+
13+
def __init__(self, major: int, minor: int, patch: int):
14+
self.major = major
15+
self.minor = minor
16+
self.patch = patch
17+
18+
def __str__(self):
19+
return f"v{self.major}.{self.minor}.{self.patch}"
20+
21+
def bump_major(self):
22+
self.major += 1
23+
24+
def bump_minor(self):
25+
self.minor += 1
26+
27+
def bump_patch(self):
28+
self.patch += 1
29+
30+
@classmethod
31+
def from_string(cls, version: str):
32+
try:
33+
major, minor, patch = map(int, version.lstrip('v').split('.', 2))
34+
return cls(major, minor, patch)
35+
except Exception as err:
36+
raise Exception(f"Error parsing '{version}' as semantic version: {err}")
37+
38+
39+
def extract_tag():
40+
tmp = subprocess.run(["git", "describe", "--tags", "--exact"], capture_output=True)
41+
if tmp.returncode == 0:
42+
raise Exception(f"There is a tag pointing to current commit: {tmp.stdout.decode().rstrip()}")
43+
latest_tag = subprocess.run(["git", "describe", "--tags", "--abbrev=0"], capture_output=True)
44+
return latest_tag.stdout.rstrip().decode()
45+
46+
47+
def mk_release(ver: SemanticVersion, notes_file: str, dry_run: bool):
48+
notes_params = ['-F', notes_file] if notes_file else ['-F', DEFAULT_NOTES]
49+
50+
cmd = ["gh", "release", 'create', '-n', ''] + notes_params + ['-t', f"Release: {ver}", f"{ver}"]
51+
if dry_run:
52+
print(f"DRY RUN: would create release with version: {ver}")
53+
print(f"DRY RUN: would run: {cmd}")
54+
else:
55+
subprocess.run(cmd)
56+
print("")
57+
print('A new release + tag were created on github. Please make sure to pull the changes locally '
58+
'and verify that docker image is building.')
59+
if not notes_file:
60+
print('')
61+
print('No notes file provided. Opening release in web browser to edit...')
62+
input('Print ENTER to exit ')
63+
subprocess.run(['gh', 'release', 'view', '-w', str(ver)])
64+
65+
66+
def run(args):
67+
tag = extract_tag()
68+
ver = SemanticVersion.from_string(tag)
69+
match args.level:
70+
case 'major':
71+
ver.bump_major()
72+
case 'minor':
73+
ver.bump_minor()
74+
case 'patch':
75+
ver.bump_patch()
76+
case _:
77+
raise Exception("No level supplied")
78+
79+
mk_release(ver, args.notes, args.dry)
80+
81+
82+
if __name__ == "__main__":
83+
import sys
84+
import argparse
85+
86+
parser = argparse.ArgumentParser(
87+
prog="Release",
88+
description='Bump version (tag) according to \'level\' and create github release. '
89+
f"Copy and edit '{DEFAULT_NOTES}' to add release notes, otherwise we will open a browser to "
90+
'manually edit.',
91+
epilog="IMPORTANT: This script assumes you have both 'git' and 'gh' "
92+
"configured and running correctly on your machine",
93+
)
94+
parser.add_argument('level', choices=['major', 'minor', 'patch'])
95+
parser.add_argument(
96+
'--dry-run',
97+
help='Do not create release + tag on github, Just print the version that would have been created',
98+
action=argparse.BooleanOptionalAction,
99+
dest='dry',
100+
default=False
101+
)
102+
parser.add_argument(
103+
'-F', '--notes-file',
104+
help='Markdown file containing release notes',
105+
dest='notes'
106+
)
107+
args = parser.parse_args()
108+
try:
109+
run(args)
110+
except Exception as err:
111+
print(err, file=sys.stderr)
112+
sys.exit(1)

0 commit comments

Comments
 (0)