Release Workflow #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Release Workflow | |
on: | |
workflow_dispatch: | |
inputs: | |
releaseType: | |
description: 'Type of release (major, minor, patch, prerelease). Set "auto" for automatic determination.' | |
required: false | |
default: 'auto' | |
type: choice | |
options: | |
- auto | |
- prerelease | |
- patch | |
- minor | |
- major | |
noop: | |
type: boolean | |
description: 'Run in dry-run mode? Set to "true" to enable.' | |
required: true | |
default: false | |
prerelease_token: | |
type: string | |
description: 'Prerelease token to use for prerelease versions.' | |
required: false | |
default: 'rc' | |
add_branch_date: | |
type: boolean | |
description: 'Add branch and data to release name.' | |
required: false | |
default: false | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
concurrency: release | |
permissions: | |
id-token: write | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
persist-credentials: false # Do not store credentials as they will be configured manually | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' | |
- name: Install Dependencies | |
run: pip install python-semantic-release | |
- name: Get Current Date | |
id: get-date | |
run: echo "CURRENT_DATE=$(date +%Y%m%d)" >> $GITHUB_ENV | |
- name: Configure git | |
env: | |
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | |
run: | | |
git config user.name 'github-actions' | |
git config user.email 'github-actions@github.com'## | |
# Inject the token directly into the remote URL securely | |
git remote set-url origin https://${{ github.actor }}:${REPO_ACCESS_TOKEN}@github.com/${{ github.repository }}.git | |
- name: Python Semantic Release | |
env: | |
GH_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} # This ensures the token is safely injected from your repository secrets | |
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | |
CURRENT_DATE: $(date +%Y%m%d) # Set the current date in the format YYYYMMDD | |
run: | | |
release_type="${{ github.event.inputs.releaseType }}" | |
prerelease_token="${{ github.event.inputs.prerelease_token }}" | |
add_branch_date="${{ github.event.inputs.add_branch_date }}" | |
# flags following "version" command | |
version_flags="" | |
# top level flags for semantic-release | |
toplevel_flags="" | |
# Add --noop if requested | |
if [[ "${{ github.event.inputs.noop }}" == "true" ]]; then | |
toplevel_flags="$toplevel_flags --noop" | |
fi | |
# Check releaseType and format accordingly | |
if [[ "$release_type" != "auto" ]]; then | |
version_flags="$version_flags --$release_type" | |
fi | |
# Check for prerelease token and add it if present | |
if [[ "$prerelease_token" != "" ]]; then | |
version_flags="$version_flags --prerelease-token $prerelease_token" | |
fi | |
# Add build metadata if add_branch_date is true | |
if [[ "$add_branch_date" == "true" ]]; then | |
metadata=$(echo $(basename $GITHUB_REF) | sed 's/refs\/heads\///') # Extract branch name from GITHUB_REF | |
build_metadata="--build-metadata $metadata.$CURRENT_DATE" | |
version_flags="$version_flags $build_metadata" | |
fi | |
# Run semantic-release with the constructed flags | |
echo "Running semantic-release with flags (top-level): $toplevel_flags, (version): $version_flags" | |
GH_TOKEN=${REPO_ACCESS_TOKEN} semantic-release $toplevel_flags version $version_flags |