How to set schedule of a periodically built job by environment variables in Jenkins - jenkins

In Jenkins you can set a job to build periodically using a cron-like time definition. e.g.
# switch timezone
TZ=Etc/GMT+6
# build once anywhere between 13:00 - 23:59
H H(13-23) * * *
Further you can configure parameterized jobs. That means on the go you can set the value of some predefined environment variables which will be used in the configuration. e.g. a certain branch on git you can let the user set the value of the environment variable "BRANCH" and than access this value using
${BRANCH}
in the configuration.
But this seems not to work with the Schedule value of a periodically build job configuration.
My problem:
I'm using the Job-Generator Plugin. It basically creates new (not parameterized but periodically running) jobs while using its own configuration as template.
In order to generate different jobs for different repositories you use the parameterized build as descriped before so in the generated job configuration the variable names ${...} are replaced by the value.
Now I don't want the Generator to run periodically but ofcourse only on demand. Therefor I want to replace the before mentioned cron rule by a variable so itself isn't build over night.
I tried to set CRON1 (TZ=Etc/GMT+6) and CRON2(H H(13-23) * * *) as unchngeable Generator variables and use
Schedule
${CRON1}
${CRON2}
but this makes Jenkins break and throw an error on trying to save the generator config.
line 1:1: unexpected char: '$'
How can I set the schedule value using an environment variable?
(I'm not trying to do Jenkins scheduled build Triggers with environment variable? . I'm using this but this doesn't solve my problem of the Job-Generator running periodically itself what it shouldn't)

You can't access the variables in that block, Instead you can use plugins to schedule the jobs. parameterized-scheduler-plugin this plugin can be helpful to you in your case. It does have good integration with pipeline scripting also. Hope this helps.

Related

Run Jenkins job with different environment variable

I'm using jenkins 1.651. We have a Job which contains a shell. The shell contains:
echo ${VAR}
We are using the EnvInject plugin to define the content of our environment variable:
VAR=10
But sometimes we want to use another value for our env var:
VAR=20
Our env var must ben 10 in 50% of the cases, and 20 in the other 50%.
Is there a way or plugin which can help us to define easily how we want to run our job? With VAR=10 or VAR=20?
We don't want to put VAR=10 in comment, every time we want to run VAR=20 and vice versa. So preferably a way which works withouth going inside our job configuration.
See Use Jenkins > Parameterized Build:
The parameter are available as environment parameters. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
or the more sophisticated Extensible Choice Parameter plugin and Active Choices Plugin.

Using parameterized credentials in Jenkins

Say I've got dev, qa, and stable server environments for some web app, with corresponding git branches. Each environment should be continuously integrated. Each of these environments has a separate username/password pair used to publish the app. I would like to make a Jenkins multiconfiguration (matrix) job to publish to all of these environments. The publishing almost certainly must be done with a shell script.
My failed attempt consisted of using the Jenkins Credentials and Credentials Binding plugins. Credentials Binding provides a way to inject credentials as environment variables using a parameter. However, setting this parameter dynamically (i.e., something like if ENV == dev: CREDS = CREDS_dev) doesn't appear to be possible. Build scripts happen afterwards, and even using the Environment Script plugin doesn't work.
Is there any way for this to happen?
Had similar situation and used groovy script with parameterized build (https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin). In my case I had a choice parameter defined as "DEPLOY" and had different values, like "Test", "Release", then in the following groovy script (Evaluated Groovy script):
if ("Test".equals(DEPLOY)) {def map = [DEPLOY_URL: "http://someurl", DEPLOY_STORAGE: "testaccount"]; return map }
You should be able to specify your credentials in here or copy env variables. After that you can access these variables in windows batch command using:
echo %DEPLOY_URL%
echo %DEPLOY_STORAGE%
I also had another choice parameter defined "Deploy.Branch", with values of "dev" and "master". And used it as a parameter to Branches to Build, the value was set to (if you want to dynamically specify branch based on parameters):
*/${Deploy.Branch}
Hope this helps.
Here's what I ended up doing. It's kind of a workaround for what I would argue is a flawed design or missing use case in Jenkins.
Redid my creds so they have standard IDs (this is in the Advanced part and you can't set it after creation)
Matrix job runs a trivial script to figure out what env maps to what creds ID, then triggers...
The main job that does the deployment

How can I create parameterized Jenkins job?

I want to use same job in different machine. But I don't want to change the configuration of the job each time. Can I pass the machine name label as parameter and run the job in different machine ? (Not simultaneously).
I want to pass parameters while running a job to the script which I have written in th configuration (batch script). Can we do that ?
Can I get a return value from a job and use it in next job?
Example criteria:
User needs to use different environments for executions.
1. Select jenkins project
2. Job -> Configure -> Select this project is parameterized check box.
3. Select Add parameter drop down - > String parameter
4. Define a string parameter (In my example I use environment variable as : BaseURL)
5.Now under build section initialize the mvn command user needs to execute.Here in this case I want to execute my jenkins build on a environment where I specify. So I should be able to execute my build in different environment each time with out changing the code.
My mvn command: I pass my environment url as a parameter (Using $ sign). This is based on user requirement.
clean test -Dcustomproperty="$BaseURL"
6. Apply and save your Jenkins project.
7. Now your project is parameterized. Then your project should have Build with parameters option.
8.Click Build with parameters link and enter your parameter (In this example my environment variable) and click build. Your jenkins job should run with the parameter.
NOTE: This build won't succeed unless the passing parameter is not utilized in the automation script. So script has to be modified to retrieve the passing variable.
String BaseURL= System.getProperty("customproperty");
Yes, you can pass a node label parameter with NodeLabel Parameter Plugin.
Yes, you can define parameters, as described, in Parameterized Builds and then use it in your script as an environment variable:
The parameter is available as environment parameters. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
This is not exactly a return value, but you can pass any parameter (with its value) to the downstream job with Parameterized Trigger Plugin.
Use parameterized Build plugin and NodeLabel Parameter Plugin

Jenkins Promoted Build Parameter Value

I have a jenkins job that runs some tests and promotes the build if all tests pass.
I then have a second job that has a 'Promoted build parameter' which is used for deployments.
THe idea is that the deply job should let you pick one of the prompted builds for deploying. The issue I'm having is that I can pick a promoted build, but I have not idea how I access information about the build.
I've named the build parameter
SELECTED_BUILD
And according to the docs this should then be available via the environment.
The problem is that it doesn't seem to be bound to anything.
If I run a build step to exectute this sheel script:
echo $SELECTED_BUILD
echo ${SELECTED_BUILD}
The values are not interpolated / set.
Any idea how I can access the values of this param?
Many Thanks,
Vackar
Inject the information as variable user jenkins plugin (eject evn variable).
while triggering parameterized build on other job, pass above variable as parameter to next job.

How to change the configuration's by using the parametrized build based on the parameter?

choice parameter
Name
a
b
c
I have a parametrized build with choice parameters like a,b,c,d. When I select a parameter, it has to do fresh checkout, and, when I select b it has to update the work space of Jenkins.
Right now whatever the parameter choose either a,b,c, it is checkout policy is fresh checkout only.
Can any one let me know how to set different properties based on the selected parameter.
You would have to forego the Jenkins SCM checkout configuration, and maintain the SCM checkout through a script (either Execute Shell or Execute Batch Command). Your script would then handle the logic of doing a certain type of checkout/update based on passed parameters
Late edit:
You could configure a Pre-scm Build Step, and there run either a Conditional Build Step or a plain shell execution (bash or batch). In that shell, test the param, and if it matched b, then wipe the local workspace/checkout folder from the shell script.
When the rest of the job runs, it will do a fresh checkout (since the workspace/checkout is empty). With other param options, it will run the job normally, doing an update.
I haven't tried this. Your biggest issue may be if pre-scm build step does not have access to environment variables at that time.

Resources