-
Notifications
You must be signed in to change notification settings - Fork 0
132 lines (114 loc) · 4.5 KB
/
bump-version.yaml
File metadata and controls
132 lines (114 loc) · 4.5 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
# SPDX-License-Identifier: Apache-2.0
name: Bump Version on PR from staging to main
on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
branches:
- main
workflow_dispatch:
inputs:
branch:
description: PR head branch to bump (default staging)
required: true
default: staging
permissions:
contents: write
jobs:
bump-version:
# Fire for staging->main PRs from same repo, or manual runs
if: >-
(github.event_name == 'pull_request' &&
github.base_ref == 'main' &&
github.head_ref == 'staging' &&
github.event.pull_request.head.repo.full_name == github.repository)
|| github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Determine branches
id: vars
shell: bash
env:
EVENT_NAME: ${{ github.event_name }}
HEAD_REF: ${{ github.head_ref }}
BASE_REF: ${{ github.base_ref }}
INPUT_BRANCH: ${{ inputs.branch }}
run: |
if [[ "$EVENT_NAME" == "pull_request" ]]; then
{ echo "head=$HEAD_REF"; echo "base=$BASE_REF"; } >> "$GITHUB_OUTPUT"
else
{ echo "head=$INPUT_BRANCH"; echo "base=main"; } >> "$GITHUB_OUTPUT"
fi
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ steps.vars.outputs.head }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
- name: Cache Poetry cache and virtualenv
uses: actions/cache@v4
with:
path: |
~/.cache/pypoetry
key: poetry-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
poetry-${{ runner.os }}-
- name: Fetch base branch
env:
BASE: ${{ steps.vars.outputs.base }}
run: |
git fetch origin "$BASE"
- name: Determine if version bump is needed
id: check
run: |
python - << 'PY'
import os, re, subprocess, sys
def extract_version_from_toml_text(text: str) -> str:
"""Parse tool.poetry.version from a TOML string without extra deps.
Looks for the [tool.poetry] section and returns the first
version = "X.Y.Z" key found before the next section.
"""
in_section = False
for line in text.splitlines():
if re.match(r"\s*\[tool\.poetry\]\s*$", line):
in_section = True
continue
if in_section and re.match(r"\s*\[.*\]\s*$", line):
break
if in_section:
m = re.match(r"\s*version\s*=\s*['\"]([^'\"]+)['\"]", line)
if m:
return m.group(1)
raise RuntimeError("version not found in [tool.poetry] section")
head_toml = open('pyproject.toml', 'r', encoding='utf-8').read()
base_ref = '${{ steps.vars.outputs.base }}'
cp = subprocess.run(['git', 'show', f'origin/{base_ref}:pyproject.toml'], check=True, capture_output=True)
base_toml = cp.stdout.decode('utf-8')
head_ver = extract_version_from_toml_text(head_toml)
base_ver = extract_version_from_toml_text(base_toml)
should_bump = (head_ver == base_ver)
print(f"Head version: {head_ver}; Base version: {base_ver}; Should bump: {should_bump}")
out = open(os.environ['GITHUB_OUTPUT'], 'a')
out.write(f"should_bump={'true' if should_bump else 'false'}\n")
out.close()
PY
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Bump patch version using Poetry (DCO sign-off)
if: steps.check.outputs.should_bump == 'true'
run: |
poetry version patch
git add pyproject.toml
# Add DCO Signed-off-by trailer via -s
git commit -s -m "ci: bump version" || echo "No changes to commit"
git push origin "${{ steps.vars.outputs.head }}"
- name: Skip bump (already bumped)
if: steps.check.outputs.should_bump != 'true'
run: echo "Version already diverged from base; skipping bump."