Skip to content

Commit c1c9dee

Browse files
authored
Automatically bump test versions
1 parent 71242aa commit c1c9dee

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Notify code owners about changes to GitHub actions
2-
.github/ @JBWilkie
2+
.github/ @umbertoDifa

.github/workflows/version_bump.yml

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Version Bump
2-
run-name: Version bump ${{ inputs.bump_type }}${{ inputs.test_mode && ' (TEST)' || '' }}
2+
run-name: Version bump ${{ github.event_name == 'workflow_dispatch' && inputs.bump_type || 'patch' }}${{ github.event_name == 'workflow_dispatch' && inputs.test_mode && ' (TEST)' || '' }}
33

44
on:
55
workflow_dispatch:
@@ -18,6 +18,8 @@ on:
1818
required: true
1919
default: false
2020
type: boolean
21+
pull_request:
22+
types: [opened, synchronize, reopened]
2123

2224
permissions:
2325
contents: write
@@ -40,25 +42,48 @@ jobs:
4042
with:
4143
python-version: "3.9"
4244

43-
- name: Configure Git
45+
- name: Install dependencies with retry
46+
uses: nick-invision/retry@7152eba30c6575329ac0576536151aca5a72780e
47+
with:
48+
timeout_minutes: 10
49+
max_attempts: 3
50+
command: |
51+
bash -c "pip install poetry pytest && \
52+
poetry install --no-interaction --no-root -vvv --all-extras && \
53+
poetry install --no-interaction --no-root --all-extras -vvv && \
54+
pip install wheel && \
55+
pip install --upgrade setuptools && \
56+
pip install --editable '.[test,ml,medical,dev, ocv]'"
57+
58+
- name: Set bump type and test mode
4459
run: |
45-
git config user.name github-actions[bot]
46-
git config user.email github-actions[bot]@users.noreply.github.com
60+
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
61+
BUMP_TYPE="${{ inputs.bump_type }}"
62+
TEST_MODE="${{ inputs.test_mode }}"
63+
else
64+
BUMP_TYPE="patch" # Default to "patch" for pull requests
65+
TEST_MODE="true" # Always run in test mode for pull requests
66+
fi
67+
echo "BUMP_TYPE=${BUMP_TYPE}" >> $GITHUB_ENV
68+
echo "TEST_MODE=${TEST_MODE}" >> $GITHUB_ENV
4769
4870
- name: Bump version
4971
run: |
50-
python deploy/increase_version.py --${{ inputs.bump_type }}
51-
BASE_VERSION=$(cat .version)
72+
python deploy/increase_version.py --${{ env.BUMP_TYPE }} --auto-confirm y
73+
BASE_VERSION=$(grep '__version__' darwin/version/__init__.py | cut -d '"' -f 2)
5274
53-
if [[ "${{ inputs.test_mode }}" == "true" ]]; then
75+
if [[ "${{ env.TEST_MODE }}" == "true" ]]; then
5476
TIMESTAMP=$(date +%Y%m%d%H%M%S)
5577
TEST_VERSION="${BASE_VERSION}-test.${TIMESTAMP}"
56-
# Update version in pyproject.toml and __init__.py
57-
sed -i "s/^version = .*/version = \"${TEST_VERSION}\"/" pyproject.toml
58-
sed -i "s/__version__ = .*/__version__ = \"${TEST_VERSION}\"/" darwin/__init__.py
78+
echo "Adding test suffix"
79+
# Update version in pyproject.toml and in __init__.py
80+
awk -v new_version="$TEST_VERSION" '/^version = / {$0 = "version = \"" new_version "\""} 1' pyproject.toml > pyproject.tmp && mv pyproject.tmp pyproject.toml
81+
awk -v new_version="$TEST_VERSION" '/^__version__ = / {$0 = "__version__ = \"" new_version "\""} 1' darwin/__init__.py > darwin/__init__.tmp && mv darwin/__init__.tmp darwin/__init__.py
82+
5983
NEW_VERSION="${BASE_VERSION}-test.${TIMESTAMP}"
84+
echo $NEW_VERSION
6085
TAG_PREFIX="test-"
61-
BRANCH_NAME="test/version-bump"
86+
BRANCH_NAME="test/version-bump-${NEW_VERSION}"
6287
else
6388
NEW_VERSION="${BASE_VERSION}"
6489
TAG_PREFIX="v"
@@ -68,20 +93,35 @@ jobs:
6893
echo "TAG_PREFIX=${TAG_PREFIX}" >> $GITHUB_ENV
6994
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
7095
96+
- name: Configure Git
97+
run: |
98+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
99+
git config --global user.name "GitHub Actions"
100+
env:
101+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
102+
71103
- name: Commit and push changes
72-
if: ${{ inputs.test_mode != 'true' }}
104+
if: ${{ env.TEST_MODE != 'true' }}
73105
run: |
106+
echo "Commit and push version changes"
74107
git add pyproject.toml darwin/__init__.py
75108
git commit -m "Version bump to ${{ env.NEW_VERSION }}"
76109
git push origin HEAD:${BRANCH_NAME}
110+
env:
111+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77112

78113
- name: Create test branch
79-
if: ${{ inputs.test_mode == 'true' }}
114+
if: ${{ env.TEST_MODE == 'true' }}
80115
run: |
116+
echo "Create test branch"
81117
git checkout -b ${BRANCH_NAME}
82118
git push origin ${BRANCH_NAME}
119+
env:
120+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83121

84122
- name: Create and push tag
85123
run: |
86124
git tag -a "${TAG_PREFIX}${NEW_VERSION}" -m "bump version to ${TAG_PREFIX}${NEW_VERSION}"
87125
git push origin "${TAG_PREFIX}${NEW_VERSION}"
126+
env:
127+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

deploy/increase_version.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ def __str__(self) -> str:
9696
return f"{self.major}.{self.minor}.{self.patch}"
9797

9898

99-
def confirm(question: str) -> bool:
99+
def confirm(question: str, auto_answer: str = None) -> bool:
100+
if auto_answer is not None:
101+
return auto_answer.lower() in ["y", "yes"]
102+
100103
while True:
101104
answer = input(f"{question} [y/n]: ").lower().strip()
102105
if answer in ["y", "yes"]:
@@ -245,7 +248,6 @@ def arguments() -> argparse.Namespace:
245248
action="store_true",
246249
help="run in CI/CD mode (no confirmation, assume failure unless --force specified)",
247250
)
248-
249251
parser.add_argument(
250252
"-v",
251253
"--version",
@@ -268,6 +270,12 @@ def arguments() -> argparse.Namespace:
268270
type=str,
269271
help="set new version number (overrides -M, -m, -p)",
270272
)
273+
parser.add_argument(
274+
"--auto-confirm",
275+
type=str,
276+
choices=["y", "n"],
277+
help="Automatically confirm the action (y/n)",
278+
)
271279

272280
return parser.parse_args()
273281

@@ -337,7 +345,10 @@ def main() -> None:
337345
if new_version.was_changed() and (
338346
force_actions
339347
or cicd_mode
340-
or confirm(f"Update version from {str(LOCAL_VERSION)} to {str(new_version)}?")
348+
or confirm(
349+
f"Update version from {str(LOCAL_VERSION)} to {str(new_version)}?",
350+
args.auto_confirm,
351+
)
341352
):
342353
_update_version(new_version)
343354
_update_pyproject_version(new_version)

0 commit comments

Comments
 (0)