在 Gitlab CI 中读取 webhook 负载 [英] Read webhook payload in Gitlab CI
问题描述
我有一个通过 webhook 触发的项目 (PROJECT_A
),并希望设置变量 $PRODUCT
.它的值用于触发构建中的某个路径..gitlab-ci.yml
文件中的作业如下所示:
I have a project (PROJECT_A
) that is triggered through a webhook, and expects the variable $PRODUCT
to be set. Its value is used to trigger a certain path in the build. The job in the .gitlab-ci.yml
file looks like this:
deploy:
stage: publish
script:
- ./generate_doc.sh $PRODUCT
网络钩子调用如下所示:
A webhook call looks like this:
http://<GITLAB_URL>/api/v4/projects/710/ref/master/trigger/pipeline?token=<TOKEN>&variables[PRODUCT]=<PRODUCT>
我通过来自其他项目的 webhook 调用此触发器,包括 PROJECT_B
.所以我在各自的 webhooks 中手动填写了所需的值,例如对于 PROJECT_B
:
I call this trigger through a webhook from other projects, including PROJECT_B
.
So I manually filled in the desired value in the respective webhooks, e.g. for PROJECT_B
:
http://<GITLAB_URL>/api/v4/projects/710/ref/master/trigger/pipeline?token=<TOKEN>&variables[PRODUCT]=PROJECT_B
当 PROJECT_A
中的管道被触发时,$PRODUCT
的值为 PROJECT_B
,正如预期的那样.
When the pipeline in PROJECT_A
is triggered, $PRODUCT
has the value PROJECT_B
, as expected.
我想进一步参数化管道并考虑提交消息等.我需要的所有信息显然都在 webhook 负载.
I would like to parameterize the pipeline further and take, among others, the commit message into account. All the information I need is apparently provided in the webhook payload.
是否有一种内置方法可以在 管道 中读取此有效负载?或者,将有效负载的内容放入 webhook 中的变量中,如下所示:
Is there a built-in way to read this payload in a pipeline? Or alternatively, put contents of the payload into a variable in the webhook like this:
http://<GITLAB_URL>/api/v4/projects/710/ref/master/trigger/pipeline?token=<TOKEN>&variables[COMMIT_REF]=???
我发现讨论关于做参数化 Jenkins 构建 使用 webhook 负载,包括 这个相关问题.Gitlab 论坛也有类似问题,没有任何回答.
I have found discussions about doing parameterized Jenkins builds using the webhook payload, including this related question. There is also a similar question in the Gitlab forum, without any answer.
有没有办法在 Gitlab CI 管道中访问该有效负载?我可能可以通过 jq
调用来提取提供的值,但我首先如何获得 Json?
Is there a way to do access that payload in a Gitlab CI pipeline? I could probably extract the provided values with a jq
call, but how can I get the Json in the first place?
推荐答案
如果您运行 compgen -v
以在 UI 中触发管道时显示环境变量(没有 JSON 有效负载),您将获得 3与POST
JSON 负载相比,作业日志中的行数更少.
If you run compgen -v
to show the environment variables when triggering the pipeline in the UI (without JSON payload) you get 3 fewer lines in your job log than when POST
ing a JSON payload.
附加变量是:
CI_BUILD_TRIGGERED
CI_PIPELINE_TRIGGERED
TRIGGER_PAYLOAD
如果您打印出它们的值并重新运行管道:
If you print their values out and re-run the pipeline:
echo CI_BUILD_TRIGGERED=$CI_BUILD_TRIGGERED
echo CI_PIPELINE_TRIGGERED=$CI_PIPELINE_TRIGGERED
echo TRIGGER_PAYLOAD=$TRIGGER_PAYLOAD
你得到(用户名 YOUR_USER_NAME 和存储库名称 YOUR_REPO_NAME)
You get (for username YOUR_USER_NAME and repo name YOUR_REPO_NAME)
CI_BUILD_TRIGGERED=true
CI_PIPELINE_TRIGGERED=true
TRIGGER_PAYLOAD=/builds/YOUR_USER_NAME/YOUR_REPO_NAME.tmp/TRIGGER_PAYLOAD
如你所见,payload 以 TRIGGER_PAYLOAD
的形式存储在一个以 .tmp
为后缀的临时目录中,它会重新运行管道并将其打印出来(cat
) 显示它包含有效负载,在我的例子中是 JSON.
So as you can see the payload is stored as TRIGGER_PAYLOAD
in a temporary directory suffixed .tmp
, which re-running the pipeline and printing it out (cat
) shows it contains the payload, in my case that’s JSON.
这篇关于在 Gitlab CI 中读取 webhook 负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!