Skip to content

Commit 9d14a8d

Browse files
committed
Generate Docker container action with Python
1 parent 27ce557 commit 9d14a8d

File tree

5 files changed

+114
-61
lines changed

5 files changed

+114
-61
lines changed

.github/actions/run-docker-container/action.yml

Lines changed: 0 additions & 45 deletions
This file was deleted.

action.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,25 @@ runs:
119119
env:
120120
ACTION_REF: ${{ github.action_ref }}
121121
ACTION_REPO: ${{ github.action_repository }}
122-
- name: Set Docker image name and tag
122+
- name: Check out action repo
123+
uses: actions/checkout@v4
124+
with:
125+
path: action-repo
126+
ref: ${{ steps.set-repo-and-ref.outputs.ref }}
127+
repository: ${{ steps.set-repo-and-ref.outputs.repo }}
128+
- name: Create Docker container action
123129
run: |
124-
# Set Docker image name and tag
125-
# if action run was triggered by a pull request to this repo,
126-
# build image from Dockerfile because it has not been pushed to GHCR,
127-
# else pull image from GHCR
128-
if [[ $GITHUB_EVENT_NAME == "pull_request" ]] &&
129-
[[ $GITHUB_REPOSITORY == "pypa/gh-action-pypi-publish" ]]; then
130-
IMAGE="../../../Dockerfile"
131-
else
132-
REF=${{ steps.set-repo-and-ref.outputs.ref }}
133-
REPO=${{ steps.set-repo-and-ref.outputs.repo }}
134-
IMAGE="docker://ghcr.io/$REPO:${REF/'/'/'-'}"
135-
fi
136-
FILE=".github/actions/run-docker-container/action.yml"
137-
sed -i -e "s|{{image}}|$IMAGE|g" "$FILE"
130+
# Create Docker container action
131+
python -m pip install -r requirements/github-actions.txt
132+
python create-docker-action.py ${{ steps.set-image.outputs.image }}
133+
env:
134+
EVENT: ${{ github.event_name }}
135+
REF: ${{ steps.set-repo-and-ref.outputs.ref }}
136+
REPO: ${{ steps.set-repo-and-ref.outputs.repo }}
138137
shell: bash
138+
working-directory: action-repo
139139
- name: Run Docker container
140-
uses: ./.github/actions/run-docker-container
140+
uses: ./action-repo/.github/actions/run-docker-container
141141
with:
142142
user: ${{ inputs.user }}
143143
password: ${{ inputs.password }}

create-docker-action.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import pathlib
3+
4+
import yaml
5+
6+
DESCRIPTION = 'description'
7+
REQUIRED = 'required'
8+
9+
EVENT = os.environ['EVENT']
10+
REF = os.environ['REF']
11+
REPO = os.environ['REPO']
12+
13+
14+
def set_image(event: str, ref: str, repo: str) -> str:
15+
if event == 'pull_request' and 'gh-action-pypi-publish' in repo:
16+
return '../../../Dockerfile'
17+
docker_ref = ref.replace('/', '-')
18+
return f'docker://ghcr.io/{repo}:{docker_ref}'
19+
20+
21+
image = set_image(EVENT, REF, REPO)
22+
23+
action = {
24+
'name': '🏃',
25+
DESCRIPTION: (
26+
'Run Docker container to upload Python distribution packages to PyPI'
27+
),
28+
'inputs': {
29+
'user': {DESCRIPTION: 'PyPI user', REQUIRED: False},
30+
'password': {
31+
DESCRIPTION: 'Password for your PyPI user or an access token',
32+
REQUIRED: False,
33+
},
34+
'repository-url': {
35+
DESCRIPTION: 'The repository URL to use',
36+
REQUIRED: False,
37+
},
38+
'packages-dir': {
39+
DESCRIPTION: 'The target directory for distribution',
40+
REQUIRED: False,
41+
},
42+
'verify-metadata': {
43+
DESCRIPTION: 'Check metadata before uploading',
44+
REQUIRED: False,
45+
},
46+
'skip-existing': {
47+
DESCRIPTION: (
48+
'Do not fail if a Python package distribution'
49+
' exists in the target package index'
50+
),
51+
REQUIRED: False,
52+
},
53+
'verbose': {DESCRIPTION: 'Show verbose output.', REQUIRED: False},
54+
'print-hash': {
55+
DESCRIPTION: 'Show hash values of files to be uploaded',
56+
REQUIRED: False,
57+
},
58+
'attestations': {
59+
DESCRIPTION: (
60+
'[EXPERIMENTAL]'
61+
' Enable experimental support for PEP 740 attestations.'
62+
' Only works with PyPI and TestPyPI via Trusted Publishing.'
63+
),
64+
REQUIRED: False,
65+
}
66+
},
67+
'runs': {
68+
'using': 'docker',
69+
'image': image,
70+
'args': [
71+
'${{ inputs.user }}',
72+
'${{ inputs.password }}',
73+
'${{ inputs.repository-url }}',
74+
'${{ inputs.packages-dir }}',
75+
'${{ inputs.verify-metadata }}',
76+
'${{ inputs.skip-existing }}',
77+
'${{ inputs.verbose }}',
78+
'${{ inputs.print-hash }}',
79+
'${{ inputs.attestations }}',
80+
],
81+
},
82+
}
83+
84+
action_path = pathlib.Path('.github/actions/run-docker-container/action.yml')
85+
action_path.parent.mkdir(parents=True, exist_ok=True)
86+
87+
with action_path.open(mode='w', encoding='utf-8') as file:
88+
yaml.dump(action, file, allow_unicode=True, sort_keys=False)

requirements/github-actions.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# NOTE: used by create-docker-action.py
2+
pyyaml

requirements/github-actions.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.12
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --config=../.pip-tools.toml --output-file=github-actions.txt --strip-extras github-actions.in
6+
#
7+
pyyaml==6.0.1
8+
# via -r github-actions.in

0 commit comments

Comments
 (0)