I have a gitlab-ci.yml file that creates a maven deployment and am trying to trigger another pipeline that takes the package from the registry and create a docker image
image: maven:latest
before_script:
- mvn --version
stages:
- build
- trigger
build:
stage: build
script:
- mvn package
- mvn deploy
artifacts:
paths:
- target/*
trigger:
stage: trigger
trigger:
project: my/other/project/repo
branch: master
I'm having trouble finding out just how to get that package (or published artifacts) to the triggered pipeline. When using the container registry I just specify the image and gitlab pulls it down, not sure if I'm missing something obvious here but how do I actually use/download a package from gitlab?
I've tried to curl files in the package registry like this
curl --header "PRIVATE-TOKEN: my_token" "https://gitlab.com/api/v4/projects/00112233/packages/generic/io/my/package/0.0.1-SNAPSHOT/myfile.xml"
which throws the following error
{"error":"package_version is invalid"}
But when I curl GET on the package that is the package version
[
{
"id": 123456,
"name": "io/my/package",
"version": "0.0.1-SNAPSHOT",
"package_type": "maven",
"status": "default",
"_links": {
"web_path": "/my/web/path/-/packages/00112233",
"delete_api_path": "https://gitlab.com/api/v4/projects/0123456/packages/00112233"
},
"created_at": "2022-01-11T23:59:28.626Z",
"tags": [
]
}
]
Is it possible to use or download the packages in gitlab in another pipeline triggered?
.gitlab-ci.yml
image: maven:latest
before_script:
- mvn --version
variables:
PARENT_JOB_ID: myteststring # This works
# PARENT_JOB_ID: $CI_JOB_ID # This doesn't
stages:
- build
- trigger
build:
stage: build
script:
- mvn package
- mvn deploy
trigger:
stage: trigger
trigger:
include: my_ci.yml
my_ci.yml
image:
name: docker:20
services:
- docker:dind
stages:
- test
test:
stage: test
script:
- echo $JOB_ID # echos nothing if $CI_JOB_ID is $PARENT_JOB_ID, echos the string if it's something else
========================
Output from job when PARENT_JOB_ID is set to a random string
Executing "step_script" stage of the job script
00:00
Using docker image sha256:15a9bc7c6340df2ac9d6c8196ca1d905180ddf2ca8b29a8d98f5422e2e5ccf85 for docker:20 with digest docker@sha256:a729cce205a05b0b86dc8dca87823efaffc3f74979fe7dc86a707c2fbf631b61 ...
$ echo "Test string = $PARENT_JOB_ID"
Test string = myteststring
Output from job when PARENT_JOB_ID is set to CI_JOB_ID
Executing "step_script" stage of the job script
00:00
Using docker image sha256:15a9bc7c6340df2ac9d6c8196ca1d905180ddf2ca8b29a8d98f5422e2e5ccf85 for docker:20 with digest docker@sha256:a729cce205a05b0b86dc8dca87823efaffc3f74979fe7dc86a707c2fbf631b61 ...
$ echo "Test string = $PARENT_JOB_ID"
Test string =
Maybe you are using the wrong API for the situation. Since you store it in artifacts. https://docs.gitlab.com/ee/api/job_artifacts.html#get-job-artifacts
GET /projects/:id/jobs/:job_id/artifacts
Before we use the the artifact API we firstly need the id of the job, which contains the latest artifacts, from the parent pipeline. To get this firstly we will find the id of the last pipeline (https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines)
GET /projects/:id/pipelines
Command:
PIPELINE_ID=$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/pipelines" | jq '.[0].id')
Next, we will use the pipeline id to fetch the job id by specifying the job name. In your case build .To achieve this we will use the Gitlab API https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-jobs
GET /projects/:id/pipelines
Command:
PARENT_JOB_ID=$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/pipelines/$PIPELINE_ID/jobs" | jq '.[] | select(.name=="build") | .id')
Combining all this with the artifacts API we get the following code for the child pipeline code, my_ci.yml in your case:
image:
name: docker:20
services:
- docker:dind
stages:
- test
test:
stage: test
script:
- apk add jq curl
- |
PIPELINE_ID=$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/pipelines" | jq '.[0].id')
PARENT_JOB_ID=$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/pipelines/$PIPELINE_ID/jobs" | jq '.[] | select(.name=="build") | .id')
wget -U "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.17 (KHTML,like Gecko) Ubuntu/11.04 Chromium/11.0.654.0 Chrome/11.0.654.0 Safari/534.17" --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/jobs/$PARENT_JOB_ID/artifacts" -O artifacts.zip
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With