在Gitlab中,我如何以编程方式下载CI管道末尾发布的人工制品。
通过UI轻松下载它,但是如何通过API获取它。换句话说,是否可以通过令牌或类似方式访问它?
答案 0 :(得分:2)
这对我有用:
#!/bin/bash
GITLAB_URL="https://gitlab.example.com"
GITLAB_ARTIFACT_TOKEN="<token>"
group="<group>"
project="<project>"
branch="<branch>"
job="<job>"
outZipFile="$project.zip"
outHeadersFile="$outZipFile.httpheaders"
etagArgs=()
# The following is unfortunately not yet supported by GitLab; the returned Etag never changes:
#if [[ -f "$outHeadersFile" ]] && [[ -f "$outZipFile" ]]; then
# etag=$(grep etag < "$outHeadersFile" | cut -f2 -d' ')
# if [[ -n "$etag" ]]; then
# etagArgs=("--header" "If-None-Match: $etag")
# echo "using etag: $etag"
# fi
#fi
response=$(curl "$GITLAB_URL/api/v4/projects/${group}%2F${project}/jobs/artifacts/$branch/download?job=$job" \
--silent \
-w "%{http_code}\n" \
-D "$outHeadersFile" \
-o "$outZipFile.tmp" \
--header "PRIVATE-TOKEN: $GITLAB_ARTIFACT_TOKEN" \
"${etagArgs[@]}")
if [[ "$response" == 4* ]] || [[ "$response" == 5* ]]; then
echo "ERROR - Http status: $response"
rm "$outZipFile.tmp"
exit 1
elif [[ "$response" == 304 ]]; then
echo "$project is up-to-date"
else
echo "update $outZipFile"
mv "$outZipFile.tmp" "$outZipFile"
fi
答案 1 :(得分:1)
可以通过https://docs.gitlab.com/ee/api/jobs.html#get-job-artifacts
中的API获取/ projects /:id / jobs /:job_id / artifacts
示例请求:
使用PRIVATE-TOKEN标头:
curl --location --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/1/jobs/8/artifacts"
使用JOB-TOKEN标头(仅在.gitlab-ci.yml内部):
curl --location --header "JOB-TOKEN: $CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/8/artifacts"
使用job_token参数(仅在.gitlab-ci.yml内部):
curl --location --form "job-token=$CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/8/artifacts"
答案 2 :(得分:1)
这对我有用。
curl --location --header "PRIVATE-TOKEN:MY_PRIVATE_TOKEN" “https://it-gitlab.cloud.net/api/v4/projects/projectId/jobs/jobId/artifacts” --输出观察者