How to use source command within Jenkins pipeline script
Replace source environment.sh
with
. ./environment.sh
Please note there is a space after first dot.
How to source bash script in Jenkins Pipeline enviornment section
There are two ways to load ( or source file in jenkins):-
- Using load function of jenkins ( used to load file from local path )
- Using fileLoader function ( used to load file located at remote location i.e. any SCM only file with .groovy extension will be loaded)
The file should contains (loadFile.sh or loadFile.groovy) the data as below:-
// Comments should start like this one
env.Location='Pune'
env.Day='Friday'
env.Job_UI='Jenkins'
You can use the below code with some modifications:-
stages {
stage('Load') {
agent { node { label 'master' } }
steps {
script {
// From the below two lines of code, you can use the one suits your need better
// def localenv = fileLoader.fromGit ("./Vars/loadFile.sh", "GITURL", 'BRANCH_NAME', 'Credentials', '')
load '/u/users/admin/loadFile.sh'
}
}
}
stage('Print') {
agent { node { label 'master' } }
steps {
script {
echo "Location :- ${env.Location}"
echo "Day :- ${env.Day}"
echo "Job :- ${env.Job_UI}"
}
}
}
}
}
How to source multiple scripts in different locations in jenkins
As Nahuel Fouilleul mentioned in his answer this is just one line and the script and yml files are other arguments and as I mentioned in the comment to Nahuel Fouilleul the problem is because of "[[" I can't source that script in shell environment of jenkins (even if the script has its own shebang) so I added the shebang to the shell block in jenkins as below and now it works.
sh '''#!/bin/bash -xe
. ../env/scriptA.sh arg-1 ../env/scriptB.sh ../compose/build.yml arg-2
echo "other commands"
'''
Jenkins - source: not found
source
isn't a standard shell command; it's not one of the "special built-in utilities" in the POSIX.1 spec. Some shells happen to have a command named source
but it's not required to be present.
There is a similar standard command .
that executes a file in the context of the current shell. If you're using the bash-specific source
, you can usually just change that to the standard .
without making any further changes
. ./login
Note that .
searches $PATH
for the file to run; it will not search the current directory unless specifically told to. Also note that you typically only use .
for scripts that have side effects like setting environment variables, and in a context like what you show where each command is running in a separate shell, this won't have longer-lasting effects.
Since the output of the aws ecr get-login
command is a single docker login
command that doesn't directly change the shell context, you can also just run it as a shell script
sh ./login
Declarative pipeline - Running a shell command
Declarative pipeline with 'sh' step will look like:
stage ("Preparing") {
steps {
sh'''
export PATH=\"${PATH}\":\"${WORKSPACE}\"
BASE_DIR=$(dirname $0)
source "${BASE_DIR}/shellscript.sh"
helm uninstall ${helmReleaseName} --namespace ${kubenamespace}
'''
}
}
Take a look here
Run bash command on jenkins pipeline
The Groovy script you provided is formatting the first line as a blank line in the resultant script. The shebang, telling the script to run with /bin/bash instead of /bin/sh, needs to be on the first line of the file or it will be ignored.
So instead, you should format your Groovy like this:
stage('Setting the variables values') {
steps {
sh '''#!/bin/bash
echo "hello world"
'''
}
}
And it will execute with /bin/bash.
How do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?
The latest version of the pipeline sh
step allows you to do the following;
// Git committer email
GIT_COMMIT_EMAIL = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()
echo "Git committer email: ${GIT_COMMIT_EMAIL}"
Another feature is the returnStatus
option.
// Test commit message for flags
BUILD_FULL = sh (
script: "git log -1 --pretty=%B | grep '\\[jenkins-full]'",
returnStatus: true
) == 0
echo "Build full flag: ${BUILD_FULL}"
These options where added based on this issue.
See official documentation for the sh
command.
For declarative pipelines (see comments), you need to wrap code into script
step:
script {
GIT_COMMIT_EMAIL = sh (
script: 'git --no-pager show -s --format=\'%ae\'',
returnStdout: true
).trim()
echo "Git committer email: ${GIT_COMMIT_EMAIL}"
}
Related Topics
What Is The Size of Coap Packet
Linux: How to Put a Load on System Memory
Redirecting Apache Logs to Stdout
Mongo Daemon Doesn't Run by Service Mongod Start
How to Compile Intel MAC Binaries on Linux
Checking for Null String in Bash
How to Send Signal from One Program to Another
How to Use Gpg Signing Key on a Remote Server
In a Sigill Handler, How to Skip The Offending Instruction
How Does The Os Know The Real Size of The Physical Memory
How to Manually Install The Eclipse-Cdt Plugin from an Archive/Zip on Ubuntu
Find Files Modified Over 1 Hour Ago But Less Than 3 Days
Command to Check Status of Message Queue and Shared Memory in Linux
How to Find Sample Source Files for Linux Device Drivers, Third Edition
How to Take Advantage of The Vdso Object with Your Own Programming Language
Can't Access Publicly Exposed Docker Container Port from External Machine, Only from Localhost