[JIRA] [gitlab-plugin] (JENKINS-34700) parameters are not "overwritten"

93 views
Skip to first unread message

nico.bollen@gmail.com (JIRA)

unread,
May 10, 2016, 1:47:01 AM5/10/16
to jenkinsc...@googlegroups.com
Nico Bollen created an issue
 
Jenkins / Bug JENKINS-34700
parameters are not "overwritten"
Issue Type: Bug Bug
Assignee: Robin Müller
Components: gitlab-plugin
Created: 2016/May/10 5:46 AM
Labels: plugin
Priority: Minor Minor
Reporter: Nico Bollen

We have a number of jobs triggered by gitlab and thus we use the gitlabSourceBranch and gitlabTargetBranch variable to get the correct branch from gitlab.
To allow manual triggers we enable the "this job is parameterized" option and set 2 string parameters to some default value and with the same names gitlabSourceBranch and gitlabTargetBranch. Till before about 1 week the job parameters default values where overwritten by the gitlab hook values, but after some updates it's the other way around, so the gitlab hook values are overwritten by the jobs default values...

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v6.4.2#64017-sha1:e244265)
Atlassian logo

nico.bollen@gmail.com (JIRA)

unread,
May 10, 2016, 2:21:01 AM5/10/16
to jenkinsc...@googlegroups.com
Nico Bollen commented on Bug JENKINS-34700
 
Re: parameters are not "overwritten"

or the parameters are written in the incorrect order?
1. gitlab hook values
2. parameterized job "default" values

rm1990@gmx.de (JIRA)

unread,
May 10, 2016, 3:00:01 AM5/10/16
to jenkinsc...@googlegroups.com

Since version 1.2 the plugin set the gitlab hook values as environment variables instead of build parameters. To set default values for these you should have a look at this plugin: EvnInject Plugin

nico.bollen@gmail.com (JIRA)

unread,
May 10, 2016, 3:54:01 AM5/10/16
to jenkinsc...@googlegroups.com

Ok that could explain a lot. How to allow manual triggers of a specific branch which is not the "default" configured?

nico.bollen@gmail.com (JIRA)

unread,
May 10, 2016, 5:58:26 AM5/10/16
to jenkinsc...@googlegroups.com

I'm using EnvInject Plugin to add some defaults for the gitlabSourceBranch and gitlabTargetBranch but both values are not overwritten in case of a gitlab webhook triggering the job... (if I remove the EnvInject Plugin stuff the environment variables are correct).

nico.bollen@gmail.com (JIRA)

unread,
May 10, 2016, 6:05:03 AM5/10/16
to jenkinsc...@googlegroups.com

ok seems the EnvInject stuff is 2nd.
So you need to check if the variables already exist.
For now I got following Groovy script in Job Config --> Prepare an environment for the run --> Evaluated Groovy script

import hudson.model.*
def env = Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars')
def map = [:]
println env['gitlabSourceBranch']
if(env['gitlabSourceBranch']== null)

{ map['gitlabSourceBranch'] = 'develop' }

println env['gitlabTargetBranch']
if(env['gitlabTargetBranch']== null)

{ map['gitlabTargetBranch'] = 'develop' }

return map

nico.bollen@gmail.com (JIRA)

unread,
May 10, 2016, 6:07:01 AM5/10/16
to jenkinsc...@googlegroups.com
Nico Bollen edited a comment on Bug JENKINS-34700

nico.bollen@gmail.com (JIRA)

unread,
May 12, 2016, 5:00:06 AM5/12/16
to jenkinsc...@googlegroups.com

btw, projects readme needs updating:

"You can trigger a job a manually by clicking This build is parameterized and adding the any of the relevant build parameters. These include:"

abcfy2@163.com (JIRA)

unread,
May 17, 2016, 3:45:01 AM5/17/16
to jenkinsc...@googlegroups.com
feng yu commented on Bug JENKINS-34700

Same issue here. The gitlab-plugin not working for me since 1.2.0. The plugin readme should be updated.

mikhail.zakharenko@gmail.com (JIRA)

unread,
Jun 2, 2016, 6:53:02 AM6/2/16
to jenkinsc...@googlegroups.com

How i can use EvnInject Plugin to override default build parameters without Groovy script ?

msrb@redhat.com (JIRA)

unread,
Jun 2, 2016, 7:55:02 AM6/2/16
to jenkinsc...@googlegroups.com

Mikhail Zakharenko not sure what you are trying to do, but I stopped using default build parameters and I do everything in EnvInject+Groovy instead. For example this script checks if environment variable already exists. If it does, then it means it was set by GitLab plugin. Otherwise the script sets it to some default value.

def env = Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars')
def map = [:]
map["gitlabActionType"] = env.get("gitlabActionType", "PUSH")
map["gitlabBranch"] = env.get("gitlabBranch", "master")
map["gitlabSourceBranch"] = env.get("gitlabSourceBranch", "master")
...
return map

crussell52@gmail.com (JIRA)

unread,
Jun 13, 2016, 2:03:01 PM6/13/16
to jenkinsc...@googlegroups.com

Why the shift to environment variables over parameters? I am interested in the motivation so that we can see if there is a way to meet that need and avoid the following issues:

This makes it much harder to create a Jenkins job which can be triggered by GitLab or by manual execution.

Before, you simply defined the job with appropriate parameters, then filled them out in the case of a manual execution.

Now you need to define parameters, then write a groovy script to conditionally overwrite environment variables. If you want to use defaults, this is even harder:

1. Define parameters without defaults.
2. Write groovy script to write environment vars using the following order or precedence: Parameter if not empty; else environment if not empty; else default

This creates a tedious setup which is difficult to maintain across several projects.

Such an implementation also hides defaults from the end user when they start the build. The only viable work-around to that is to write the defaults into the helper-text for each parameter. This means that if your defaults change, you must update the groovy script and the documentation further adding to the maintenance cost.

This change makes it is much harder to now determine what the execution parameters of a specific build were.

Before, you could just inspect the build parameters. The values which are relevant to the build operations are clearly visible.

Now, you must sift through the very cluttered list of environment variables. Even if a gitlab-value is irrelevant for the build operations it shows up in the list further obfuscating what was used as build parameters.

crussell52@gmail.com (JIRA)

unread,
Jun 13, 2016, 2:13:01 PM6/13/16
to jenkinsc...@googlegroups.com
Chris Russell edited a comment on Bug JENKINS-34700
(edited for grammar)

Why the shift to environment variables over parameters?  I am interested in the motivation so that we can see if there is a way to meet that need and avoid the following issues:

h3. This makes it much harder to create a Jenkins job which can be triggered by GitLab *or* by manual execution. 

*Before*, you simply defined the job with appropriate parameters, then filled them out in the case of a manual execution.  

*Now* you need to define parameters, then write a groovy script to conditionally overwrite environment variables. If you want to use defaults, this is even harder:


1. Define parameters without defaults. 
2. Write groovy script to write environment vars using the following order or precedence:  Parameter if not empty; else environment if not empty; else default

This creates a tedious setup which is difficult to maintain across several projects. 

Such an implementation also hides defaults from the end user when they start the build. The only viable work-around to that is to write the defaults into the helper-text for each parameter.  This means that if your defaults change, you must update the groovy script and the documentation further adding to the maintenance cost.

h3. This change makes it is much harder to now determine what the execution parameters of a specific build were. 

*Before*, you could just inspect the build parameters.  The values which are relevant to the build operations are clearly visible. 

*Now*, you must sift through the very cluttered list of environment variables.
 Even if a  All  gitlab -value is irrelevant for the build operations it shows  values show  up in the  environment  list  further obfuscating what was  instead of just the ones which were  used  as  by the  build  parameters ; this further obfuscates the meaningful values .

crussell52@gmail.com (JIRA)

unread,
Jun 13, 2016, 2:14:03 PM6/13/16
to jenkinsc...@googlegroups.com
Chris Russell edited a comment on Bug JENKINS-34700
(edited for grammar)

Why the shift to environment variables over parameters?  I am interested in the motivation so that we can see if there is a way to meet that need and avoid the following issues:

h3. This makes it much harder to create a Jenkins job which can be triggered by GitLab *or* by manual execution. 

*Before*, you simply defined the job with appropriate parameters, then filled them out in the case of a manual execution.  

*Now* you need to define parameters, then write a groovy script to conditionally overwrite environment variables. If you want to use defaults, this is even harder:

1. Define parameters without defaults. 
2. Write groovy script to write environment vars using the following order or precedence:  Parameter if not empty; else environment if not empty; else default

This creates a tedious setup which is difficult to maintain across several projects. 

Such an implementation also hides defaults from the end user when they start the build. The only viable work-around to that is to write the defaults into the helper-text for each parameter.  This means that if your defaults change, you must update the groovy script and the documentation ,  further adding to the maintenance cost.


h3. This change makes it is much harder to now determine what the execution parameters of a specific build were. 

*Before*, you could just inspect the build parameters.  The values which are relevant to the build operations are clearly visible. 

*Now*, you must sift through the very cluttered list of environment variables. All gitlab values show up in the environment list instead of just the ones which were used by the build; this further obfuscates the meaningful values.

owen@nerdnetworks.org (JIRA)

unread,
Jun 13, 2016, 3:35:03 PM6/13/16
to jenkinsc...@googlegroups.com

Chris Russell The change was required for two reasons. First, in order to support the Pipeline project type. See https://github.com/jenkinsci/gitlab-plugin/issues/271 for the discussion around this. Second, a security issue in Jenkins was fixed (see https://wiki.jenkins-ci.org/display/JENKINS/Plugins+affected+by+fix+for+SECURITY-170), but the fix prevents plugins from defining arbitrary job parameters; parameters must be explicitly configured in the Jenkins job. So, going forward, unless you define every parameter you want to use in your job config, the plugin can't change their values. At least with environment variables, you can use all of them in shell script steps and Pipeline job types.

We need to update the documentation to provide a complete example of how to use the EnvInject plugin to work around this.

crussell52@gmail.com (JIRA)

unread,
Jun 13, 2016, 4:49:01 PM6/13/16
to jenkinsc...@googlegroups.com

Owen Mehegan: Fantastic background on the issue, thank you very much.

Pipeline support is paramount, in my opinion; as far as I'm concerned, that alone justifies this change. I highly recommend including a very brief explanation of why this changed when you finish updating the docs – it is not obvious to end users, and the change can have real impact on the way some craft their workflows.

Thank you for your hard work on maintaining this plugin. (Robin Müller you too, of course!)

nico.bollen@gmail.com (JIRA)

unread,
Jun 14, 2016, 2:07:03 AM6/14/16
to jenkinsc...@googlegroups.com

Chris Russell we use the "SharedObjects Plugin" in which I have defined 1 Groovy script which does following:

  • check if environment variables ("gitlabSourceBranch" and "gitlabTargetBranch") are set by gitlab webhook
  • if so update job parameters ("source_branch" and "target_branch")
    Our jobs now use "source_branch" and "target_branch" parameters instead of "gitlabSourceBranch" and "gitlabTargetBranch".
    These "source_branch" and "target_branch" parameters are set to a default using the normal "parameterized job" stuff as before.

So in the end we needed to do a bulk operation on changing the parameter names in all jobs and to add the triggering of the shared object groovy script.

An example implementation in the documents would for sure have helped in this...

This is the Groovy script we use for now:

Groovy shared object
import hudson.model.*
def env= Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars')
def build= Thread.currentThread()?.executable
def map= [:]
def source= env['source_branch']
def target= env['target_branch']
if(env['gitlabSourceBranch']!=null) {
  source=env['gitlabSourceBranch']
}
if(env['gitlabTargetBranch']!=null) {
  target=env['gitlabTargetBranch']
}

def currentActions= build.getAction(ParametersAction.class)
def curParams= currentActions.getParameters()
List<ParameterValue> newParams= []
curParams.each() {
  println it.name
  if ( it.name.equals("source_branch") ) {
    newParams.add( new StringParameterValue("source_branch", source) )
  }
  else if (it.name.equals("target_branch")) {
    newParams.add( new StringParameterValue("target_branch", target) )
  }
  else {
    newParams.add( it )
  }
}

build.actions.remove(currentActions)
newActions = new ParametersAction(newParams)
build.actions.add(newActions)

map['source_branch']= source
map['target_branch']= target

return map
This message was sent by Atlassian JIRA (v7.1.7#71011-sha1:2526d7c)
Atlassian logo

nico.bollen@gmail.com (JIRA)

unread,
Jun 14, 2016, 2:07:03 AM6/14/16
to jenkinsc...@googlegroups.com
Nico Bollen edited a comment on Bug JENKINS-34700
[~crussell52] we use the "SharedObjects Plugin" in which I have defined 1 Groovy script which does following:
- check if environment variables ("gitlabSourceBranch" and "gitlabTargetBranch") are set by gitlab webhook
- if so update job parameters ("source_branch" and "target_branch")

Our jobs now use "source_branch" and "target_branch" parameters instead of "gitlabSourceBranch" and "gitlabTargetBranch".
These "source_branch" and "target_branch" parameters are set to a default using the normal "parameterized job" stuff as before.

So in the end we needed to do a bulk operation on changing the parameter names in all jobs and to add the triggering of the shared object groovy script.

An example implementation in the documents would for sure have helped in this...

This is the Groovy script we use for now:

{code:title=Groovy shared object|borderStyle=solid}
{code}

crussell52@gmail.com (JIRA)

unread,
Jun 15, 2016, 10:27:05 AM6/15/16
to jenkinsc...@googlegroups.com

Nico Bollen Thank you for that. That certainly helps cut down my research and experimentation.

I've found that I've been able to get the current environment using the following in the groovy section EnvInject:

def env= currentBuild.properties.get('envVars')

I've also opened https://issues.jenkins-ci.org/browse/JENKINS-35945 requesting enhanced documentation when configuration changes occur between versions, in case you are interested in watching it.

omorillovictoria@googlemail.com (JIRA)

unread,
Jun 15, 2016, 1:02:02 PM6/15/16
to jenkinsc...@googlegroups.com
Change By: Oscar Salvador Morillo Victoria
Assignee: Robin Müller Oscar Salvador Morillo Victoria
Reply all
Reply to author
Forward
0 new messages