Skip to content

Commit

Permalink
Add script to increment version automatically and hook it into the re…
Browse files Browse the repository at this point in the history
…lease
  • Loading branch information
alecgrieser committed Jan 31, 2025
1 parent e64aa61 commit 663c7d8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
25 changes: 19 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@ name: Release

on:
workflow_dispatch:
inputs:
artifact_version:
description: 'Target version for artifacts to build and publish'
required: true

jobs:
gradle:
runs-on: ubuntu-latest
permissions:
checks: write
contents: read
contents: write
packages: write
pages: write
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.13'
- name: Checkout sources
uses: actions/checkout@v4.2.2
- name: Run Gradle Test
uses: ./actions/gradle-test
with:
artifacts_version: "${{ inputs.artifact_version }}"
gradle_args: "-PreleaseBuild=true"

# Always push a version bump back to main. There are failure scenarios that can result
# in published artifacts but an erroneous build, and it's safer to have version gaps
- name: Configure Git
if: always()
run: |
git config --global user.name 'FoundationDB CI'
git config --global user.email 'foundationdb_ci@apple.com'
- name: Increment Version
if: always()
run: python build/increment_version.py gradle.properties --commit
- name: Push Changes
if: always()
run: git push
2 changes: 1 addition & 1 deletion actions/gradle-test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ runs:
run: sed -i -e "s/^org\.gradle\..*/#&/g" gradle.properties
- name: Run build and test
shell: bash
run: ARTIFACT_VERSION="${{ inputs.artifact_version }}" GRADLE_OPTS="-XX:+HeapDumpOnOutOfMemoryError -Xverify:none -XX:+TieredCompilation -Xmx4096m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED" ./gradlew --no-daemon --console=plain -b ./build.gradle build destructiveTest -PcoreNotStrict ${{ inputs.gradle_args }}
run: GRADLE_OPTS="-XX:+HeapDumpOnOutOfMemoryError -Xverify:none -XX:+TieredCompilation -Xmx4096m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED" ./gradlew --no-daemon --console=plain -b ./build.gradle build destructiveTest -PcoreNotStrict ${{ inputs.gradle_args }}
- name: Copy Test Reports
shell: bash
if: always()
Expand Down
95 changes: 95 additions & 0 deletions build/increment_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/python3
#
# increment_version.py
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
import re
import subprocess
import sys


VERSION_POSITIONS = ['MAJOR', 'MINOR', 'BUILD', 'PATCH']
VERSION_LINE = re.compile(r'version\s*\=\s*(\d+)\.(\d+)\.(\d+)\.(\d+)')


def incremented_version(version: (int, int, int, int), update_type: str) -> tuple[int, int, int, int]:
update_pos = VERSION_POSITIONS.index(update_type)
new_version = []
for i in range(len(version)):
if i < update_pos:
new_version.append(version[i])
elif i == update_pos:
new_version.append(version[i] + 1)
else:
new_version.append(0)
return tuple(new_version)


def version_string(version: tuple[int, int, int, int]) -> str:
return '.'.join(map(str, version))


def update_version(filename: str, update_type: str) -> tuple[int, int, int, int]:
lines = []
found = False
version = None
new_version = None
with open(filename, 'r') as fin:
for l in fin:
m = VERSION_LINE.match(l)
if m:
if version is not None:
raise ValueError('File contains multiple version lines')
version = (int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)))
new_version = incremented_version(version, update_type)
lines.append(f'version={version_string(new_version)}\n')
found = True
else:
lines.append(l)

if not found:
raise ValueError(f'Unable to find version in {filename}')

with open(filename, 'w') as fout:
for l in lines:
fout.write(l)
print(f'Update version file {filename}')
return new_version


def main(argv: list[str]):
parser = argparse.ArgumentParser(prog='increment_version',
description='Utility to increment the project version stored in a version file')
parser.add_argument('filename', type=str, help='File containing version to increment')
parser.add_argument('-u', '--update-type', type=str, default='BUILD', choices=VERSION_POSITIONS,
help='Type of update. Determines which position within the build number is updated')
parser.add_argument('-c', '--commit', action='store_true', default=False, help='Whether to commit the update or not')

args = parser.parse_args(argv)
new_version = update_version(args.filename, args.update_type)

if args.commit:
subprocess.check_output(['git', 'add', args.filename])
subprocess.check_output(['git', 'commit', '-m', f'Updating version to {version_string(new_version)}'])
print('Version update committed')


if __name__ == '__main__':
main(sys.argv[1:])
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

rootProject.name=fdb-record-layer
version=4.0
version=4.0.570.0
releaseBuild=false

# this should be false for release branches (i.e. if there is no -SNAPSHOT on the above version)
Expand Down

0 comments on commit 663c7d8

Please sign in to comment.