Skip to content

Commit 61da13d

Browse files
authored
Merge pull request #230 from br3ndonland/ghcr
Build Docker image and push to GHCR
2 parents fb13cb3 + 36965cb commit 61da13d

File tree

4 files changed

+224
-15
lines changed

4 files changed

+224
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
3+
name: 🏗️
4+
5+
on: # yamllint disable-line rule:truthy
6+
pull_request:
7+
push:
8+
branches: ["release/*", "unstable/*"]
9+
workflow_dispatch:
10+
inputs:
11+
tag:
12+
description: Docker image tag
13+
required: true
14+
type: string
15+
16+
jobs:
17+
smoke-test:
18+
uses: ./.github/workflows/reusable-smoke-test.yml
19+
build-and-push:
20+
if: github.event_name != 'pull_request'
21+
runs-on: ubuntu-latest
22+
needs:
23+
- smoke-test
24+
timeout-minutes: 10
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Build Docker image
28+
run: |
29+
DOCKER_TAG="${DOCKER_TAG/'/'/'-'}"
30+
DOCKER_TAG_MAJOR=$(echo "$DOCKER_TAG" | cut -d '.' -f 1)
31+
DOCKER_TAG_MAJOR_MINOR=$(echo "$DOCKER_TAG" | cut -d '.' -f 1-2)
32+
IMAGE="ghcr.io/$GITHUB_REPOSITORY:${DOCKER_TAG}"
33+
IMAGE_MAJOR="ghcr.io/$GITHUB_REPOSITORY:${DOCKER_TAG_MAJOR}"
34+
IMAGE_MAJOR_MINOR="ghcr.io/$GITHUB_REPOSITORY:${DOCKER_TAG_MAJOR_MINOR}"
35+
echo "IMAGE=$IMAGE" >>"$GITHUB_ENV"
36+
echo "IMAGE_MAJOR=$IMAGE_MAJOR" >>"$GITHUB_ENV"
37+
echo "IMAGE_MAJOR_MINOR=$IMAGE_MAJOR_MINOR" >>"$GITHUB_ENV"
38+
docker build . \
39+
--build-arg BUILDKIT_INLINE_CACHE=1 \
40+
--cache-from $IMAGE \
41+
--tag $IMAGE
42+
docker tag $IMAGE $IMAGE_MAJOR
43+
docker tag $IMAGE $IMAGE_MAJOR_MINOR
44+
env:
45+
DOCKER_TAG: ${{ inputs.tag || github.ref_name }}
46+
- name: Log in to GHCR
47+
run: >-
48+
echo ${{ secrets.GITHUB_TOKEN }} |
49+
docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
50+
- name: Push Docker image to GHCR
51+
run: |
52+
docker push $IMAGE
53+
docker push $IMAGE_MAJOR
54+
docker push $IMAGE_MAJOR_MINOR

.github/workflows/self-smoke-test-action.yml renamed to .github/workflows/reusable-smoke-test.yml

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22

3-
name: 🧪
3+
name: ♻️ 🧪
44

55
on: # yamllint disable-line rule:truthy
6-
push:
7-
pull_request:
6+
workflow_call:
87

98
env:
109
devpi-password: abcd1234
@@ -27,7 +26,33 @@ env:
2726
PYTEST_THEME_MODE
2827
2928
jobs:
29+
fail-fast:
30+
31+
strategy:
32+
matrix:
33+
os: [macos-latest, windows-latest]
34+
35+
runs-on: ${{ matrix.os }}
36+
37+
timeout-minutes: 2
38+
39+
steps:
40+
- name: Check out the action locally
41+
uses: actions/checkout@v4
42+
with:
43+
path: test
44+
- name: Fail-fast in unsupported environments
45+
continue-on-error: true
46+
id: fail-fast
47+
uses: ./test
48+
- name: Error if action did not fail-fast in unsupported environments
49+
if: steps.fail-fast.outcome == 'success'
50+
run: |
51+
>&2 echo This action should fail-fast in unsupported environments.
52+
exit 1
53+
3054
smoke-test:
55+
3156
runs-on: ubuntu-latest
3257

3358
services:

action.yml

+67-12
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,70 @@ branding:
9191
color: yellow
9292
icon: upload-cloud
9393
runs:
94-
using: docker
95-
image: Dockerfile
96-
args:
97-
- ${{ inputs.user }}
98-
- ${{ inputs.password }}
99-
- ${{ inputs.repository-url }}
100-
- ${{ inputs.packages-dir }}
101-
- ${{ inputs.verify-metadata }}
102-
- ${{ inputs.skip-existing }}
103-
- ${{ inputs.verbose }}
104-
- ${{ inputs.print-hash }}
105-
- ${{ inputs.attestations }}
94+
using: composite
95+
steps:
96+
- name: Fail-fast in unsupported environments
97+
if: runner.os != 'Linux'
98+
run: |
99+
>&2 echo This action is only able to run under GNU/Linux environments
100+
exit 1
101+
shell: bash -eEuo pipefail {0}
102+
- name: Reset path if needed
103+
run: |
104+
# Reset path if needed
105+
# https://github.com/pypa/gh-action-pypi-publish/issues/112
106+
if [[ $PATH != *"/usr/bin"* ]]; then
107+
echo "\$PATH=$PATH. Resetting \$PATH for GitHub Actions."
108+
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
109+
echo "PATH=$PATH" >>"$GITHUB_ENV"
110+
echo "$PATH" >>"$GITHUB_PATH"
111+
echo "\$PATH reset. \$PATH=$PATH"
112+
fi
113+
shell: bash
114+
- name: Set repo and ref from which to run Docker container action
115+
id: set-repo-and-ref
116+
run: |
117+
# Set repo and ref from which to run Docker container action
118+
# to handle cases in which `github.action_` context is not set
119+
# https://github.com/actions/runner/issues/2473
120+
REF=${{ env.ACTION_REF || env.PR_REF || github.ref_name }}
121+
REPO=${{ env.ACTION_REPO || env.PR_REPO || github.repository }}
122+
REPO_ID=${{ env.PR_REPO_ID || github.repository_id }}
123+
echo "ref=$REF" >>"$GITHUB_OUTPUT"
124+
echo "repo=$REPO" >>"$GITHUB_OUTPUT"
125+
echo "repo-id=$REPO_ID" >>"$GITHUB_OUTPUT"
126+
shell: bash
127+
env:
128+
ACTION_REF: ${{ github.action_ref }}
129+
ACTION_REPO: ${{ github.action_repository }}
130+
PR_REF: ${{ github.event.pull_request.head.ref }}
131+
PR_REPO: ${{ github.event.pull_request.head.repo.full_name }}
132+
PR_REPO_ID: ${{ github.event.pull_request.base.repo.id }}
133+
- name: Check out action repo
134+
uses: actions/checkout@v4
135+
with:
136+
path: action-repo
137+
ref: ${{ steps.set-repo-and-ref.outputs.ref }}
138+
repository: ${{ steps.set-repo-and-ref.outputs.repo }}
139+
- name: Create Docker container action
140+
run: |
141+
# Create Docker container action
142+
python create-docker-action.py
143+
env:
144+
REF: ${{ steps.set-repo-and-ref.outputs.ref }}
145+
REPO: ${{ steps.set-repo-and-ref.outputs.repo }}
146+
REPO_ID: ${{ steps.set-repo-and-ref.outputs.repo-id }}
147+
shell: bash
148+
working-directory: action-repo
149+
- name: Run Docker container
150+
uses: ./action-repo/.github/actions/run-docker-container
151+
with:
152+
user: ${{ inputs.user }}
153+
password: ${{ inputs.password }}
154+
repository-url: ${{ inputs.repository-url || inputs.repository_url }}
155+
packages-dir: ${{ inputs.packages-dir || inputs.packages_dir }}
156+
verify-metadata: ${{ inputs.verify-metadata || inputs.verify_metadata }}
157+
skip-existing: ${{ inputs.skip-existing || inputs.skip_existing }}
158+
verbose: ${{ inputs.verbose }}
159+
print-hash: ${{ inputs.print-hash || inputs.print_hash }}
160+
attestations: ${{ inputs.attestations }}

create-docker-action.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import json
2+
import os
3+
import pathlib
4+
5+
DESCRIPTION = 'description'
6+
REQUIRED = 'required'
7+
8+
REF = os.environ['REF']
9+
REPO = os.environ['REPO']
10+
REPO_ID = os.environ['REPO_ID']
11+
REPO_ID_GH_ACTION = '178055147'
12+
13+
14+
def set_image(ref: str, repo: str, repo_id: str) -> str:
15+
if repo_id == REPO_ID_GH_ACTION:
16+
return '../../../Dockerfile'
17+
docker_ref = ref.replace('/', '-')
18+
return f'docker://ghcr.io/{repo}:{docker_ref}'
19+
20+
21+
image = set_image(REF, REPO, REPO_ID)
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+
},
71+
}
72+
73+
action_path = pathlib.Path('.github/actions/run-docker-container/action.yml')
74+
action_path.parent.mkdir(parents=True, exist_ok=True)
75+
action_path.write_text(json.dumps(action, ensure_ascii=False), encoding='utf-8')

0 commit comments

Comments
 (0)