diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e8e070..819a2e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## Changed - "Dogfood" action to check itself +- Move python code out of workflow file ## [v1.0.0] - 2024-04-18 diff --git a/action.yml b/action.yml index c2243aa..530bfc9 100644 --- a/action.yml +++ b/action.yml @@ -15,6 +15,10 @@ runs: - name: Checkout repository uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.10' + # https://github.com/orgs/community/discussions/9049#discussioncomment-4239509 # Due to limitations on composite Actions, we can't do something like: # - uses: docker://ghcr.io/uclahs-cds/cicd-base:${{ inputs.docker-tag }} @@ -22,29 +26,10 @@ runs: # There absolutely should not be an 'action.yml' file underneath the .git # folder of the calling repository already, so we can use that. - name: Configure workflow - shell: python env: DOCKER_IMAGE_TAG: ${{ inputs.docker-tag }} - run: | - import os - import re - from pathlib import Path - - # Bail out if there are any illegal characters in the tag - tag = os.environ.get("DOCKER_IMAGE_TAG") - if re.search(r"[^a-zA-Z0-9_.\-]", tag): - raise ValueError(f"Problem with the tag `{tag}`!") - - template = Path( - os.environ.get("GITHUB_ACTION_PATH"), "template", "action.yml" - ) - - Path(".git/action.yml").write_text( - template.read_text(encoding="utf-8").replace( - "DOCKER_IMAGE_TAG", tag - ), - encoding="utf-8" - ) + shell: bash + run: python '${{ github.action_path }}/create_template.py' - name: Run checks uses: ./.git diff --git a/create_template.py b/create_template.py new file mode 100644 index 0000000..34b3e41 --- /dev/null +++ b/create_template.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +"Generate an action.yml file from the template." +import os +import re +from pathlib import Path + +# Bail out if there are any illegal characters in the tag +tag = os.environ.get("DOCKER_IMAGE_TAG") +if re.search(r"[^a-zA-Z0-9_.\-]", tag): + raise ValueError(f"Problem with the tag `{tag}`!") + +template = Path(os.environ.get("GITHUB_ACTION_PATH"), "template", "action.yml") +output_file = Path(os.environ.get("GITHUB_WORKSPACE"), ".git", "action.yml") + +output_file.write_text( + template.read_text(encoding="utf-8").replace( + "DOCKER_IMAGE_TAG", tag + ), + encoding="utf-8" +)