Skip to content

Commit

Permalink
Merge pull request #81 from FullStackWithLawrence/next
Browse files Browse the repository at this point in the history
Setup automated Python package version bump
  • Loading branch information
lpm0073 authored Nov 17, 2023
2 parents a0d1455 + c6c4a2f commit 3ed978a
Show file tree
Hide file tree
Showing 23 changed files with 372 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# If a new release is to be published then we want to consider running QA tests
# to ensure formatting and documentation is correct.
#------------------------------------------------------------------------------
name: Evaluate Pull Request
name: Pull Request Controller

on:
workflow_dispatch:
Expand Down
112 changes: 112 additions & 0 deletions .github/workflows/semanticVersionBump.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#------------------------------------------------------------------------------
# Lawrence McDaniel - https://lawrencemcdaniel.com
# Version Bump Workflow
#
# Calculate the version of the 'next' branch based on semantic-release rules.
# Compares the existing value of __version__.py to the calculated value.
# If they are different, it will update __version__.py and push the changes
# to the main branch.
#------------------------------------------------------------------------------
name: Semantic Version Bump (next)

on:
workflow_dispatch:
push:
branches:
- next
- next-major

jobs:
bump-version-next:
runs-on: ubuntu-latest
env:
VERSION_FILE: __version__.py
VERSION_FILE_PATH: ${{ github.workspace }}/api/terraform/python/layer_genai/openai_utils/

steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: '3.11'

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '20.9.0'

- name: Install npm dev dependencies
run: npm install

- name: Get current version
# step 1
# the current version persisted to __version__.py
id: current_version
run: |
cd ${{ env.VERSION_FILE_PATH }}
echo "CURRENT_VERSION=$(python -c 'from __version__ import __version__; print(__version__)')" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

- name: null step
id: null_step1
run: echo "i ensure that CURRENT_VERSION is set."

- name: Get next version
# step 2
# calculate the next version based on semantic-release rules
# this will return a null string is there in fact is no version bump.
# so set NEXT_VERSION to CURRENT_VERSION if there is no version bump.
id: next_version
run: |
NEXT_VERSION=$(npx semantic-release --dry-run --no-ci | awk '/The next release version is/{print $NF}')
echo "NEXT_VERSION=${NEXT_VERSION:-${{ env.CURRENT_VERSION }}}" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
CURRENT_VERSION: ${{ env.CURRENT_VERSION }}

- name: null step
id: null_step2
run: echo "i ensure that NEXT_VERSION is set."

- name: Check versions
# step 3
# compare the current version to the next version.
# if they are different, set VERSION_CHANGED to true
id: check_versions
run: |
if [ "$CURRENT_VERSION" != "$NEXT_VERSION" ]; then
echo "VERSION_CHANGED=true" >> $GITHUB_ENV
else
echo "VERSION_CHANGED=false" >> $GITHUB_ENV
fi
env:
CURRENT_VERSION: ${{ env.CURRENT_VERSION }}
NEXT_VERSION: ${{ env.NEXT_VERSION }}

- name: another null step
id: null_step3
run: echo "i ensure that CURRENT_VERSION, NEXT_VERSION and VERSION_CHANGED are set."


- name: Update __version__.py
# step 4
# if VERSION_CHANGED is true, update __version__.py and push the changes to the
# branch that triggered this workflow.
if: env.VERSION_CHANGED == 'true'
id: update_version
run: |
echo "__version__ = '${{ env.NEXT_VERSION }}'" > ${{ env.VERSION_FILE }}
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add ${{ env.VERSION_FILE }}
git commit -m "chore: [gh] Update __version__.py to ${{ env.NEXT_VERSION }} [skip ci]"
git push https://${{ secrets.PAT }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }}
env:
VERSION_FILE: ${{ env.VERSION_FILE_PATH }}${{ env.VERSION_FILE }}
GITHUB_TOKEN: ${{ secrets.PAT }}
NEXT_VERSION: ${{ env.NEXT_VERSION }}
VERSION_CHANGED: ${{ env.VERSION_CHANGED }}
89 changes: 89 additions & 0 deletions .github/workflows/semanticVersionBumpMerge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#------------------------------------------------------------------------------
# Lawrence McDaniel - https://lawrencemcdaniel.com
# Version Bump and Merge Workflow
#
# Calculate the next version in the main branch. Compares the raw value of
# __version__.py to the calculated value in setup.py. If they are different,
# it will update __version__.py and push the changes to the main branch.
#------------------------------------------------------------------------------
name: Semantic Version Bump (main)

on:
push:
branches:
- main

jobs:
set-version-main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false

- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: '3.11'

- name: Get current version
# step 1
# the current version persisted to __version__.py
id: current_version
run: |
echo "CURRENT_VERSION=$(python -c 'from __version__ import __version__; print(__version__)')" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

- name: Get next version
# step 2
# retrieve the the next version based on the logic in setup.py, which
# strips the pre-release tag from the current version.
id: next_version
run: |
echo "NEXT_VERSION=$(python -c 'from setup_utils import get_semantic_version; print(get_semantic_version())')" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

- name: null step
id: null_step
run: echo "i ensure that NEXT_VERSION is set."

- name: Check versions
# step 3
# calculate the next version based on a comparison of the current version
# to the version calucalated in setup.py. If they are different, then
# the version used in setup.py takes precedence.
id: check_versions
run: |
if [ "$CURRENT_VERSION" != "$NEXT_VERSION" ]; then
echo "VERSION_CHANGED=true" >> $GITHUB_ENV
else
echo "VERSION_CHANGED=false" >> $GITHUB_ENV
fi
env:
CURRENT_VERSION: ${{ env.CURRENT_VERSION }}
NEXT_VERSION: ${{ env.NEXT_VERSION }}

- name: another null step
id: another_null_step
run: echo "i ensure that CURRENT_VERSION, NEXT_VERSION and VERSION_CHANGED are set."


- name: Update __version__.py
# step 4
# if VERSION_CHANGED is true, update __version__.py and push the changes to the
# main branch.
if: env.VERSION_CHANGED == 'true'
id: update_version
run: |
echo "__version__ = '${{ env.NEXT_VERSION }}'" > __version__.py
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add __version__.py
git commit -m "chore: [gh] Update __version__.py to ${{ env.NEXT_VERSION }} [skip ci]"
git push https://${{ secrets.PAT }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }}
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
NEXT_VERSION: ${{ env.NEXT_VERSION }}
VERSION_CHANGED: ${{ env.VERSION_CHANGED }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ tests/go.*
# ReactJS
client/node_modules/
node_modules

package-lock.json
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ api-init:
# create python virtual environments for dev as well
# as for the Lambda layer.
# ---------------------------------------------------------
npm install && \
python3.11 -m venv venv && \
source venv/bin/activate && \
pip install --upgrade pip && \
Expand Down
Loading

0 comments on commit 3ed978a

Please sign in to comment.