-
Notifications
You must be signed in to change notification settings - Fork 0
133 lines (115 loc) · 4.36 KB
/
release.yml
File metadata and controls
133 lines (115 loc) · 4.36 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
name: release
on:
push:
tags:
- "v*.*.*"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
# https://github.com/actions/checkout
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
# https://github.com/actions/setup-go
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version-file: "go.mod"
# https://github.com/goreleaser/goreleaser-action
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Push semver subtags
shell: python
run: |
#!/usr/bin/env python3
"""
utility for pushing 1- and 2-element semantic version tags from a semver push
"""
# https://github.com/actions/toolkit/blob/master/docs/action-versioning.md#recommendations
import logging
import os
import re
import shlex
import subprocess
import sys
from typing import Final, List
logging.basicConfig(
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z",
level=os.environ.get("LOG_LEVEL", "debug").upper(),
)
# the following regex was tweaked one on from https://semver.org/
semver: Final = re.compile(
r"^"
r"(?:[vVrR])?"
r"(?P<major>0|[1-9]\d*)\."
r"(?P<minor>0|[1-9]\d*)\."
r"(?P<patch>0|[1-9]\d*)"
r"(?:-"
r"(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"
r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*"
r"))?"
r"(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?"
r"$"
)
# https://docs.github.com/en/actions/learn-github-actions/variables
ref_name: Final = os.environ.get("GITHUB_REF_NAME")
actor: Final = os.environ.get("GITHUB_ACTOR")
def run(cmd: List[str]) -> int:
"""run the given command (in list format)"""
logging.info("running command %s", " ".join([shlex.quote(word) for word in cmd]))
try:
result = subprocess.check_call(cmd, shell=False)
except subprocess.CalledProcessError as e:
logging.fatal("failed to run command, error: %s", e)
sys.exit(1)
return result
def gitconfig():
"""
set the identity of the git comitter in the global git config
"""
logging.info("configuring git")
config = {
"user.email": f"{actor}@users.noreply.github.com",
"user.name": actor,
}
for k, v in config.items():
logging.debug("setting git config %s=%s", k, v)
run(["git", "config", "--global", k, v])
def main() -> int:
"""
entrypoint for direct execution; returns an int for use by sys.exit
"""
logging.info(
"%s starting up as unix user %s and actor %s in %s, with ref: %s",
__name__,
os.environ.get("USER", "-unknown-"),
actor,
os.getcwd(),
ref_name,
)
if not ref_name:
logging.fatal("cannot continue without a GITHUB_REF_NAME in the environment")
return 1
if not actor:
logging.fatal("GITHUB_ACTOR is required to be set in the environment")
return 1
gitconfig()
if m := semver.match(ref_name):
major, minor = m.group("major"), m.group("minor")
for tag in (f"v{major}", f"v{major}.{minor}"):
msg = f"[skip ci] Update {tag} tag from {ref_name}"
logging.info("commit: %s", msg)
run(["git", "tag", "-f", "-a", "-m", msg, tag])
run(["git", "push", "-f", "origin", tag])
return 0
return 1
if __name__ == "__main__":
sys.exit(main())