From aed6c4b1b02d8644667434f8e4c077b9a2db90b4 Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Mon, 27 May 2024 13:45:20 -0400 Subject: [PATCH] Generate Docker container action with Python --- .../actions/run-docker-container/action.yml | 45 ---------- action.yml | 32 +++---- create-docker-action.py | 88 +++++++++++++++++++ requirements/github-actions.in | 2 + requirements/github-actions.txt | 8 ++ 5 files changed, 114 insertions(+), 61 deletions(-) delete mode 100644 .github/actions/run-docker-container/action.yml create mode 100644 create-docker-action.py create mode 100644 requirements/github-actions.in create mode 100644 requirements/github-actions.txt diff --git a/.github/actions/run-docker-container/action.yml b/.github/actions/run-docker-container/action.yml deleted file mode 100644 index e1b1ecd..0000000 --- a/.github/actions/run-docker-container/action.yml +++ /dev/null @@ -1,45 +0,0 @@ ---- -name: 🏃 -description: >- - Run Docker container to - upload Python distribution packages to PyPI -inputs: - user: - description: PyPI user - required: false - password: - description: Password for your PyPI user or an access token - required: false - repository-url: - description: The repository URL to use - required: false - packages-dir: - description: The target directory for distribution - required: false - verify-metadata: - description: Check metadata before uploading - required: false - skip-existing: - description: >- - Do not fail if a Python package distribution - exists in the target package index - required: false - verbose: - description: Show verbose output. - required: false - print-hash: - description: Show hash values of files to be uploaded - required: false -runs: - using: docker - image: "{{image}}" - args: - - ${{ inputs.user }} - - ${{ inputs.password }} - - ${{ inputs.repository-url }} - - ${{ inputs.packages-dir }} - - ${{ inputs.verify-metadata }} - - ${{ inputs.skip-existing }} - - ${{ inputs.verbose }} - - ${{ inputs.print-hash }} - - ${{ inputs.attestations }} diff --git a/action.yml b/action.yml index e67f9c2..d051ab1 100644 --- a/action.yml +++ b/action.yml @@ -119,25 +119,25 @@ runs: env: ACTION_REF: ${{ github.action_ref }} ACTION_REPO: ${{ github.action_repository }} - - name: Set Docker image name and tag + - name: Check out action repo + uses: actions/checkout@v4 + with: + path: action-repo + ref: ${{ steps.set-repo-and-ref.outputs.ref }} + repository: ${{ steps.set-repo-and-ref.outputs.repo }} + - name: Create Docker container action run: | - # Set Docker image name and tag - # if action run was triggered by a pull request to this repo, - # build image from Dockerfile because it has not been pushed to GHCR, - # else pull image from GHCR - if [[ $GITHUB_EVENT_NAME == "pull_request" ]] && - [[ $GITHUB_REPOSITORY == "pypa/gh-action-pypi-publish" ]]; then - IMAGE="../../../Dockerfile" - else - REF=${{ steps.set-repo-and-ref.outputs.ref }} - REPO=${{ steps.set-repo-and-ref.outputs.repo }} - IMAGE="docker://ghcr.io/$REPO:${REF/'/'/'-'}" - fi - FILE=".github/actions/run-docker-container/action.yml" - sed -i -e "s|{{image}}|$IMAGE|g" "$FILE" + # Create Docker container action + python -m pip install -r requirements/github-actions.txt + python create-docker-action.py ${{ steps.set-image.outputs.image }} + env: + EVENT: ${{ github.event_name }} + REF: ${{ steps.set-repo-and-ref.outputs.ref }} + REPO: ${{ steps.set-repo-and-ref.outputs.repo }} shell: bash + working-directory: action-repo - name: Run Docker container - uses: ./.github/actions/run-docker-container + uses: ./action-repo/.github/actions/run-docker-container with: user: ${{ inputs.user }} password: ${{ inputs.password }} diff --git a/create-docker-action.py b/create-docker-action.py new file mode 100644 index 0000000..bc2e005 --- /dev/null +++ b/create-docker-action.py @@ -0,0 +1,88 @@ +import os +import pathlib + +import yaml + +DESCRIPTION = 'description' +REQUIRED = 'required' + +EVENT = os.environ['EVENT'] +REF = os.environ['REF'] +REPO = os.environ['REPO'] + + +def set_image(event: str, ref: str, repo: str) -> str: + if event == 'pull_request' and 'gh-action-pypi-publish' in repo: + return '../../../Dockerfile' + docker_ref = ref.replace('/', '-') + return f'docker://ghcr.io/{repo}:{docker_ref}' + + +image = set_image(EVENT, REF, REPO) + +action = { + 'name': '🏃', + DESCRIPTION: ( + 'Run Docker container to upload Python distribution packages to PyPI' + ), + 'inputs': { + 'user': {DESCRIPTION: 'PyPI user', REQUIRED: False}, + 'password': { + DESCRIPTION: 'Password for your PyPI user or an access token', + REQUIRED: False, + }, + 'repository-url': { + DESCRIPTION: 'The repository URL to use', + REQUIRED: False, + }, + 'packages-dir': { + DESCRIPTION: 'The target directory for distribution', + REQUIRED: False, + }, + 'verify-metadata': { + DESCRIPTION: 'Check metadata before uploading', + REQUIRED: False, + }, + 'skip-existing': { + DESCRIPTION: ( + 'Do not fail if a Python package distribution' + ' exists in the target package index' + ), + REQUIRED: False, + }, + 'verbose': {DESCRIPTION: 'Show verbose output.', REQUIRED: False}, + 'print-hash': { + DESCRIPTION: 'Show hash values of files to be uploaded', + REQUIRED: False, + }, + 'attestations': { + DESCRIPTION: ( + '[EXPERIMENTAL]' + ' Enable experimental support for PEP 740 attestations.' + ' Only works with PyPI and TestPyPI via Trusted Publishing.' + ), + REQUIRED: False, + } + }, + 'runs': { + 'using': 'docker', + 'image': image, + 'args': [ + '${{ inputs.user }}', + '${{ inputs.password }}', + '${{ inputs.repository-url }}', + '${{ inputs.packages-dir }}', + '${{ inputs.verify-metadata }}', + '${{ inputs.skip-existing }}', + '${{ inputs.verbose }}', + '${{ inputs.print-hash }}', + '${{ inputs.attestations }}', + ], + }, +} + +action_path = pathlib.Path('.github/actions/run-docker-container/action.yml') +action_path.parent.mkdir(parents=True, exist_ok=True) + +with action_path.open(mode='w', encoding='utf-8') as file: + yaml.dump(action, file, allow_unicode=True, sort_keys=False) diff --git a/requirements/github-actions.in b/requirements/github-actions.in new file mode 100644 index 0000000..2314722 --- /dev/null +++ b/requirements/github-actions.in @@ -0,0 +1,2 @@ +# NOTE: used by create-docker-action.py +pyyaml diff --git a/requirements/github-actions.txt b/requirements/github-actions.txt new file mode 100644 index 0000000..0f1caa2 --- /dev/null +++ b/requirements/github-actions.txt @@ -0,0 +1,8 @@ +# +# This file is autogenerated by pip-compile with Python 3.12 +# by the following command: +# +# pip-compile --allow-unsafe --config=../.pip-tools.toml --output-file=github-actions.txt --strip-extras github-actions.in +# +pyyaml==6.0.1 + # via -r github-actions.in