Skip to content

Remove "pull_request_target" workflows and replace them with storing images as artifacts #43268

Closed

Description

@potiuk

I've learned that as of artifact@v4 APIs for GitHub Actions, it should be possible to get rid of the GitHub Registry use as CI cache.

Note

I think that change might be way smaller. We should be able to continue using Github Registry for both - latest image and image cache, and only change CI to pull artifacts from latest "canary" build and build image cache from that.

In our CI/CD pipelines we are using "pull_request_target" workflow in order to be able to publish images to GitHub registry from Pull Requests coming from forks. This is done in order to speed up the workflows - our images take a lot of time to build and by reusing the cache, we are saving a lot of build time.

However after following a discussion in Apache Software Foundation #builds channel https://the-asf.slack.com/archives/CF6PY5M4N/p1729595424091089 - with Pulsar and Netbeans CI/CD maintainers, it looks like we have a viable alternative - that will remove the need of using GH Registry for PRs - instead we could use artifacts.

Our images are quite big (2GB) but Pulsar team successfully uses artifacts to store 1.7GB ones, so this should be quite possible for us to use the artifacts instead of registry.

We dicussed also - with Pulsar and Netbeans CI maintainers about caching strategy we use and it seems entirely doable to switch to artifacts and get the same effect, without the need of having "pull_request_target" workflow. That would be a major win for security of our workflows to switch to it.

This is especially atractive because the new artifact API v4 introduced in February 2024 is very fast, implements automated chunking of binaries uploaded/downloaded and parallell uploads/downloads - which according to Pulsar/Netbeans team had a huge impact on upload/download times for the images - see https://github.blog/news-insights/product-news/get-started-with-v4-of-github-actions-artifacts/ - there are a lot of details in the post, but the way uploading artifacts works with v4 is that you directly interact with Azure Blob Storage, the files are chunked etc. The examples they show in the blog post is speeding up the upload of 1GB file from 2 m to 3 s.

Also v4 when run-id and other parameters have been added - making it suitable to implement similar caching solution we have, where artifacts from "canary" build could be used by all PRs as source of their image cache.

Seems that artifacts have no individual size limits and they also do not have "total limits" with the ASF "Enterprise Account" level - there are things

The way it should work:

All workflows - PRs from apache and fork work in the same way:

  1. Pull latest image (stored as artifact) from latest "main" successful build (we can use GH API to find the run-id to pull it from and the download-artifact@v4 action already supports pulling artifacts from other workflows and from other runs.

  2. The PRs first unpack and push the "main" image to be present in local docker registry - no need to have GH Registry "PUSH" access.

  3. The PRs then build the image locally not using --cache-from but to use such local registry cache. Achieving basically the same (or even bettter) caching speedups comparing to what we have now.

  4. The successful "canary" build artifact could be uploaded at "finalize" time to become the new source of cache for all the PRs.

  5. Breeze locak CI image could also be modified to pull latest image when rebuilding the image forcefully - otherwise rely on locally built images.

  6. The --image-tag option of the local breeze (which pulls the image from a specific workflow run) could be easily adapted to be changed to --pr PR# - allowing to replicate a failure from specific PR locally without looking up the image.

  7. The artifacts can have arbitrary set expiry time, so they could be available for a few days after the PR or "canary" build completed.

The benefits:

  • we get rid of "Build image" "pull_request_target" workflow - that would be a huge security improvement
  • we get rid of the need of creating new images every time we create a new "release" branch.
  • we get rid of GitHub Registry
  • all the runs - commiter and non-commiter, from fork or apache would be identical, making it much easier to test changes in workflows
  • looking at the upload/download stats for artifacts, it is likely that switching to artifacts might get some speedup comparing to using the registry

We can use https://github.com/assignUser/stash from Jacob Wujciak (ASF member) to make things easier. This will soon be available as Apache-wide action: apache/infrastructure-actions#32

Activity

  1. converted this from a draft issue on Oct 22, 2024
  2. added
    area:CI
    area:dev-env
    on Oct 22, 2024
  3. moved this from Backlog to Ready in CI / DEV ENV planned workon Nov 30, 2024
  4. gopidesupavan commented on Dec 10, 2024

    @gopidesupavan
    Member

    Going to take a stab on this , when ever i open CI/CD project pull_request_target reminds me its dangerous .... :)

  5. gopidesupavan commented on Dec 14, 2024

    @gopidesupavan
    Member

    @potiuk

    Before actually dive into the code, i just tried the above flow with simple docker image. sorry if i am confusing too much :)

    The first flow option 1 is same as above steps

    Option1:

    Build the docker image and pushed to artifactory
    Download the image from artifactory and unpack and push to local registry
    Build from the local registry.
    

    Looks like this is not using the existing image from the local registry. instead its building again.

    RUN: https://github.com/gopidesupavan/prt-removal/actions/runs/12329425958/job/34413743618#step:9:208
    Workflow: https://github.com/gopidesupavan/prt-removal/blob/main/.github/workflows/build-push-reuse.yml

    After searching further, it seems docker buildx not loading image that exported from sources(tar file). docker/buildx#847

    And as per docker buildx it seems the way is to use named context https://docs.docker.com/build/ci/github-actions/named-contexts/

    Option2:

     Build the docker image with inline cache and pushed to artifactory
     Download the image from artifactory and unpack and push to local registry
     Build from the local registry with inline cache.
    

    It works well with the inline cache. It uses the existing image from the local registry and not building any layers again. as
    there are no changes in the docker file.
    RUN: https://github.com/gopidesupavan/prt-removal/actions/runs/12329496451/job/34413888753#step:9:193
    Workflow: https://github.com/gopidesupavan/prt-removal/blob/main/.github/workflows/with-inline-cache.yml

    Option3:

    Build the docker image with local cache and pushed cache dir to artifactory
    Download the cache from artifactory to tmp dir
    Build the image with local cache referring to the local cache dir. (in pr flow)
    

    It also works well with the local cache type. all the layers from the local cache dir and not building any layers again.
    RUN: https://github.com/gopidesupavan/prt-removal/actions/runs/12329584735/job/34414063998#step:7:193
    Workflow: https://github.com/gopidesupavan/prt-removal/blob/main/.github/workflows/with-local-cache.yml

    Consider that in the above every option first two steps executed in the schedule run. push the schedule run image and use it pr flows.

    So basically i tried to mimic the scenario. in each workflow that i used above first job is in the context of schedule run and next job is pr flow.

    Unable to fully evaluate the side effects of the inline cache. IMHO, the last option seems similar to what we aim to achieve, but instead of pushing the image, we could push all the local caches for the different images (3.9, 3.10, 3.11, 3.12) built during the scheduled run. These cache artifacts (using the run-id as you suggested) could then be downloaded and utilized in the PR workflow.

  6. gopidesupavan commented on Dec 14, 2024

    @gopidesupavan
    Member

    My hunch is that the builder context is being lost when the job exits. When we download the artifact and attempt to use it, it likely creates a completely new builder context. As a result, it tries to rebuild everything from scratch instead of utilizing the downloaded image from the artifact.

  7. moved this from Ready to In progress in CI / DEV ENV planned workon Dec 24, 2024
  8. potiuk commented on Dec 28, 2024

    @potiuk
    MemberAuthor

    My hunch is that the builder context is being lost when the job exits. When we download the artifact and attempt to use it, it likely creates a completely new builder context. As a result, it tries to rebuild everything from scratch instead of utilizing the downloaded image from the artifact.

    I have another idea to check - exploring it while working on #42999 and #44511... Stay tuned :)

  9. 18 remaining items

  10. removed their assignment
    on Dec 29, 2024
  11. gopidesupavan commented on Dec 29, 2024

    @gopidesupavan
    Member

    Woohoooo nice :)

  12. potiuk commented on Dec 29, 2024

    @potiuk
    MemberAuthor

    Indeed :).. Let's see how many new problems it brings :)

  13. added a commit that references this issue on Dec 29, 2024
    9a4d14c
  14. added a commit that references this issue on Jan 16, 2025
    fa55cab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:CIarea:dev-env

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions