Use an artifact from a project, in a different project's CI

ProjectA has created an artifact, projectB needs this artifact to compile. How do you implement retrieving projectA’s artifact in the gitlab-ci.ymal for projectB?

Thanks in advance
Bob

4 Likes

Looks like it isn’t possible yet, but there’s an open feature request here. Seems to be a popular idea!

For us, we’ve cheated and used Docker images. It compiles the file into a new Docker container and pushes that to the registry, and then we can import that for multiple other projects. It suits our workflow well because we don’t compile “ProjectA” very often. For your situation it could add a lengthy compile time to your workflow, so it may not work as well for you.

I’d recommend adding a +1 to the above issue though, and subscribe to get updated if it’s implemented.

Thanks Chris.
I see that there is an API call to get an artifact from a repository, but I don’t know how to use it. I have been in windows to long!.
Bob

1 Like

Yeah, it looks a bit messy. Seems like you’d need to get a list of the latest builds for a project (so you can obtain the build_id), and then use that to pull the latest artifacts.
https://docs.gitlab.com/ee/api/builds.html#list-project-builds
https://docs.gitlab.com/ee/api/builds.html#get-build-artifacts
(Additional verification would be needed here, such as skipping any builds which have failed).

It does seem possible though. Not sure what language you’re working with so I’m not sure of the best way to interact with the API.

The advantage of the Docker workaround is we only push to the “latest” branch if the build and tests completed successfully, so I can mess around with the project without it affecting anything which has to actually use it as a dependency.

Until there is an easier method, I decided on pushing the required dll’s to a local nugget server (done in the ci ymal for the dll build). Then add the dll’s using nugget, to the project that requires them. This way when I run nugget restore in the ci ymal, the latest built dll’s will be pulled and used. I think this is the easiest way to attack the problem for a windows, visual studio, c#, WPF, and desktop application.

Bob

2 Likes

Hello
Do anyone knows if there is an easier way to download artifacts from different project, other than described by chrisatomix 3 years ago?

The use case is:

project A is publishing artifact:

artifacts:
name: “artifact-${CI_COMMIT_SHORT_SHA}.tar”

project B would like to download artifact for specific commit, without knowing JOB_ID that was used to build artifact-${CI_COMMIT_SHORT_SHA}.tar

So far I found a way to download latest artifact, without providing JOB_ID.
Sometimes I need to reuse already builded artifact, and I think building it again it is a waste of time

Michal.

I just found an answer, so if anyone had similar issue:

Below url will download most recent artifact for master branch

https://gitlab.com/c33s-group/yaml-convert/-/jobs/artifacts/master/download?job=build

but we can change branch to CI_COMMIT_SHORT_SHA and download artifact for specific commit

https://gitlab.com/c33s-group/yaml-convert/-/jobs/artifacts/79dd08b9/download?job=build
4 Likes

Which GitLab API is this (link to page please) I’ve been trying this against a locally hosted GitLab system (12.7) and cannot make it work.

1 Like

I’m working now on retrieving artifacts between jobs.
I think it works only for public projects.
see doc here https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html
source answer here https://stackoverflow.com/questions/56233243/gitlab-ci-get-last-artifact
Note: between jobs better using cache instead artifacts

1 Like

Your best option is to use the Job Artifacts API, they are fairly simple to implement. You can create a User Access Token to retrieve job artifacts; what I have done is stored a User Access Token as a protected variable in my CI/CD settings for the projects which will fetch an artifact.

One thing I have learned is that you need to review the Job Artifacts API carefully. Some of them will attempt to pull artifacts from the most recently successful pipeline, like the Download a single artifact file from specific tag or branch API. For this API, if your most recent pipeline executes successfully (to completion) but the job you are fetching artifact(s) for does not produce the artifact(s), you will get a 404 response. For the same API, if your most recent pipeline executes successfully (to completion) but the job you are fetching artifact(s) for does not execute at all, you will get a 400 response.

The response codes are not always the most useful; I struggled for a few hours debugging why my setup was failing even though I had successfully tested it an hour before. I assumed the API would find the last pipeline for which the job successfully executed and produced an artifact. It does not check for this condition, it only looks for the last successful pipeline execution.

I guess this can be used with cURL´, wget`, CMake routines for downloading files etc. Have you tried it?