Write GitHub Action for package/tag version verification (avoid duplication of code in GitHub Actions)
kniklas opened this issue · comments
Currently both publishing workflows: publish-dev.yml
and publish-master.yml
use duplicated code which verifies if tag version is the same as checked out python package version (stored in src/getfx/__init__.py
.
Ideally this duplicated code should be replaced with a new GitHub Action:
- preferably should be written in Node.js (preferred over docker as it takes at least 15-30s to build an image)
- (nice to have) such action should use repository (or environment) secret to run or not (by default run)
Example of code snippet to be converted to GitHub Action:
- name: Set variables
id: get_version
run: |
echo ::set-output name=TAG_VER::$(echo $GITHUB_REF | cut -d / -f 3)
echo "GETFX_SRC_VER=v$(cat src/getfx/__init__.py | grep version | awk -F"= " '{ print $2 }' | sed 's/"//g')" >> $GITHUB_ENV
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-8)" >> $GITHUB_ENV
- name: Fail if SRC and GIT TAG versions are different
if: env.GETFX_SRC_VER != steps.get_version.outputs.TAG_VER
uses: actions/github-script@v3
with:
script: core.setFailed('Different tag and SRC versions ${{ steps.get_version.outputs.TAG_VER}} vs ${{ env.GETFX_SRC_VER}}')
Note:
- first and second echo commands are specific to the new GitHub action and can be replaced by it
- third echo command is required only by later parts of workflow action which uses it for publishing documentation (it should remain in workflow action)
Note on using secrets in if:
statements
There was an attempt to add in if: statement secret that would indicate if this check should be performed, but after few trials it was not successful. Probably this can be overcome (https://stackoverflow.com/questions/61255989/dynamically-retrieve-github-actions-secret) but apparently secrets can't be used for that purpose: actions/runner#520
Additionally following link could be check if will be useful: https://github.com/marketplace/actions/secret-input-gate
Not sure if this problem will manifest for new GitHub Action.
Consider composite actions: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action