jenkins job triggering downstram job with paramenters not working - jenkins

I am trying to execute a downstream job using the jenkins plugin to pass the parameters to the downstream job. I have added the following configuration as a build step: . The top job completes successfully however in the downstream job, where I am using jenkins plugin for docker build and push, it does not accept the JBNAME :
Building in workspace /home/jenkins/workspace/dockerbuild
ERROR: Unrecognized macro '**JBNAME**' in 'url/repo/**${JBNAME**}'
org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'JBNAME' in 'url/repo/**${JBNAME}**'
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expand(TokenMacro.java:207)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(TokenMacro.java:246)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(TokenMacro.java:232)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.expandAll(DockerBuilder.java:324)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.getImageTags(DockerBuilder.java:336)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.exec(DockerBuilder.java:305)
at com.cloudbees.dockerpublish.DockerBuilder$Perform.access$100(DockerBuilder.java:291)
at com.cloudbees.dockerpublish.DockerBuilder.perform(DockerBuilder.java:262)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
at hudson.model.Run.execute(Run.java:1720)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:404)
Build step 'Docker Build and Publish' marked build as failure
Finished: FAILURE
When I look at the parameters link on the failed build page, the field is empty, it did not pass any parameters, including current or predefined:
I am lost at the moment. I really need to pass the upstream job name to the docker build job in order to preserve the proper container naming.
Thank You very much!

Recent Jenkins versions do not allow undeclared parameters anymore. See related security advisory for details.
In your case the you probably could just declare the JBNAME as a parameter for the downstream job. I.e. select
This build is parameterized in the downstream job configuration and add new string parameter of name JBNAME.

Related

jenkins task plugin empty dropdown for task list

I suddenly got this on a jenkins broken build that uses the jenkins task plugin (failure):
channel stopped
ERROR: Build step failed with exception
java.lang.NullPointerException
at hudson.plugins.batch_task.BatchTaskInvoker$Config.invoke(BatchTaskInvoker.java:109)
at hudson.plugins.batch_task.BatchTaskInvoker.perform(BatchTaskInvoker.java:207)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1074)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
at hudson.model.Run.execute(Run.java:1840)
at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Build step 'Invoke batch tasks' marked build as failure
In the build page it says Downstream Tasks
* Invalid entry: name of project ยป name of task
When I go to configure the project where I would put Post-build Actions invoke build tasks it has an empty dropdown for the task selector. If I type in the build name it says "actually there are no tasks"
How to fix?
Turns out the task plugin had somehow gotten mis-configured. Or at least the old task list deleted.
To fix it, go to the top of the jenkins project -> configure and there is another section called "batch tasks" that had gotten unchecked somehow. Check it, add some more scripts, then the task selector dropdown at the bottom will start working (again), failure message goes away. Suspect user error or some kind of corruption from stopping a build midstream.

Terraform Jenkins integration - forcing (bypassing) the auto approval process

I am trying to integrate Terraform (to create an AWS instance) with Jenkins and need to force (bypass) the approval process. I am trying to do it as a freestyle job providing the required information under the "Build ENv" section.
I am able to create an AWS instance from Terraform. Now I am trying to automate the process using Jenkins. The job is failing because I am not able to by pass the approval process.
Building in workspace C:\Program Files (x86)\Jenkins\workspace\TerrafromInstancecreation
[ModuleOne] $ "C:\Program Files (x86)\Jenkins\tools\org.jenkinsci.plugins.terraform.TerraformInstallation\Terrafrom_0.12.6\terraform.exe" get -update
[ModuleOne] $ "C:\Program Files (x86)\Jenkins\tools\org.jenkinsci.plugins.terraform.TerraformInstallation\Terrafrom_0.12.6\terraform.exe" apply -input=false "-state=C:\Program Files (x86)\Jenkins\workspace\TerrafromInstancecreation\terraform-plugin\terraform-plugin.tfstate"
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
[1mEnter a value:[0m [0m
Apply cancelled.
FATAL: java.lang.Exception: Terraform Apply failed: 1
at org.jenkinsci.plugins.terraform.TerraformBuildWrapper.executeApply(TerraformBuildWrapper.java:249)
at org.jenkinsci.plugins.terraform.TerraformBuildWrapper.setUp(TerraformBuildWrapper.java:269)
at hudson.model.Build$BuildExecution.doRun(Build.java:157)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1810)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
I need to force the auto approval process so that the APPLY phase can run through without prompting and then failing.
My sincere request to please provide any scripts / parameters that I need to use . I am performing this as a freestyle jenkins job.
You should use the -auto-approve=true flag when running non interactively. This will also skip the showing of the plan before and just apply out any changes that Terraform deems necessary.

How do I pass SSH keys from Jenkins Pipeline to Jenkins build jobs?

I'm working on a set of jobs to tag a bunch of related Git repos with the same tag. At the moment, the flow is decomposed into three types of jobs: an overall Jenkins scripted Pipeline, a job that does a build and drops a tag if the build succeeds, and a job triggered by the tagging job that does the final release build. My intention is to allow users to run either the overall pipeline or one of the jobs beneath it depending on if they need to re-run a step in the process or do an entire release.
One of my requirements is that this all needs to happen with the invoking user's credentials, which are then passed to Git so the updates (maven pom changes, etc.) are logged into the commit history as their user. I was successful in this by combining User-scoped credentials with the Authorize Project plugin (so the job can access the user-scoped credentials), the Build User Vars to set user.name and user.email in Git, and the SSH Agent plugin to supply the keys to Git so the commit and tag can be pushed as the correct user.
What I'm trying to do now is collect the user's SSH key with a credentials parameter to the scripted pipeline job and then pass that credentials parameter to the downstream tagging job (which also takes a credentials parameter). Unfortunately, when I do that the downstream job fails because the SSH Agent in the downstream job can't retrieve the credentials based on the value that the credentials parameter in the pipeline passes on to the credentials parameter in the tagging job.
The error I'm getting is:
FATAL:
java.io.IOException: [ssh-agent] Could not find specified credentials
at com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper.preCheckout(SSHAgentBuildWrapper.java:209)
at jenkins.scm.SCMCheckoutStrategy.preCheckout(SCMCheckoutStrategy.java:76)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:490)
at hudson.model.Run.execute(Run.java:1737)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:421)
FATAL: [ssh-agent] Could not find specified credentials
java.io.IOException: [ssh-agent] Could not find specified credentials
at com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper.preCheckout(SSHAgentBuildWrapper.java:209)
at jenkins.scm.SCMCheckoutStrategy.preCheckout(SCMCheckoutStrategy.java:76)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:490)
at hudson.model.Run.execute(Run.java:1737)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:421)
Right now, my Job DSL for the pipeline job looks like this:
parameters {
stringParam('sitePrefix',Projects.siteAbbr,"Three-character site code")
activeChoiceParam('modules'){
choiceType('MULTI_SELECT')
groovyScript{
script("[${projectsAsGroovyString}]")
}
description("Modules to build")
}
credentialsParam('gitUser'){
type('com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey')
required()
description('Personal SSH Key for tagging and releasing')
}
stringParam('gitBranch','develop','Branch to tag')
stringParam('releaseVersion',null,'Version you want to release')
stringParam('developmentVersion',null,'Snapshot version to set after release. If unset, generates a new patch snapshot based on the release version')
}
and my actual pipeline code contains code like this:
def tag_params = [
[$class:'com.cloudbees.plugins.credentials.CredentialsParameterValue',name: 'gitUser',value:params.gitUser],
// credentials(name:'gitUser',value:params.gitUser),
string(name:'gitBranch',value:params.gitBranch),
string(name:'releaseVersion',value:params.releaseVersion),
string(name:'developmentVersion',value:params.developmentVersion),
booleanParam(name:'buildRelease',value:false),
]
stage('Tag bom'){
// Run tag job
build job: "bom_tag_release", parameters: tag_params
// Run release build
build job: "bom_tag_build", parameters: build_params
}
The downstream job is just using another credentials parameter to receive the credentials, not the Credentials Binding plugin because that only seems to handle secret files not the SSH keys that SSH Agent needs. Is passing a credential id from a pipeline to a job even possible or should I be looking at another approach?
Thanks!

ssh-agent plugin can't find credential specified as parameter

In the ssh-agent credentials section of my jenkins job, I have two options:
Specific credentials
Parameter expression
My job works fine when I select Specific credentials, but when I choose Parameter expression, and fill in the value as ${CREDENTIAL_ID}, and run my job with parameter CREDENTIAL_ID=<hex id of credential>, the job dies with this trace:
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Done
FATAL:
java.io.IOException: [ssh-agent] Could not find specified credentials
at com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper.preCheckout(SSHAgentBuildWrapper.java:204)
at jenkins.scm.SCMCheckoutStrategy.preCheckout(SCMCheckoutStrategy.java:76)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:528)
at hudson.model.Run.execute(Run.java:1738)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
FATAL: [ssh-agent] Could not find specified credentials
java.io.IOException: [ssh-agent] Could not find specified credentials
at com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper.preCheckout(SSHAgentBuildWrapper.java:204)
at jenkins.scm.SCMCheckoutStrategy.preCheckout(SCMCheckoutStrategy.java:76)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:528)
at hudson.model.Run.execute(Run.java:1738)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
Finished: FAILURE
Any idea what might be going on? I'm running Jenkins ver. 1.654, Credentials Binding Plugin 1.6, SSH credentials plugin 1.11, and SSH agent plugin 1.9
I was trying to achieve the same thing, using a String Parameter with the credential ID (the hex value, just like you posted in your problem's description).
The issue is that it seems that the Parameter Expression expects you to use a Credential Parameter instead of a String Parameter.
Although it's not exactly how you wanted to use it (you'll have to choose the key from a dropdown list when you trigger a build), changing the parameter type may be a suitable solution for you.

How to use a different set of parameters for release builds in jobs triggered via parameterized trigger plugin

I have a set of jobs that run shell scripts. Parameters for those scripts can be chosen via some choices defined in the build.
Now i want to use the release plugin to prevent people from accidentally choosing from a set of "release only" parameters.
So what i basically need is the ability to have one build with two distinct sets of parameter choices.
To achieve this i have configured the jobs as follows:
Master Job
normal build
- choiceParameter name:TEST values:"normal"
release build
- choiceParameter name:TEST values:"release"
Child Job
normal build
- choiceParameter name:TEST values:"normal"
release build
- choiceParameter name:TEST values:"release"
MasterJob triggers ChildJob via "Parameterized Build" plugin
When i execute a normal build everything works fine.
But when i trigger a Release Build on the MasterJob i get the following exception:
ERROR: Build step failed with exception
java.lang.IllegalArgumentException: Illegal choice for parameter TEST: release
at hudson.model.ChoiceParameterDefinition.checkValue(ChoiceParameterDefinition.java:75)
at hudson.model.ChoiceParameterDefinition.createValue(ChoiceParameterDefinition.java:87)
at hudson.model.ChoiceParameterDefinition.createValue(ChoiceParameterDefinition.java:19)
at hudson.plugins.parameterizedtrigger.ProjectSpecificParameterValuesActionTransform.convertToDefinedType(ProjectSpecificParameterValuesActionTransform.java:83)
at hudson.plugins.parameterizedtrigger.ProjectSpecificParameterValuesActionTransform.transformParametersAction(ProjectSpecificParameterValuesActionTransform.java:34)
at hudson.plugins.parameterizedtrigger.ProjectSpecificParametersActionFactory.getProjectSpecificBuildActions(ProjectSpecificParametersActionFactory.java:32)
at hudson.plugins.parameterizedtrigger.BuildTriggerConfig.getBuildActions(BuildTriggerConfig.java:290)
at hudson.plugins.parameterizedtrigger.BuildTriggerConfig.perform2(BuildTriggerConfig.java:336)
at hudson.plugins.parameterizedtrigger.BlockableBuildTriggerConfig.perform2(BlockableBuildTriggerConfig.java:57)
at hudson.plugins.parameterizedtrigger.TriggerBuilder.perform(TriggerBuilder.java:85)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:761)
at hudson.model.Build$BuildExecution.build(Build.java:203)
at hudson.model.Build$BuildExecution.doRun(Build.java:160)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:536)
at hudson.model.Run.execute(Run.java:1741)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Build step 'Trigger/call builds on other projects' marked build as failure
Finished: FAILURE
Fixing this error is easy. I just have to add the value "release" to the choices in the normal build. But this destroys the whole intention of this setup.
Is there a way to get this kind of setup to work?
If you want people to restrict running arbitrary script on production boxes, You can use Node label plugin.
You can configure the job to select which node(box/machine) user can run the job, This way you can restrict user running jobs on prod env.

Resources