Take credentialsId from user - jenkins-pipeline

I am building a pipeline template where I want an ability to get credentialId from user.
Currently I have this piece of code which works just fine, but I could not find a better way to parameterize it.
withCredentials([usernamePassword(credentialsId: 'mycredentialId', usernameVariable: 'mycredentialId_USR', passwordVariable: 'mycredentialId_PSW')
I tried using environment variable like this to substitute the credentialId.
environment {
userCredentialID = "${userCredentialID}"
}
and then use that variable inside pipeline step like this, but it does not work.
withCredentials([usernamePassword(credentialsId: '${userCredentialID}', usernameVariable: 'userCredentialID_USR', passwordVariable: 'userCredentialID_PSW')
Any leads on how to parameterize a credential would be very helpful.
Thank you very much!

Related

How to use custom substitutions with secretmanager in cloudbuild?

I'm having an issue with using custom substitutions in my cloudbuild.yaml.
substitutions:
_CUSTOM_SUBSTITUTION: this-is-a-path
availableSecrets:
secretManager:
- versionName: projects/$_CUSTOM_SUBSTITUTION/secrets/client_id/versions/1
env: CLIENT_ID
- versionName: projects/$_CUSTOM_SUBSTITUTION/secrets/client_secret/versions/1
env: CLIENT_SECRET
From what I can tell from trial and error, using something like $PROJECT_ID in the place of $_CUSTOM_SUBSTITUTION will run the build, but if I use a custom substitution like above, the trigger does not run a build at all when a commit is pushed.
I've also tested with various other base substitutions, like $BRANCH_NAME to the same effect. I'm getting the feeling that it's just not possible to do this in cloudbuild at the moment?
It ended up being a combination of need curly braces ${_CUSTOM_SUBSTITUTION} and some syntax fixing in the cloudbuild.yaml. I didn't have enough experience with cloudbuild to find that.
The offending part was something this:
AUTH_TOKEN=$$(cat /workspace/token.txt). Originally I had just 1 $ there, which was also working code pulled from another project.
For anyone running into this in the future, using gloud builds submit can let you run it directly for troubleshooting.

Jenkins: Facing issue Insert choice parameters in Goals and options

I want to run the Jenkins job which is as below, which is absolutly working fine:
clean verify -Denvironment=default -Dtags="type:NL" -Dwebdriver.driver=chrome -Dorganization=$organization -Dit.test=TC09_DBG_Debug_Features
For -Dtags="type:NL", with 'NL', i am having three different tags SE,DE and DEMO. Which is as below:
-Dtags="type:NL"
-Dtags="type:SE"
-Dtags="type:DE"
-Dtags="type:DEMO"
I decided to go with 'Choice parameters' in Jenkins having NL,SE,DE and DEMO as choices and name as 'Tenant'.
The problem i am facing is how to put choice parameters in Goals and options.
I tried
clean verify -Denvironment=default -Dtags= "-DpropertyName="$Tenant"" -Dwebdriver.driver=chrome -Dorganization=$organization -Dit.test=TC09_DBG_Debug_Features
but no help. Where am i going wrong. Thanks for help and try in advance.
Came up with a solution:
clean verify -Denvironment=default -Dtags="$Tenant" -Dwebdriver.driver=chrome -Dorganization=$organization -Dit.test=TC09_DBG_Debug_Features

Use of variables (bash script) in the mail body in jenkins

I currently have the following script:
var = foo
And the configuration of the email (Editable Email Publisher) I have it like this:
configuration Email Publisher
Reading in other questions, someone said that it worked for him using this:
${ENV, var="var"}
However, it does not work for me, can you help me please?
When you run a script that add environment variable the lifetime of this variable is only until the script ends.
You have plugin Environment Injector (was EnvInject Plugin) that using this plugin you can inject variable to all the job life time.
So if you want to add variable in the build section and to use it in the post build section you need to inject the variable.

Pushing back to git repo in Jenkinsfile using GIT_ASKPASS

Is there a way to push back to the repo that the Jenkins Pipeline checked out using the same process that the code was checked out with (using GIT_ASKPASS)?
I currently have a workaround solution for achieving this by grabbing the credentials like this:
withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'GIT_PASS', usernameVariable: 'GIT_USER')]) {
sh('git push https://$GIT_USER:$GIT_PASS#github.com/orgname/private-repo.git master')
}
I'm not a Groovy developer, but found a method in the git-client-plugin that I would like to use. Is there a way to use the following method directly in the Jenkinsfile
launchCommandWithCredentials
https://github.com/jenkinsci/git-client-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java#L1649

Access to build environment variables from a groovy script in a Jenkins build step (Windows)

I'm using Scriptler plugin, so I can run a groovy script as a build step. My Jenkins slaves are running on windows in service mode. With scriptler, I don't need to use windows batch scripts.
But I have trouble to get the environment variables in a build step... This is working:
System.getenv("BASE")
Where BASE is part of the env-vars on jenkins startup. However, I would like to get
%JOB_NAME%
If I'm adding an "Execute Windows batch command" build step:
echo %JOB_NAME%
It works.
If I'm adding a scriptler script as a build step with the same settings:
println "JOB_NAME: " + System.getenv("JOB_NAME")
I'm getting:
JOB_NAME: null
So how can I reach the injected environment variables from a groovy script as a build step?
build and listener objects are presenting during system groovy execution. You can do this:
def myVar = build.getEnvironment(listener).get('myVar')
You might be able to get them like this:
def thr = Thread.currentThread()
def build = thr?.executable
def envVarsMap = build.parent.builds[0].properties.get("envVars")
On jenkins 2.x, with groovy plugin 2.0, running SystemGroovyScript I managed to get to build variables, as below:
def build = this.getProperty('binding').getVariable('build')
def listener = this.getProperty('binding').getVariable('listener')
def env = build.getEnvironment(listener)
println env.MY_VARIABLE
If you are using goovy from file, simple System.getenv('MY_VARIABLE') is sufficient
The Scriptler Groovy script doesn't seem to get all the environment variables of the build. But what you can do is force them in as parameters to the script:
When you add the Scriptler build step into your job, select the option "Define script parameters"
Add a parameter for each environment variable you want to pass in. For example "Name: JOB_NAME", "Value: $JOB_NAME". The value will get expanded from the Jenkins build environment using '$envName' type variables, most fields in the job configuration settings support this sort of expansion from my experience.
In your script, you should have a variable with the same name as the parameter, so you can access the parameters with something like:
println "JOB_NAME = $JOB_NAME"
I haven't used Sciptler myself apart from some experimentation, but your question posed an interesting problem. I hope this helps!
The only way I could get this to work (on Linux) was to follow this advice:
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script
import hudson.model.*
// get current thread / Executor and current build
def thr = Thread.currentThread()
def build = thr?.executable
// if you want the parameter by name ...
def hardcoded_param = "FOOBAR"
def resolver = build.buildVariableResolver
def hardcoded_param_value = resolver.resolve(hardcoded_param)
println "param ${hardcoded_param} value : ${hardcoded_param_value}"
This is on Jenkins 1.624 running on CentOS 6.7
Jenkins 2.x has the global variables. env is one of them from any script...
println env.JOB_NAME
More at https://build.intuit.com/services-config/pipeline-syntax/globals#env
One thing to note, if you are using a freestyle job, you won't be able to access build parameters or the Jenkins JVM's environment UNLESS you are using System Groovy Script build steps. I spent hours googling and researching before gathering enough clues to figure that out.
In System Groovy Script (Jenkins 2.89), I was able to use the environmental variable to disable another Jenkins job
import jenkins.*
import jenkins.model.*
def env = binding.build.environment
Jenkins.instance.getItemByFullName(env.job_name).setDisabled(false)
I also added a conditional step so as to either enable or disable another Jenkins job.
Thanks #Allan Lewis, your comment was helpful.

Resources