Skip to content

Fixed beta release in publish workflow (part 2) #12

Fixed beta release in publish workflow (part 2)

Fixed beta release in publish workflow (part 2) #12

Workflow file for this run

# This GitHub workflow will publish the package to Pypi and create a new stable branch when releasing the master branch.
# For more information see:
# https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
# https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
name: publish
on:
push: # When pushing a tag
tags:
- "*"
jobs:
publish:
name: Build and publish to PyPI
if: startsWith(github.ref, 'refs/tags')
runs-on: ubuntu-latest
steps:
#-------- Info gathering and checks
- name: Set pushed tag
id: set-tag
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const result = "${{ github.ref }}".match("refs/tags/(.*)$")[1]
console.log(result)
return result
- name: Check validity of pushed tag
run: |
if [[ ${{ steps.set-tag.outputs.result }} =~ ^[0-9]+\.[0-9]+\.[0-9]+((a|b|rc)[0-9]+)?$ ]]; then
echo "Pushed tag '${{ steps.set-tag.outputs.result }}' is valid";
else
echo "Pushed tag '${{ steps.set-tag.outputs.result }}' is invalid (must be 'M.N.U' or a pre-release 'M.N.Ub/c/rcP'";
false;
fi
- name: Determine whether releasing the master branch
id: set-is-master-branch
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const resp = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/master",
})
const result = (resp.data.object.sha == "${{ github.sha }}")
console.log(result)
return result
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Determine name of stable branch for pushed tag
id: set-stable-branch
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const result = "stable_"+"${{ steps.set-tag.outputs.result }}".match("([0-9]+\.[0-9]+)\.")[1]
console.log(result)
return result
- name: Determine whether releasing stable branch for pushed tag
id: set-is-stable-branch
uses: actions/github-script@v6
with:
result-encoding: string
script: |
var resp
try {
resp = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/${{ steps.set-stable-branch.outputs.result }}",
})
}
catch(err) {
console.log("false (stable branch does not exist: "+err+")")
return false
}
const result = (resp.data.object.sha == "${{ github.sha }}")
console.log(result)
return result
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Determine name of beta branch for pushed tag
id: set-beta-branch
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const result = "beta_"+"${{ steps.set-tag.outputs.result }}".match("([0-9]+\.[0-9]+)\.")[1]
console.log(result)
return result
- name: Determine whether releasing beta branch for pushed tag
id: set-is-beta-branch
uses: actions/github-script@v6
with:
result-encoding: string
script: |
var resp
try {
resp = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/${{ steps.set-beta-branch.outputs.result }}",
})
}
catch(err) {
console.log("false (beta branch does not exist: "+err+")")
return false
}
const result = (resp.data.object.sha == "${{ github.sha }}")
console.log(result)
return result
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check released commit to be master or stable or beta branch for pushed tag
run: |
if [[ ${{ steps.set-is-master-branch.outputs.result }} == 'false' && ${{ steps.set-is-stable-branch.outputs.result }} == 'false' && ${{ steps.set-is-beta-branch.outputs.result }} == 'false' ]]; then
echo "Released commit is not 'master' or '${{ steps.set-stable-branch.outputs.result }}' or '${{ steps.set-beta-branch.outputs.result }}' branch";
false;
fi
- name: Set update version
id: set-update-version
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const result = "${{ steps.set-tag.outputs.result }}".match("[0-9]+\.[0-9]+\.([0-9]+)")[1]
console.log(result)
return result
- name: Check update version to be 0 when releasing master branch
if: ${{ steps.set-is-master-branch.outputs.result == 'true' }}
run: |
if [[ ${{ steps.set-update-version.outputs.result }} != '0' ]]; then
echo "Update version '${{ steps.set-update-version.outputs.result }}' in tag '${{ steps.set-tag.outputs.result }}' is invalid (must be 0 when releasing master branch)";
false;
fi
- name: Check update version to be non-0 when releasing stable branch for pushed tag
if: ${{ steps.set-is-stable-branch.outputs.result == 'true' }}
run: |
if [[ ${{ steps.set-update-version.outputs.result }} == '0' ]]; then
echo "Update version '${{ steps.set-update-version.outputs.result }}' in tag '${{ steps.set-tag.outputs.result }}' is invalid (must be non-0 when releasing stable branch for pushed tag)";
false;
fi
#-------- Setup of work environment
- name: Checkout repo
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Development setup
run: |
make develop
#-------- Publishing of package
- name: Build the distribution
run: |
make build
- name: Display the distribution directory
run: |
ls -l dist
# - name: Publish distribution to TestPyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# packages_dir: dist
# password: ${{ secrets.TEST_PYPI_API_TOKEN }}
# repository_url: https://test.pypi.org/legacy/
- name: Publish distribution to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages_dir: dist
password: ${{ secrets.PYPI_API_TOKEN }}
#-------- Creation of Github release
- name: Determine whether release on Github exists for the pushed tag
id: set-release-exists
uses: octokit/[email protected]
with:
route: GET /repos/${{ github.repository }}/releases/tags/${{ steps.set-tag.outputs.result }}
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create release on Github for the pushed tag if it does not exist
if: ${{ steps.set-release-exists.outputs.status == 404 }}
uses: octokit/[email protected]
with:
route: POST /repos/${{ github.repository }}/releases
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_TAG_NAME: ${{ steps.set-tag.outputs.result }}
INPUT_NAME: "Release ${{ steps.set-tag.outputs.result }}"
INPUT_BODY: "Change log https://zhmc-prometheus-exporter.readthedocs.io/en/${{ steps.set-tag.outputs.result }}/changes.html"
#-------- Creation of stable branch
- name: Create new stable branch when releasing master branch
if: steps.set-is-master-branch.outputs.result == 'true'
uses: actions/github-script@v6
with:
script: |
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/heads/${{ steps.set-stable-branch.outputs.result }}",
sha: "${{ github.sha }}",
})
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}