-
Notifications
You must be signed in to change notification settings - Fork 4
111 lines (95 loc) · 3.36 KB
/
Copy pathrelease.yml
File metadata and controls
111 lines (95 loc) · 3.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
name: release
on:
push:
branches: [main]
paths:
- "pyproject.toml"
- "CHANGELOG.md"
workflow_dispatch:
inputs:
dry_run:
description: "Only print the release notes; do not create the tag or the release"
type: boolean
default: false
permissions: {}
concurrency:
group: release
cancel-in-progress: false
jobs:
release:
name: "Tag and Publish Release"
if: github.repository == 'marcieltorres/safe-chat-slack-bot'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Check out repository code
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Read the version from pyproject.toml
id: version
run: |
python3 - <<'PY' >> "$GITHUB_OUTPUT"
import re
import tomllib
with open("pyproject.toml", "rb") as handler:
version = tomllib.load(handler)["tool"]["poetry"]["version"]
if not re.fullmatch(r"\d+\.\d+\.\d+", version):
raise SystemExit(f"::error::invalid version in pyproject.toml: {version!r}")
print(f"version={version}")
PY
- name: Check whether the tag already exists
id: tag
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
if git rev-parse -q --verify "refs/tags/${VERSION}" > /dev/null; then
echo "exists=true" >> "$GITHUB_OUTPUT"
echo "::notice::tag ${VERSION} already exists, nothing to release"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Extract the release notes from CHANGELOG.md
if: steps.tag.outputs.exists == 'false'
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
python3 - <<'PY'
import os
import pathlib
import re
version = os.environ["VERSION"]
changelog = pathlib.Path("CHANGELOG.md").read_text(encoding="utf-8")
section = re.search(
rf"^## \[{re.escape(version)}\].*?$\n(?P<body>.*?)(?=^## \[|^\[[^\]]+\]:|\Z)",
changelog,
re.MULTILINE | re.DOTALL,
)
if section is None:
raise SystemExit(f"::error::CHANGELOG.md has no '## [{version}]' section")
body = section.group("body").strip()
if not body:
raise SystemExit(f"::error::the '## [{version}]' section is empty")
pathlib.Path("release-notes.md").write_text(f"{body}\n", encoding="utf-8")
PY
cat release-notes.md
- name: Create and push the tag
if: steps.tag.outputs.exists == 'false' && !inputs.dry_run
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "${VERSION}" -m "${VERSION}"
git push origin "${VERSION}"
- name: Publish the GitHub Release
if: steps.tag.outputs.exists == 'false' && !inputs.dry_run
env:
VERSION: ${{ steps.version.outputs.version }}
GH_TOKEN: ${{ github.token }}
run: |
gh release create "${VERSION}" \
--title "${VERSION}" \
--notes-file release-notes.md \
--verify-tag