jenkinsci / gitlab-plugin

A Jenkins plugin for interfacing with GitLab
https://plugins.jenkins.io/gitlab-plugin/
GNU General Public License v2.0
1.44k stars 618 forks source link

Feature request: ability to pass environment variables and parameter values via webhook URI #912

Open DavidAntliff opened 5 years ago

DavidAntliff commented 5 years ago

I think it would be useful to have a mechanism to convey arbitrary information from GitLab to a triggered Jenkins job. At the moment, the GitLab plugin makes variables such as gitlabActionType and gitlabTargetRepoName readily available to Jenkins, however there doesn't seem to be a way to add arbitrary variables.

I've searched around for this and it seems to be a fairly common question. Some people switch to a generic webhook plugin and populate those variables themselves. Others write a gitlab-CI job that sends the initial webhook request and then monitors the Jenkins job(s).

I think it would be sufficient to add parameters to the webhook URL such that the GitLab plugin adds them to the environment (is this safe?). For example:

http://jenkins.server/project/MyProject?MYPARAM=FOO

Then the associated Jenkins job would have a variable called MYPARAM set to value FOO in its environment. This should also be accessible in the scripted pipeline via env.MYPARAM.

This would be even better if such parameters could match the names of "parameterised" parameters, enabling the webhook to set overriding values for those, rather than using the default value.

The reason this is useful to me is that I have a repository with multiple webhooks configured that (ideally) point at the same Jenkins job, but I need that job to behave slightly differently for each. I want to put a special flag in the webhook URL to direct the Jenkins job for each trigger. I'd rather not create multiple Jenkins jobs, or even worse, multiple Jenkinsfiles.

desolat commented 4 years ago

The webhook already provides repository-related parameters to the Jenkins job. Have a look at https://github.com/jenkinsci/gitlab-plugin#parameter-configuration for how to utilize them. But be aware that this script needs administrator approval on current Jenkins versions due to tighter script security.

lwb4 commented 4 years ago

it would be sufficient to add parameters to the webhook URL such that the GitLab plugin adds them to the environment (is this safe?). For example:

http://jenkins.server/project/MyProject?MYPARAM=FO)

This would be IMMENSELY useful to my company. We have hundreds of jenkins jobs that all just call the same single job internally with separate, hard-coded parameters. It would help us reduce the number of jenkins scripts that we have to maintain by a factor of ten.

If the maintainers are open to it, I would be happy to start a pull request for this feature.

pauloneves commented 4 years ago

I have the same requisite. This is a fairly common question in StackOverflow:

As the original poster noted, the solution is almost always to stop using GitLab plugin and use the old Jenkins hook.

KhimairaCrypto commented 4 years ago

I did not see any reference to TAGS pipeline-syntax/globals. Seams like gitlab includes the ref: as part of the payload. Is there any variable that I can use to get the tag?

Dorsosiv commented 1 year ago

Is there any progress on the subject ? I also have the same issue that I have to create a lot of jenkinsfiles each with different hardcoded parameters/values because it is not possible to send any parameters or values from Gitlab MR with webhook to Jenkins item pipeline.