Run Bash Command on Jenkins Pipeline

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.

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

How do I make Jenkins 2.0 execute a sh command in the same directory as the checkout?

You can enclose your actions in dir block.

checkout scm
stage "Build Pex"
dir ('<your new directory>') {
sh('./build.sh')
}
... or ..
checkout scm
stage "Build Pex"
sh(""" <path to your new directory>/build.sh""")

...

<your new directory> is place holder your actual directory. By default it is a relative path to workspace. You can define absolute path, if you are sure this is present on the agent.

Executing shell commands from inside Pipeline Shared Library

Yes, it is possible but not really an obvious solution. Every call that is usually done in the Jenkinsfile but was moved to the shared-library needs to reference the steps object you passed.

You can also reference the Jenkins environment by calling steps.env.

I will give you a short example:

class Deployer implements Serializable {
def steps
Deployer(steps) {
this.steps = steps
}

def callMe() {
// Always call the steps object
steps.echo("Test")
steps.echo("${steps.env.BRANCH_NAME}")
steps.sh("ls -al")

// Your command could look something like this:
// def process = steps.sh(script: "ls -l", returnStdout: true).execute(steps.env, null)
...
}
}

You also have to import the object of the shared library and create an instance of it. Define the following outside of your Pipeline.

import com.mypackage.Deployer // path is relative to your src/ folder of the shared library
def deployer = new Deployer(this) // 'this' references to the step object of the Jenkins

Then you can call it in your pipeline as the following:

... script { deployer.test() } ...

execution of shell command from jenkinsfile

Your problem here is that all the $ are interpreted by jenkins when the string is in double quotes. So the first 2 times there's no problem since the first variable comes from jenkins and the second time it's a single quote string.
The the third variable is in a double quote string, therefore jenkins tries to replace the variable with its value but it can't find it since it's generated only when the shell script is executed.

The solution is to escape the $ in $client_name (or define client_name in an environment block).

I rewrote the block:

String nodeLabel = env.PrimaryNode ? env.PrimaryNode : "slave1"
echo "Running on node [${nodeLabel}]"

node("${nodeLabel}"){
sh "p4 print -q -o config.yml //c/test/gradle/hk/config.yml"
def config = readYaml file: 'devops-config.yml'
def out = sh (script: """
client_name=${config.BasicVars.p4_client}
echo "client name: \$client_name"
cmd_output = p4 clients -e \$client_name
echo "out variable: \$cmd_output"
""", returnStdout: true)
}


Related Topics



Leave a reply



Submit