从 Gitlab 包注册表下载包
Downloading a package from Gitlab package registry
我有一个创建 Maven 部署的 gitlab-ci.yml 文件,我正在尝试触发另一个从注册表中获取包并创建 docker 图像
的管道
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
我无法确定如何将该包(或已发布的工件)发送到触发的管道。使用容器注册表时,我只指定图像,gitlab 将其拉下来,不确定我是否遗漏了一些明显的东西,但我实际上如何 use/download 来自 gitlab 的包?
我试过像这样在包注册表中卷曲文件
curl --header "PRIVATE-TOKEN: my_token" "https://gitlab.com/api/v4/projects/00112233/packages/generic/io/my/package/0.0.1-SNAPSHOT/myfile.xml"
抛出以下错误
{"error":"package_version is invalid"}
但是当我在包版本上卷曲 GET 时
[
{
"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": [
]
}
]
是否可以在触发的另一个管道中使用或下载gitlab中的包?
附加代码:
.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
我的_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
========================
PARENT_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 = myteststring
当 PARENT_JOB_ID 设置为 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 =
也许您对这种情况使用了错误的 API。由于您将其存储在工件中。 https://docs.gitlab.com/ee/api/job_artifacts.html#get-job-artifacts
GET /projects/:id/jobs/:job_id/artifacts
在我们使用工件 API 之前,我们首先需要作业的 ID,其中包含来自父管道的最新工件。为了首先得到这个,我们将找到最后一个管道的 id (https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines)
GET /projects/:id/pipelines
命令:
PIPELINE_ID=$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/pipelines" | jq '.[0].id')
接下来,我们将使用管道 ID 通过指定作业名称来获取 作业 ID。在你的情况下 build 。为了实现这一点,我们将使用 Gitlab API https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-jobs
GET /projects/:id/pipelines
命令:
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')
将所有这些与工件 API 结合起来,我们得到以下子管道代码的代码,在您的例子中是 my_ci.yml:
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
我有一个创建 Maven 部署的 gitlab-ci.yml 文件,我正在尝试触发另一个从注册表中获取包并创建 docker 图像
的管道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
我无法确定如何将该包(或已发布的工件)发送到触发的管道。使用容器注册表时,我只指定图像,gitlab 将其拉下来,不确定我是否遗漏了一些明显的东西,但我实际上如何 use/download 来自 gitlab 的包?
我试过像这样在包注册表中卷曲文件
curl --header "PRIVATE-TOKEN: my_token" "https://gitlab.com/api/v4/projects/00112233/packages/generic/io/my/package/0.0.1-SNAPSHOT/myfile.xml"
抛出以下错误
{"error":"package_version is invalid"}
但是当我在包版本上卷曲 GET 时
[
{
"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": [
]
}
]
是否可以在触发的另一个管道中使用或下载gitlab中的包?
附加代码:
.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
我的_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
========================
PARENT_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 = myteststring
当 PARENT_JOB_ID 设置为 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 =
也许您对这种情况使用了错误的 API。由于您将其存储在工件中。 https://docs.gitlab.com/ee/api/job_artifacts.html#get-job-artifacts
GET /projects/:id/jobs/:job_id/artifacts
在我们使用工件 API 之前,我们首先需要作业的 ID,其中包含来自父管道的最新工件。为了首先得到这个,我们将找到最后一个管道的 id (https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines)
GET /projects/:id/pipelines
命令:
PIPELINE_ID=$(curl -s --header "PRIVATE-TOKEN: <access_token>" "https://gitlab.com/api/v4/projects/<project_id>/pipelines" | jq '.[0].id')
接下来,我们将使用管道 ID 通过指定作业名称来获取 作业 ID。在你的情况下 build 。为了实现这一点,我们将使用 Gitlab API https://docs.gitlab.com/ee/api/jobs.html#list-pipeline-jobs
GET /projects/:id/pipelines
命令:
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')
将所有这些与工件 API 结合起来,我们得到以下子管道代码的代码,在您的例子中是 my_ci.yml:
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