-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build Docker image and push to GHCR #230
base: unstable/v1
Are you sure you want to change the base?
Conversation
This looks.. intriguing! I don't remember if I ever considered combining composite+docker actions (I did play with having two composites in the same repo in the past, though). I'll need to take some time to think about it and look through the patch more closely. Please, don't expect an immediate review, however it does look very promising at glance! Originally I thought that I'd have a workflow where I trigger a release, that release adds a commit that hardcodes an update to This looks like a better idea so far. Thanks again! |
That sounds great. Take your time. Thanks for your consideration. If you do decide to accept this change, I'm happy to help maintain the workflows in the future. Feel free to mention me @br3ndonland and I will help address any issues that come up. |
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.
Thanks! I've hit "rebase" on the UI to get this on top of the recent changes/linting/lockfile bumps but haven't yet looked into it deeper. |
--tag $IMAGE | ||
- name: Log in to GHCR | ||
if: github.event_name != 'pull_request' | ||
run: | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually prefer this syntax so that the whole thing is parsed as a single line:
run: | | |
run: >- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally I don't use run: >-
because it will fold the contents of the block into a single line. This can alter the behavior of the script within the block. On this line though, it makes no difference, so sure.
args: | ||
- ${{ inputs.user }} | ||
- ${{ inputs.password }} | ||
- ${{ inputs.repository-url }} | ||
- ${{ inputs.packages-dir }} | ||
- ${{ inputs.verify-metadata }} | ||
- ${{ inputs.skip-existing }} | ||
- ${{ inputs.verbose }} | ||
- ${{ inputs.print-hash }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These aren't actually used. Somebody contributed them, but I don't see them being needed. So perhaps we shouldn't keep them around anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't the inputs used by the entrypoint script?
gh-action-pypi-publish/twine-upload.sh
Lines 37 to 41 in 699cd61
INPUT_REPOSITORY_URL="$(get-normalized-input 'repository-url')" | |
INPUT_PACKAGES_DIR="$(get-normalized-input 'packages-dir')" | |
INPUT_VERIFY_METADATA="$(get-normalized-input 'verify-metadata')" | |
INPUT_SKIP_EXISTING="$(get-normalized-input 'skip-existing')" | |
INPUT_PRINT_HASH="$(get-normalized-input 'print-hash')" |
push: | ||
branches: ["release/*", "unstable/*"] | ||
tags: ["*"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we push the tag first, there's going to be some time when the new version is out already, but the container doesn't actually exist yet. Therefore, I prefer building my release automation processes around the workflow_dispatch
trigger, so that I'm able to type in the target version in the UI, get the image published and only then push a tag, only when it's known that the image exists already. Let's try reworking this with similar philosophy in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying. I've replaced the tags trigger with workflow_dispatch
instead (049447a).
pull_request: | ||
workflow_run: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two separate workflow runs is often hard to track. Instead, I adopted a practice of modularizing the workflow pieces as reusable workflows having the reusable-
prefix in their names. This allows embedding everything in all the right places. Let's try this, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will return to this suggestion at a later time.
uses: actions/checkout@v3 | ||
with: | ||
path: test | ||
uses: actions/checkout@v4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not make version upgrades in the same PR? And I'd rather not change the testing logic/dirs unless it's required somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll stick with actions/checkout@v3
and keep the previous test
path.
.pre-commit-config.yaml
Outdated
@@ -31,7 +31,7 @@ repos: | |||
args: | |||
- --builtin-schema | |||
- github-workflows-require-timeout | |||
files: ^\.github/workflows/[^/]+$ | |||
files: ^\.github\/workflows/[^/]+$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you submit this separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this change. The existing regex (^\.github/workflows/[^/]+$
) seems like it should work fine, but pre-commit was matching files in .github/actions/
and raising errors. Dynamically generating the entire action.yml
as you suggested will avoid the problem.
Dockerfile
Outdated
@@ -3,6 +3,7 @@ FROM python:3.12-slim | |||
LABEL "maintainer" "Sviatoslav Sydorenko <wk+pypa@sydorenko.org.ua>" | |||
LABEL "repository" "https://github.com/pypa/gh-action-pypi-publish" | |||
LABEL "homepage" "https://github.com/pypa/gh-action-pypi-publish" | |||
LABEL "org.opencontainers.image.source" "https://github.com/pypa/gh-action-pypi-publish" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd accept this in a separate PR even before the rest is figured out/refactored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to separate PR (#241).
steps: | ||
- name: Reset path if needed | ||
run: | | ||
# Reset path if needed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this be needed outside the container?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because you've set up a test that modifies the $PATH
(#112, 1350b8b).
gh-action-pypi-publish/.github/workflows/self-smoke-test-action.yml
Lines 85 to 90 in 699cd61
- name: ✅ Smoke-test the locally checked out action | |
uses: ./test | |
env: | |
DEBUG: >- | |
true | |
PATH: utter-nonsense |
REF=${{ env.ACTION_REF || github.ref_name }} | ||
REPO=${{ env.ACTION_REPO || github.repository }} | ||
echo "ref=$REF" >>"$GITHUB_OUTPUT" | ||
echo "repo=$REPO" >>"$GITHUB_OUTPUT" | ||
shell: bash | ||
env: | ||
ACTION_REF: ${{ github.action_ref }} | ||
ACTION_REPO: ${{ github.action_repository }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it easier to do it like this?
REF=${{ env.ACTION_REF || github.ref_name }} | |
REPO=${{ env.ACTION_REPO || github.repository }} | |
echo "ref=$REF" >>"$GITHUB_OUTPUT" | |
echo "repo=$REPO" >>"$GITHUB_OUTPUT" | |
shell: bash | |
env: | |
ACTION_REF: ${{ github.action_ref }} | |
ACTION_REPO: ${{ github.action_repository }} | |
REF=${{ github.action_ref || github.ref_name }} | |
REPO=${{ github.action_repository || github.repository }} | |
echo "ref=${REF}" >>"${GITHUB_OUTPUT}" | |
echo "repo=${REPO}" >>"${GITHUB_OUTPUT}" | |
shell: bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would think so, but the syntax is needed to work around a limitation of composite actions. See:
action.yml
Outdated
- name: Set Docker image name and tag | ||
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" | ||
shell: bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I was thinking… Why do we need to check in action.yml
to Git even if this modifies it anyway? Why not generate it all, then?
Also, I'd use Python instead of Bash here. Then, it'd be possible to have a dict with data and write it as YAML using json.dump()
(because JSON is valid YAML, almost always).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could work. I've added a Python script that generates the Docker action (9ae1850).
Regarding your related comment:
I think that generating the file is a good idea. It should be possible to write the file without bringing in the PyYAML dependency. But it's not that easy for reading it. Can we make use of yq somehow, and convert YAML to JSON this way, maybe?
I've started by using PyYAML but will think about a way to do this with yq
.
user: ${{ inputs.user }} | ||
password: ${{ inputs.password }} | ||
repository-url: ${{ inputs.repository-url || inputs.repository_url }} | ||
packages-dir: ${{ inputs.packages-dir || inputs.packages_dir }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would break the deprecation messages, it seems. Have you checked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. The new inputs don't have defaults, so for example, ${{ inputs.repository-url || inputs.repository_url }}
will default to inputs.repository_url
and deprecationMessage
will be logged.
I'm done with the initial review. More is needed, but I'd rather accept what I can through separate PRs to make this one smaller. And the suggested refactoring could be done in parallel. I think that generating the file is a good idea. It should be possible to write the file without bringing in the PyYAML dependency. But it's not that easy for reading it. Can we make use of |
@webknjaz thank you for your detailed review. I've addressed most of your comments so far. |
Description
Closes #58
Up to this point, the project has been set up as a Docker action referencing the Dockerfile.
gh-action-pypi-publish/action.yml
Lines 86 to 88 in 3fbcf7c
The downside to using the Dockerfile for the action is that the Docker image must be built every time the action is used (#58).
This PR 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
Build container image with GitHub Actions
This PR will build Docker images with the Docker CLI (
docker build
). Builds will include inline cache metadata so layers can be reused by future builds.This PR only proposes to build container images for
x86_64
(linux/amd64
) because GitHub Actions Linux runners currently only supportx86_64
CPU architectures (actions/runner-images#5631), and this project only supports GitHub Actions Linux runners. The README explains:Push container image to GHCR
The workflow will log in to GHCR using the built-in GitHub token and push the Docker image. Workflow runs triggered by pull requests will build the Docker image and run the smoke tests but will not push the Docker image.
Update action to pull container image from GHCR
Docker actions support pulling in pre-built Docker images by supplying a registry address to the
image:
key. The downside to this syntax is that there's no way to specify the correct Docker tag because the GitHub Actionsimage:
anduses:
keys don't accept any context. For example, if a user's workflow hasuses: pypa/gh-action-pypi-publish@release/v1.8
, then the action should pull in a Docker image built from therelease/v1.8
ref, something likeghcr.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.Related
github.action_repository
andgithub.action_ref
are empty inrun
for composite actions actions/runner#2473