Generate Docker container action with Python

This commit is contained in:
Brendon Smith 2024-05-27 13:45:20 -04:00
parent 0d8d5059c8
commit aed6c4b1b0
No known key found for this signature in database
5 changed files with 114 additions and 61 deletions

View File

@ -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 }}

View File

@ -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 }}

88
create-docker-action.py Normal file
View File

@ -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)

View File

@ -0,0 +1,2 @@
# NOTE: used by create-docker-action.py
pyyaml

View File

@ -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