I'm googling about a hour but can't find an answer
I have a script which checked date in database and compare it with date in file.
After if date in database are newer we need to ask user in Jenkins did he really want to proceed and build a job
Can we interact with user using groovy script in Jenkins?
My goal is:
1)User choose parameters and press Build with parameters
2)Shell script check date and if date in database newer run groovy script
3)Groovy script ask user : "Date in database are newer then in file. Do you really want to proceed?.Press yes or no"
4) And if user press "yes" - execute second shell script.
Here is a simplified version of a Pipeline. See the explanation for your steps.
Inorder to build with parameters, you can refer the following block. Here I'm using a String parameter, but you can use different input parameter types based on your requirement. Once the value is entered it will be assigned to a variable called TEST_STRING
parameters {
string(name: "TEST_STRING", defaultValue: "default", trim: true, description: "Sample string parameter")
}
In order to get the current date you can use the following script. Here from within the shell script, you can print the Database date to the standard out and then return it. (Note: Without knowing your shell logic, I can't give an exact solution, you may want to tweak this accordingly). As you can see below, the date will be returned and assigned to a variable called date. In the below example instead of echo $(date) you can add your shell logic. Also note the returnStdout: true flag will tell Jenkins to return the StandardOut of the shell step.
def date = sh(script: 'echo $(date)', returnStdout: true)
Once you receive the Database date you can do the check like below. If the Data is greater you can prompt the user for input. You need to implement the CheckDate(date) function according to the output you are getting from the DB. Simply CheckDate(date) would return true if DB date is greater.
if(CheckDate(date)) { //CheckDate here Simply
input id: 'Proceed', message: 'Date in database are newer then in file. Do you really want to proceed?', ok: 'Proceed!'
}
Next you can execute the second script if the user clicked on Proceed.
sh "echo 'Second Shell script!!! ${params.TEST_STRING}'"
Full Pipeline
pipeline {
agent any
parameters {
string(name: "TEST_STRING", defaultValue: "default", trim: true, description: "Sample string parameter")
}
stages {
stage('Test') {
steps {
script {
def date = sh(script: 'echo $(date)', returnStdout: true)
if(CheckDate(date)) { //CheckDate here
input id: 'Proceed', message: 'Date in database are newer then in file. Do you really want to proceed?', ok: 'Proceed!'
}
// If aborted you will not reach here.
sh "echo 'Second Shell script!!! ${params.TEST_STRING}'"
}
}
}
}
}
If you are using Build with Parameters then you can use ${params.VAR_NAME} to access the parameters.
Related
Whenever I run this pipeline in Jenkins I have to manually copy-paste some values from a YAML file in a remote Gitlab repository. What I would like to achieve is an auto-fill of the values that .
This is how my Jenkinsfile and the YAML look like:
Jenkinsfile
pipeline {
agent {
docker {
image 'artifactory...'
args "..."
}
}
parameters {
string(name: 'BACKEND_TAG_1', defaultValue: '', description: 'Tag...')
string(name: 'BACKEND_TAG_2', defaultValue: '', description: 'Tag...')
}
stage('prepare') {
steps {
script {
dir('application') {
git url: env.PIPELINE_APPLICATION_GIT_URL, branch: env.PIPELINE_APPLICATION_GIT_BRANCH
}
Values = readYaml file: 'application/values.yaml'
values.yaml
version:
default: 0.1.2
company_tag_1: 0.1.124
company_tag_2: 0.1.230
So I need to loop into the parameters and assign the corresponding values:
Values.each { Value ->
Value.version.minus('company')
/* This value should be assigned to the corresponding parameter BACKEND_TAG_* parameter.
e.g.: BACKEND_TAG_1.default=company_tag_1
BACKEND_TAG_2.default=company_tag_2
*/
}
Reading the YAML works fine but I don't know how to proceed in the assignment of the values.
I presume you would like to populate all parameters before click Build button. I mean after clicking "Build with Parameters" button, you basically would like to see your parameters are populated from your YAML file.
If this is the case You can use Active Choice Parameter or Extended Choice Parameter plugins for this purpose. These Plugins are able to run Groovy Script, so you can develop a small Groovy Script read and select parameters automatically.
I have Jenkins Pipeline which is triggering for different projects. However the only difference in all the pipelines is just the name.
So I have added a parameter ${project} in parameter of jenkins and assigned it a value of the name of the project.
We have a number of projects and I am trying to find a better way through which I can achieve this.
I am thinking how can we make the parameter run with different parameters for all the projects without actually creating different projects under jenkins.
I am pasting some screenshot for you to understand what exactly I want to achieve.
As mentioned here, this is a radioserver project, having a pipeline which has ${project} in it.
How can I give multiple values to that {project} from single jenkins job?
IF you have any doubts please message me or add a comment.
You can see those 2 projects I have created, it has all the contents same but just the parameterized value is different, I am thinking how can I give the different value to that parameter.
As you can see the 2 images is having their default value as radioserver, nrcuup. How can I combine them and make them run seemlessly ?
I hope this will help. Let me know if any changes required in answer.
You can use conditions in Jenkins. Based on the value of ${PROJECT}, you can then execute the particular stage.
Here is a simple example of a pipeline, where I have given choices to select the value of parameter PROJECT i.e. test1, test2 and test3.
So, whenever you select test1, jenkins job will execute the stages that are based on test1
Sample pipeline code
pipeline {
agent any
parameters {
choice(
choices: ['test1' , 'test2', 'test3'],
description: 'PROJECT NAME',
name: 'PROJECT')
}
stages {
stage ('PROJECT 1 RUN') {
when {
expression { params.PROJECT == 'test1' }
}
steps {
echo "Hello, test1"
}
}
stage ('PROJECT 2 RUN') {
when {
expression { params.PROJECT == 'test2' }
}
steps {
echo "Hello, test2"
}
}
}
}
Output:
when test1 is selected
when test2 is selected
Updated Answer
Yes, it is possible to trigger the job periodically with a specific parameter value using the Jenkins plugin Parameterized Scheduler
After you save the project with some parameters (like above mentioned pipeline code), go back again to the Configure and under Build Trigger, you can see the option of Build periodically with parameters
Example:
I will here run the job for PROJECT=test1 every even minutes and PROJECT=test2 every uneven minutes. So, below is the configuration
*/2 * * * * %PROJECT=test1
1-59/2 * * * * %PROJECT=test2
Please change the crontab values according to your need
Output:
When I'm in "build with parameters" before starting job, I want to pass the value entered in a textbox to the Job. I´m using Active choice and Active choice reactive parameter like this:
This is the groovy script which I then use to run job and show output. But I´m getting NULL on echo command.
node {
def commit = params.val
stage ('Pulling code from Bitbucket') {
git branch: 'master',
credentialsId: '2bbc73c4-254e-45bd-85f4-6a169699310c',
url: 'git#bitbucket.org:repo/test.git'
sh (""" echo ${commit}""")
}
}
Which is the correct way to pass parameter into build ?
From your output, you have defined a parameter named ID1 that references some other parameter named OPTIONS. The correct way to reference these parameters is params.ID1 and params.OPTIONS. I can't see a parameter named val that can be addressed by params.val.
Below is a basic construct I built to present my question.
stage('Run pre-checks') {
steps {
//Run pre-check scripts
}
}
stage('Deploy config ') {
when {
expression {
//Insert expression here
}
}
steps {
//Run a script
}
}
Based on the construct below, what I am trying to do is to run a specific script only when a specific output from the “run pre-checks” script is displayed on the stdout of that stage, which will be a python script. If the pre-check shows this output, you will skip the second step rather than running it. Would anyone know the environment variable(s) and/or Methods that would be able to do this?
Sh command can return a value instead of printing it.
MY_OUTPUT = sh (
script: '... your command....',
returnStdout: true
)
Then you can do whatever you want with MY_OUTPUT (trim(), substr...)
The current link to the documentation is here (Sh shell script) :
returnStdout (optional)
If checked, standard output from the task is returned as the step value as a String, rather than being printed to the build log. (Standard error, if any, will still be printed to the log.) You will often want to call .trim() on the result to strip off a trailing newline.
Type: boolean
I am trying to setup a Jenkinsfile that gets a job to call other jobs, based on the parameters passed into itself.
Instead of having multiple when conditions, I'm thinking that it would be smarter (and manageable for future scaling) if the names of the jobs being called would ideally be concatenating a common prefix with the parameter being passed in, for example:
CICD_api-gateway
CICD_front-end
CICD_customer-service
I'm having difficulty mixing string interpolation with string concatenation to achieve this:
build job: 'CICD_"${params.SERVICE_NAME}"', wait : false
In Linux, we are able to use eval to achieve this. I'm not sure what the equivalent is in Jenkinsfile syntax.
The full code below:
pipeline {
agent any
parameters { string(name: 'SERVICE_NAME', defaultValue: '', description: 'Service being deployed.') }
stages {
stage('Build Trigger'){
steps{
echo "CICD_${params.SERVICE_NAME}"
build job: 'CICD_"${params.SERVICE_NAME}"', wait : false
}
}
}
}
Change it to be a Gstring from the beginning, no need for the single quotes:
build job: "CICD_${params.SERVICE_NAME}", wait : false