Pass a value to a shell script from jenkins pipeline - bash

How to pass values to a shell script from jenkins during the runtime of the pipeline job.
I have a shell script and want to pass the values dynamically.
#!/usr/bin/env bash
....
/some code
....
export USER="" // <--- want to pass this value from pipeline
export password="" //<---possibly as a secret
The jenkins pipeline executes the above shell script
node('abc'){
stage('build'){
sh "cd .."
sh "./script.sh"
}
}

You can do something like the following:
pipeline {
agent any
environment {
USER_PASS_CREDS = credentials('user-pass')
}
stages {
stage('build') {
steps {
sh "cd .."
sh('./script.sh ${USER_PASS_CREDS_USR} ${USER_PASS_CREDS_PSW}')
}
}
}
}
The credentials is from using the Credentials API and Credentials plugin. Your other option is Credentials Binding plugin where it allows you to include credentials as part of a build step:
stage('build with creds') {
steps {
withCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
// available as an env variable, but will be masked if you try to print it out any which way
// note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
sh 'echo $PASSWORD'
// also available as a Groovy variable
echo USERNAME
// or inside double quotes for string interpolation
echo "username is $USERNAME"
sh('./script.sh $USERNAME $PASSWORD')
}
}
}
Hopefully this helps.

Related

Run a set of linux commands using Jenkinsfile in Jenkins

I had to create a jenkins job to automate certain tasks that will perform certain operations like Updating the public site, Changing public version to latest public release, Updating Software on public site and Restarting Server these include certain operations such as copy files to a tmp folder, log in to a an on-prem server, go to the folder and unzip the file etc.
I have created the jenkinsfile as follows:
pipeline {
options {
skipDefaultCheckout()
timestamps()
}
parameters {
string(name: 'filename', defaultValue: 'abc', description: 'Enter the file name that needs to be copied')
string(database: 'database', defaultValue: 'abc', description: 'Enter the database that needs to be created')
choice(name: 'Run', choices: '', description: 'Data migration')
}
agent {
node { label 'aws && build && linux && ubuntu' }
}
triggers {
pollSCM('H/5 * * * *')
}
stages {
stage('Clean & Clone') {
steps {
cleanWs()
checkout scm
}
}
stage('Updating the public site'){
steps{
sh "scp ./${filename}.zip <user>#<server name>:/tmp"
sh "ssh <user>#<server name>"
sh "cp ./tmp/${filename}.zip ./projects/xyz/xyz-site/"
sh "cd ./projects/xyz/xyz-site/ "
sh "unzip ./${filename}.zip"
sh "cp -R ./${filename}/* ./"
}
stage('Changing public version to latest public release') {
steps {
sh "scp ./${filename}.sql.gz <user>#<server name>:/tmp"
sh "ssh <user>#<server name>"
sh "mysql -u root -p<PASSWORD>"
sh "show databases;"
sh "create database ${params.database};"
sh "GRANT ALL PRIVILEGES ON <newdb>.* TO 'ixxyz'#'localhost' WITH GRANT OPTION;"
sh "exit;"
sh "zcat tmp/${filename}.sql.gz | mysql -u root -p<PASSWORD> <newdb>"
sh "db.default.url="jdbc:mysql://localhost:3306/<newdb>""
sh "ps aux|grep monitor.sh|awk '{print "kill "$2}' |bash"
}
}
stage('Updating Software on public site') {
steps {
sh "scp <user>#<server>:/tmp/abc<version>_empty_h2.zip"
sh "ssh <user>#<server name>"
sh "su <user>"
sh "mv tmp/<version>_empty_h2.zip ./xyz/projects/xyz"
sh "cd xyz/projects/xyz"
sh "cp latest/conf/local.conf <version>_empty_h2/conf/"
}
}
stage('Restarting Server') {
steps {
sh "rm latest/RUNNING_PID"
sh "bash reload.sh"
sh "nohup bash monitor.sh &"
}
}
}
}
Is there a way I can dynamically obtain the zip filename in the root folder? I used ${filename}.zip , but it doesn't seem to work.
Also, is there a better way to perform these operations using jenkins? Any help is much appreciated.
You could write all your steps in one shell script for each stage and execute under one stage.
Regarding filename.zipeither you can take this as a parameter and pass this value to your stages. OR You can also use find command as a shell command or shell script to find .zip files in a current directory. find <dir> -iname \*.zip find . -iname \*.zip .
Example:
pipeline {
options {
skipDefaultCheckout()
timestamps()
}
parameters {
string(name: 'filename', defaultValue: 'abc', description: 'Enter the file name that needs to be copied')
choice(name: 'Run', choices: '', description: 'Data migration')
}
stage('Updating the public site'){
steps{
sh "scp ./${params.filename}.zip <user>#<server name>:/tmp"
...
}
}
}
For executing script at a certain location based on your question , you could use dir with path where your scripts are placed.
OR you can also give the path directly sh label: 'execute script', script:"C:\\Data\\build.sh"
stage('Your stage name'){
steps{
script {
// Give path where your scripts are placed
dir ("C:\\Data") {
sh label: 'execute script', script:"build.sh <Your Arguments> "
...
}
}
}
}

How to pass "Json string" from groovy variable to shell variable with in the groovy function

I am trying to create shared libs for jenkins to build the app. When I am trying to pass the the json string from groovy function to shell block for build command execution. whereas json string passing without quotes. How to retain the quotes.
stage('build app') {
steps {
script {
build project:"TestApp.xcodeproj",
workspace: "TestApp.xcworkspace",
scheme: "Develop",
config: "Debug",
target: "{ "TestApp": { "info_plist": "TestApp/Info.plist", "profile_name": "Test App Debug (January 2021)", "app_id": "com.******.Debug" } }"
}
}
}
def build(Map buildParams) {
sh """#!/bin/bash -l
export XCODE_PROJ="${buildParams.project}"
export XCODE_WORKSPACE="${buildParams.workspace}"
export XCODE_BUILD_SCHEME="${buildParams.scheme}"
export XCODE_BUILD_CONFIGURATION="${buildParams.config}"
export XCODE_TARGET_JSON="${buildParams.target}"
#build App
fastlane build app
"""
}
Expecting the json string as it is in shell block with "Quotes". Whereas getting error expecting '}' found :. When i escape the quotes of json strings, getting, values without "quotes"
{ TestApp: { info_plist: TestApp/Info.plist, profile_name: Test App Debug (January 2021), app_id: com.******.Debug } }
which results fastlane throwing error invalid token. How to retain the quotes in shell block variable
Escaping the quotes in json string with backslash() is worked for me.
'{ "TestApp": { "info_plist": "TestApp/Info.plist", "profile_name": "Test App Debug (January 2021)", "app_id": "com.******.Debug" } }'

How to pass shell variable to ansible playbook command

I have this Jenkins pipeline where I need to run ansiblePlaybook() command with inventory file. Inventory file does contain date (current date) part (my-instance-ips-(mm-dd-yyyy)). Here, I am facing issue in creating currentDate variable and pass into pipeline script
Jenkins File:
pipeline {
agent any
stages {
stage ( 'Executing shell script' ) {
steps {
script {
sh """
currentDate = "\$(date +'%m-%d-%Y')"
inventory_file = "my-instance-ips-{$currentDate}.yml"
ansiblePlaybook (
playbook: 'task.yml',
inventory: $inventory_file,
installation: 'ansible-2.6.5','
-e "DATE = $currentDate")
"""
}
}
}
}
}
Error Message:
groovy.lang.MissingPropertyException: No such property: currentDate for class: groovy.lang.Bindin
Could someone help me out to create current date in pipeline script and the same should pass over to ansible playbook command?
Looks like you are invoking ansible plugin. If that is what you are trying to achive, then your ansible playbook call should not be inside sh step.
You need to get the command's output first then invoke ansible plugin.
import java.time.format.DateTimeFormatter
pipeline {
agent any
stages {
stage ( 'Executing shell script' ) {
steps {
script {
/* currentDate = sh (
script: "date +'%m-%d-%Y'"
returnStatus: true
).trim()
*/
cDate = java.time.LocalDate.now()
currentDate = cDate.format(DateTimeFormatter.ofPattern("MM-dd-yyyy"))
inventory_file = "my-instance-ips-${currentDate}.yml"
println inventory_file
ansiblePlaybook ([
playbook: 'task.yml',
// credentialsId: 'xxxx',
disableHostKeyChecking: true,
inventory: "${inventory_file}",
extraVars: [
DATE: "${currentDate}"
]
])
}
}
}
}
}

Protractor passing parameters in script run command

I need to pass the credentials in command running a script.
For now, I am using in protractor file following part:
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
}
}));
if (browser.params.Url == 'http://devel/') {
browser.params.webmaster='abc';
browser.params.webmaspass='foo';
}
//(other environments)
else {
console.log('-------------error during log in');
}*/
}
and it was working fine, but I need to change it - I can't pass credentials in this way. I thought about changing it to:
if (browser.params.Url == 'http://devel/') {
browser.params.webmaster='';
browser.params.webmaspass='';
}
and run the script using
npm run dev-script --browser.params.Url='http://devel/' --browser.params.webmaster='abc' --browser.params.webmaspass='foo'
where package.json I have:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev-script": "protractor --params.Url=http://devel/ --browser.params.webmaster='' --browser.params.webmaspass=''"
},
(or any variation) But it fails - I can't update params during running script, I need to write down the credentials in the code (which I find a little unsafe)
I found issues like Protractor needs password for login => insecure? but it about Google Auth problems
Any idea?
You need to remove the variable assignment in the onPrepare. You are overwriting what you are passing in from the command line by setting it to an empty string.
When you pass them in from the command line they will be availble on the params object. There is no need to set them again in your onPrepare. Add a console.log() in your onPrepare and you will see.
Run it from the command line like this: protractor conf.js --params.webmaster=abc --params.webmaspass=foo --params.url=http://devel/
Again, if you log them in your onPrepare you will see that it is working. The way you currently have it you are just overwriting the values you are passing in through the command line.
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
}
}));
if (browser.params.Url == 'http://devel/') {
consoel.log(browser.params.webmaster) //should be abc
console.log(browser.params.webmaspass) //should be foo
}
//(other environments)
else {
console.log('-------------error during log in');
}*/
}
Another way you can do this is to set some environment variables before your test run and then you can access them in your scripts by using process.env.envVariableName or ${envVariableName}. Both ways will work.
set DEVEL_WEBMASTER=abc
set DEVEL_WEBMASPASS=foo
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: true
}
}));
if (browser.params.Url == 'http://devel/') {
browser.params.webmaster=process.env.DEVEL_WEBMASTER;
browser.params.webmaspass=process.env.DEVEL_WEBMASPASS;
}
//(other environments)
else {
console.log('-------------error during log in');
}*/
}
Just remember that if you use this method you would have to set the variables for each session. If you are planning to automate these tests using a CI environment you can just add them there as secret variables (if you have that option) and they will always be there ready and waiting. There will be no need to set them manually during each build.
What I did it here was create the scripts in my package.json:
scripts: {
"automation-test": "concurrently --raw --kill-others \"./node_modules/.bin/webdriver-manager start\" \"sleep 5 && ./node_modules/.bin/protractor configuration/protractor.config.js\"",
"automation:pending": "TAGS=#pending npm run automation-test"
}
And in my protractor.conf.js I just assign the value to a variable so I can use in my config. Like this:
let tags = process.env.TAGS;
Then the command that I run is just this:
npm run automation:pending
but I could pass the TAGS like this as well:
npm run automation-test TAGS=#pending
I have not seen the configuration file on the parameters of the command line. you must specify the configuration file:
example: protractor config.js --params ......
Do this in your script file: i have added a config file after the command protractor
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev-script": "protractor config.js --params.Url=http://devel/ --browser.params.webmaster='' --browser.params.webmaspass=''"
},

Can't read a file parm in jenkins pipeline

example :
pipeline{
agent any
stages{
stage('Parse CSV'){
steps {
script{
def fileToParse = readFile(params.FileLocation)
}
echo fileToParse
}
}
}
}
I configured the job from the GUI, the file location parameter is called FileLocation. I uploaded a file and tried to read it. When I try to access params.FileLocation it returns null, as if it doesn't recognise it.
Your problem is with the variable scope. You def the variable in the script {} block scope, then try to use it outside of it. One easy fix is to def the variable outside the pipeline {} block at the global level. Or, just use the params.FileLocation in your echo statement.
def fileToParse
pipeline{
agent any
stages{
stage('Parse CSV'){
steps {
script{
fileToParse = readFile(params.FileLocation)
}
echo fileToParse
echo params.FileLocation
}
}
}
}
file param is not supported and it is removed from documentation as well.
https://issues.jenkins-ci.org/browse/JENKINS-27413
Check available parameters: https://jenkins.io/doc/book/pipeline/syntax/#parameters

Resources