jenkinsci / gitlab-branch-source-plugin

A Jenkins Plugin for GitLab Multibranch Pipeline jobs and Folder Organization
https://plugins.jenkins.io/gitlab-branch-source
MIT License
121 stars 88 forks source link

Env variable like GITLAB_OA_IID are not set when a MR job is triggered manually from Jenkins #293

Open jmini opened 1 year ago

jmini commented 1 year ago

Jenkins and plugins versions report

Environment ```text Jenkins: 2.346.2 OS: Linux - 3.10.0-1160.71.1.el7.x86_64 Java: 1.8.0_342 - Red Hat, Inc. (OpenJDK 64-Bit Server VM) --- PrioritySorter:4.1.0 ace-editor:1.1 adoptopenjdk:1.5 analysis-model-api:10.17.0 ant:481.v7b_09e538fcca antisamy-markup-formatter:2.7 apache-httpcomponents-client-4-api:4.5.13-138.v4e7d9a_7b_a_e61 async-http-client:1.9.40.0 atlassian-bitbucket-server-integration:3.3.2 authentication-tokens:1.4 aws-java-sdk:1.12.287-357.vf82d85a_6eefd aws-java-sdk-cloudformation:1.12.287-357.vf82d85a_6eefd aws-java-sdk-codebuild:1.12.287-357.vf82d85a_6eefd aws-java-sdk-ec2:1.12.287-357.vf82d85a_6eefd aws-java-sdk-ecr:1.12.287-357.vf82d85a_6eefd aws-java-sdk-ecs:1.12.287-357.vf82d85a_6eefd aws-java-sdk-efs:1.12.287-357.vf82d85a_6eefd aws-java-sdk-elasticbeanstalk:1.12.287-357.vf82d85a_6eefd aws-java-sdk-iam:1.12.287-357.vf82d85a_6eefd aws-java-sdk-logs:1.12.287-357.vf82d85a_6eefd aws-java-sdk-minimal:1.12.287-357.vf82d85a_6eefd aws-java-sdk-sns:1.12.287-357.vf82d85a_6eefd aws-java-sdk-sqs:1.12.287-357.vf82d85a_6eefd aws-java-sdk-ssm:1.12.287-357.vf82d85a_6eefd basic-branch-build-strategies:1.3.2 bitbucket:223.vd12f2bca5430 bootstrap4-api:4.6.0-5 bootstrap5-api:5.2.0-1 bouncycastle-api:2.26 branch-api:2.1046.v0ca_37783ecc5 build-failure-analyzer:2.4.0 build-name-setter:2.2.0 build-timeout:1.24 build-timestamp:1.0.3 caffeine-api:2.9.3-65.v6a_47d0f4d1fe checks-api:1.7.5 claim:495.v01a_c6fb_2b_a_7a cloudbees-bitbucket-branch-source:791.vb_eea_a_476405b cloudbees-disk-usage-simple:178.v1a_4d2f6359a_8 cloudbees-folder:6.740.ve4f4ffa_dea_54 cobertura:1.17 code-coverage-api:3.0.5 command-launcher:90.v669d7ccb_7c31 commons-lang3-api:3.12.0-36.vd97de6465d5b_ conditional-buildstep:1.4.2 config-file-provider:3.11.1 configurationslicing:430.v966357576543 credentials:1143.vb_e8b_b_ceee347 credentials-binding:523.vd859a_4b_122e6 crowd2:3.1.2 cucumber-reports:5.7.3 cucumber-testresult-plugin:0.10.1 custom-tools-plugin:0.8 dashboard-view:2.447.vda_124dd35f11 data-tables-api:1.12.1-3 display-url-api:2.3.6 docker-commons:1.21 docker-workflow:521.v1a_a_dd2073b_2e durable-task:501.ve5d4fc08b0be echarts-api:5.3.3-1 email-ext:2.92 extended-choice-parameter:346.vd87693c5a_86c extended-read-permission:3.2 external-monitor-job:203.v683c09d993b_9 favorite:2.4.1 file-leak-detector:1.10 font-awesome-api:6.1.2-1 forensics-api:1.15.1 generic-webhook-trigger:1.85.2 gerrit-trigger:2.36.0 git:4.12.1 git-client:3.12.1 git-server:1.11 github:1.34.5 github-api:1.303-400.v35c2d8258028 github-autostatus:3.6.2 github-branch-source:1695.v88de84e9f6b_9 github-organization-folder:1.6 gitlab-api:5.0.1-78.v47a_45b_9f78b_7 gitlab-branch-source:642.v9ed86b_b_54384 gitlab-plugin:1.5.36 golang:1.4 google-cloudbuild:0.2.6 (2021-06-02T18:43:55Z) google-oauth-plugin:1.0.7 gradle:1.40 groovy:453.vcdb_a_c5c99890 handlebars:3.0.8 handy-uri-templates-2-api:2.1.8-22.v77d5b_75e6953 htmlpublisher:1.31 icon-shim:3.0.0 ionicons-api:31.v4757b_6987003 jackson2-api:2.13.4.20221013-295.v8e29ea_354141 jacoco:3.3.2 jakarta-activation-api:2.0.1-2 jakarta-mail-api:2.0.1-2 javadoc:226.v71211feb_e7e9 javax-activation-api:1.2.0-5 javax-mail-api:1.6.2-8 jaxb:2.3.7-1 jdk-tool:55.v1b_32b_6ca_f9ca jenkins-design-language:1.25.8 jersey2-api:2.37-1 jira:3.8 jjwt-api:0.11.5-77.v646c772fddb_0 jnr-posix-api:3.1.15-2 job-dsl:1.81 job-restrictions:0.8 jquery:1.12.4-1 jquery-detached:1.2.1 jquery-ui:1.0.2 jquery3-api:3.6.1-1 jsch:0.1.55.61.va_e9ee26616e7 junit:1144.v909f4d9978e8 jython:1.9 ldap:2.12 lockable-resources:2.18 logfilesizechecker:1.5 mailer:435.v79ef3972b_5c7 managed-scripts:1.5.6 mapdb-api:1.0.9-28.vf251ce40855d mask-passwords:3.3 matrix-auth:3.1.5 matrix-project:772.v494f19991984 maven-plugin:3.20 mercurial:1260.vdfb_723cdcc81 metrics:4.2.10-389.v93143621b_050 mina-sshd-api-common:2.9.1-44.v476733c11f82 mina-sshd-api-core:2.9.1-44.v476733c11f82 momentjs:1.1.1 monitor-pro:1.0.9 monitoring:1.91.0 multiple-scms:0.8 naginator:1.18.1 node-iterator-api:49.v58a_8b_35f8363 nodejs:1.5.1 oauth-credentials:0.5 okhttp-api:4.9.3-108.v0feda04578cf p4:1.13.0 pam-auth:1.10 parameterized-trigger:2.45 permissive-script-security:0.7 pipeline-build-step:2.18 pipeline-github-lib:38.v445716ea_edda_ pipeline-graph-analysis:195.v5812d95a_a_2f9 pipeline-groovy-lib:613.v9c41a_160233f pipeline-input-step:456.vd8a_957db_5b_e9 pipeline-milestone-step:101.vd572fef9d926 pipeline-model-api:2.2118.v31fd5b_9944b_5 pipeline-model-declarative-agent:1.1.1 pipeline-model-definition:2.2118.v31fd5b_9944b_5 pipeline-model-extensions:2.2118.v31fd5b_9944b_5 pipeline-rest-api:2.27 pipeline-stage-step:296.v5f6908f017a_5 pipeline-stage-tags-metadata:2.2118.v31fd5b_9944b_5 pipeline-stage-view:2.27 pipeline-utility-steps:2.13.0 plain-credentials:139.ved2b_9cf7587b plugin-util-api:2.17.0 popper-api:1.16.1-3 popper2-api:2.11.6-1 prism-api:1.28.0-2 promoted-builds:892.vd6219fc0a_efb pubsub-light:1.16 python:1.3 rabbitmq-consumer:2.8 rebuild:1.34 resource-disposer:0.20 role-strategy:562.v44e9a_e828d0e run-condition:1.5 scm-api:621.vda_a_b_055e58f7 script-security:1189.vb_a_b_7c8fd5fde simple-theme-plugin:103.va_161d09c38c7 slack:629.vf00ea_cb_40d53 snakeyaml-api:1.32-86.ve3f030a_75631 sonar:2.14 sse-gateway:1.25 ssh-agent:295.v9ca_a_1c7cc3a_a_ ssh-credentials:305.v8f4381501156 ssh-slaves:1.834.v622da_57f702c sshd:3.242.va_db_9da_b_26a_c3 structs:324.va_f5d6774f3a_d support-core:1206.v14049fa_b_d860 testng-plugin:700.va_ea_5873a_3399 timestamper:1.20 token-macro:308.v4f2b_ed62b_b_16 trilead-api:1.67.vc3938a_35172f update-sites-manager:2.0.0 variant:59.vf075fe829ccb warnings-ng:9.18.0 windows-slaves:1.8.1 workflow-aggregator:590.v6a_d052e5a_a_b_5 workflow-api:1200.v8005c684b_a_c6 workflow-basic-steps:994.vd57e3ca_46d24 workflow-cps:2803.v1a_f77ffcc773 workflow-cps-global-lib:588.v576c103a_ff86 workflow-durable-task-step:1206.v8a_d5f86e336b workflow-job:1207.ve6191ff089f8 workflow-multibranch:716.vc692a_e52371b_ workflow-remote-loader:1.5 workflow-scm-step:400.v6b_89a_1317c9a_ workflow-step-api:639.v6eca_cd8c04a_a_ workflow-support:839.v35e2736cfd5c ws-cleanup:0.43 xray-for-jira-connector:1.3.0 ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Linux

Reproduction steps

Have a project configured:

Open an MR in GitLab.

This triggers a job in the MR view:

MR job view

On one Job in this MR view click on "Build" (or "Build with parameter" if the Jenkinsfile defines some parameter) to trigger a manual job.

Screenshot 2023-03-29 at 11 22 05

Expected Results

I would like the env variables (like GITLAB_OA_IID, GITLAB_OA_SOURCE_BRANCH, GITLAB_OA_LAST_COMMIT_ID ...) to be set as it is the case when the Job is triggered from GitLab.

Actual Results

All the GITLAB_ env variable are not set in case of a manually triggered job.

Anything else?

Maybe I should rewrite my Jenkinsfile to use other env variable that are present in both the GitLab triggered and the Manually triggered case:

But I could not find a equivalent of GITLAB_OA_LAST_COMMIT_ID

Turiok commented 1 year ago

I don't think this is possible. The variables are filled with the webhook that Gitlab sends. With a manual trigger, there is no such information received. But if you have retrieved the sources with the SCM plugin, you can look in these variables. https://plugins.jenkins.io/git/#plugin-content-commit-variables

jmini commented 1 year ago

Thank very much for your feedback @Turiok.

We have changed to use env variables that are set in both the manually triggered case and the GitLab triggered case.


We are now missing some more information such as:


Start of the log in the GitLab triggered case:

Merge request !10 opened
Triggered by GitLab Merge Request #10: patch-2 => main
Querying the current revision of merge request #10...
Current revision of merge request #10 is bea04193507a9e09fd92a06a5070155b77e43fe5

Start of the log in the manually triggered case:

Started by user Jérémie Bresson
Querying the current revision of merge request #10...
Current revision of merge request #10 is bea04193507a9e09fd92a06a5070155b77e43fe5

In both cases we are going trough the code that reads the MR:

https://github.com/jenkinsci/gitlab-branch-source-plugin/blob/ad1ce6d01959377bf3ff35f1905b5ce941e501d5/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java#L265-L271

So the information would be available, since the MR is read from GitLab in both cases.

Would it be possible to expose some of the MR attributes as environment variables that could be reused later in the Jenkins job?

For example GITLAB_OBJECT_KIND is to none also when the Job is started manually by this line:

https://github.com/jenkinsci/gitlab-branch-source-plugin/blob/ad1ce6d01959377bf3ff35f1905b5ce941e501d5/src/main/java/io/jenkins/plugins/gitlabbranchsource/Environment/GitLabWebHookEnvironmentContributor.java#L27

Any hint is very much appreciated.