Skip to content

Commit 104bf53

Browse files
committed
Build Docker image and push to GHCR
Up to this point, the project has been set up as a Docker action referencing the Dockerfile. The downside to using the Dockerfile for the action is that the Docker image must be built every time the action is used. This commit will set up the project to build the Docker image and push it to GitHub Container Registry (GHCR). This change will speed up user workflows every time the action is used because the workflows will simply pull the Docker image from GHCR instead of building again. Changes: - Add required metadata to Dockerfile - Build container image with GitHub Actions - Push container image to GHCR Docker actions support pulling in pre-built Docker images. The downside is that there's no way to specify the correct Docker tag because the GitHub Actions `image` and `uses:` keys don't accept any context. For example, if a user's workflow has `uses: pypa/gh-action-pypi-publish@release/v1.8`, then the action should pull in a Docker image built from the `release/v1.8` branch, something like `ghcr.io/pypa/gh-action-pypi-publish:release-v1.8` (Docker tags can't have `/`). The workaround is to switch the top-level `action.yml` to a composite action that then calls the Docker action, substituting the correct image name and tag.
1 parent f760068 commit 104bf53

File tree

4 files changed

+124
-13
lines changed

4 files changed

+124
-13
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: 🏃
3+
inputs:
4+
user:
5+
required: false
6+
password:
7+
required: false
8+
repository-url:
9+
required: false
10+
packages-dir:
11+
required: false
12+
verify-metadata:
13+
required: false
14+
skip-existing:
15+
required: false
16+
verbose:
17+
required: false
18+
print-hash:
19+
required: false
20+
runs:
21+
using: docker
22+
image: {{image}}
23+
args:
24+
- ${{ inputs.user }}
25+
- ${{ inputs.password }}
26+
- ${{ inputs.repository-url }}
27+
- ${{ inputs.packages-dir }}
28+
- ${{ inputs.verify-metadata }}
29+
- ${{ inputs.skip-existing }}
30+
- ${{ inputs.verbose }}
31+
- ${{ inputs.print-hash }}
32+
- ${{ inputs.attestations }}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
3+
name: 🏗️
4+
5+
on: # yamllint disable-line rule:truthy
6+
pull_request:
7+
push:
8+
branches: ["release/*", "unstable/*"]
9+
tags: ["*"]
10+
11+
jobs:
12+
build-and-push:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Build Docker image
17+
run: |
18+
IMAGE="ghcr.io/$GITHUB_REPOSITORY:${GITHUB_REF_NAME/'/'/'-'}"
19+
echo "IMAGE=$IMAGE" >>"$GITHUB_ENV"
20+
docker build . \
21+
--build-arg BUILDKIT_INLINE_CACHE=1 \
22+
--cache-from $IMAGE \
23+
--tag $IMAGE
24+
- name: Push Docker image to GHCR
25+
if: github.event_name != 'pull_request'
26+
run: |
27+
echo ${{ secrets.GITHUB_TOKEN }} |
28+
docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
29+
docker push $IMAGE

.github/workflows/self-smoke-test-action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
name: 🧪
44

55
on: # yamllint disable-line rule:truthy
6-
push:
76
pull_request:
7+
workflow_run:
8+
workflows: [🏗️]
9+
types: [completed]
810

911
env:
1012
devpi-password: abcd1234
@@ -28,6 +30,9 @@ env:
2830
2931
jobs:
3032
smoke-test:
33+
if: >-
34+
github.event_name == 'pull_request' ||
35+
github.event.workflow_run.conclusion == 'success'
3136
runs-on: ubuntu-latest
3237

3338
services:

action.yml

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,60 @@ 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: Reset path if needed
97+
run: |
98+
# Reset path if needed
99+
# https://github.com/pypa/gh-action-pypi-publish/issues/112
100+
if [[ $PATH != *"/usr/bin"* ]]; then
101+
echo "\$PATH=$PATH. Resetting \$PATH for GitHub Actions."
102+
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
103+
echo "PATH=$PATH" >>"$GITHUB_ENV"
104+
echo "$PATH" >>"$GITHUB_PATH"
105+
echo "\$PATH reset. \$PATH=$PATH"
106+
fi
107+
shell: bash
108+
- name: Set repo and ref from which to run Docker container action
109+
id: set-repo-and-ref
110+
run: |
111+
# Set repo and ref from which to run Docker container action
112+
# to handle cases in which `github.action_` context is not set
113+
# https://github.com/actions/runner/issues/2473
114+
REF=${{ env.ACTION_REF || github.ref_name }}
115+
REPO=${{ env.ACTION_REPO || github.repository }}
116+
echo "ref=$REF" >>"$GITHUB_OUTPUT"
117+
echo "repo=$REPO" >>"$GITHUB_OUTPUT"
118+
shell: bash
119+
env:
120+
ACTION_REF: ${{ github.action_ref }}
121+
ACTION_REPO: ${{ github.action_repository }}
122+
- name: Set Docker image name and tag
123+
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"
138+
shell: bash
139+
- name: Run Docker container
140+
uses: ./.github/actions/run-docker-container
141+
with:
142+
user: ${{ inputs.user }}
143+
password: ${{ inputs.password }}
144+
repository-url: ${{ inputs.repository-url || inputs.repository_url }}
145+
packages-dir: ${{ inputs.packages-dir || inputs.packages_dir }}
146+
verify-metadata: ${{ inputs.verify-metadata || inputs.verify_metadata }}
147+
skip-existing: ${{ inputs.skip-existing || inputs.skip_existing }}
148+
verbose: ${{ inputs.verbose }}
149+
print-hash: ${{ inputs.print-hash || inputs.print_hash }}
150+
attestations: ${{ inputs.attestations }}

0 commit comments

Comments
 (0)