I have a jenkins pipeline script that I am updating wish to use the following shell command:
sh script: """
export PATH=\"${PATH}\":\"${WORKSPACE}\"
BASE_DIR=$(dirname $0)
source "${BASE_DIR}/shellscript.sh"
helm uninstall ${helmReleaseName} --namespace ${kubenamespace}
"""
And the result always is:
Errors encountered validating Jenkinsfile:
I've played around with it.
But it fails the validation? The question is why?
Thanks
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
Related
I have tried this command in Jenkins pipeline script
sh ‘curl ${BUILD_URL}consoleText —-output ${WORKSPACE}/logs/log.txt’
But it’s not resolving the WORKSPACE variable.Any idea how to proceed.
You can use it in below way:
def Workspace=env.WORKSPACE
sh "<Yourcommand> ${Workspace}"
I'm trying to run a terraform commands from Jenkinsfile stage. The code I'm using is as below:
node {
checkout(scm)
stage ('Templates Deployment'){
sh "terraform init"
}
}
This fails with the error as :
+terraform init
/var/lib/jenkins/workspace/Terraform-Code/#tmp/durable-df843027/script.sh: line 1: terraform: command not found
Terraform is installed on the Jenkins server. When I execute the terraform init command from the server(CLI), it works fine.
But while running it from the Jenkinsfile(console) it's throwing this error.
Can someone please suggest how this error can be resolved? Any help to execute terraform commands via Jenkinsfile is highly appreciated.
Configure Terraform
Go to Manage Jenkins > Global Tool Configuration > It will display Terraform on the list.
give full path of terraform binary or set PATH before terraform init
`node {
checkout(scm)
stage ('Templates Deployment'){
sh """
PATH=/bin/terraform
terraform init"
}
}`
You can set the PATH inside the environment block:
pipeline {
agent any
environment {
PATH = "/usr/local/bin/:$PATH"
}
stages{
stage("first stage"){
steps{
sh "cd /Users/<user>/Terraform/proj1"
sh "pwd"
sh "terraform"
}
}
}
}
I am stuck in trying to get a Jenkinsfile to work. It keeps failing on sh step and gives the following error
process apparently never started in /home/jenkins/workspace
...
(running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)
I have tried adding
withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin'])
before sh step in groovy file
also tried to add
/bin/sh
in Manage Jenkins -> Configure System in the shell section
I have also tried replacing the sh line in Jenkinsfile with the following:
sh "docker ps;"
sh "echo 'hello';"
sh ./build.sh;"
sh ```
#!/bin/sh
echo hello
```
This is the part of Jenkinsfile which i am stuck on
node {
stage('Build') {
echo 'this works'
sh 'echo "this does not work"'
}
}
expected output is "this does not work" but it just hangs and returns the error above.
what am I missing?
It turns out that the default workingDir value for default jnlp k8s slave nodes is now set to /home/jenkins/agent and I was using the old value /home/jenkins
here is the config that worked for me
containerTemplate(name: 'jnlp', image: 'lachlanevenson/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}', workingDir: '/home/jenkins/agent')
It is possible to get the same trouble with the malformed PATH environment variable. This prevents the sh() method of the Pipeline plugin to call the shell executable. You can reproduce it on a simple pipeline like this:
node('myNode') {
stage('Test') {
withEnv(['PATH=/something_invalid']) {
/* it hangs and fails later with "process apparently never started" */
sh('echo Hello!')
}
}
}
There is variety of ways to mangle PATH. For example you use withEnv(getEnv()) { sh(...) } where getEnv() is your own method which evaluates the list of environment variables depending on the OS and other conditions. If you make a mistake in the getEnv() method and PATH gets overwritten you get it reproduced.
Im trying to set a environment variable(VIRTUALENV) in Jenkins - stage(check_style) and use that in the shell but it throws a error.
withEnv(['VIRTUAL_ENV=${env.WORKSPACE}/venv']){
stage ('Check_style') {
sh """
export PATH=${VIRTUAL_ENV}/bin:${PATH}
make flake8 | tee report/flake8.log || true
"""
}
}
Error:-
PATH=${env.WORKSPACE}/venv/bin:/usr/bin:/bin:/usr/sbin:/sbin: bad substitution
withEnv(["VIRTUAL_ENV=${env.WORKSPACE}/venv"]) should work
I am having trouble getting a shell command to complete in a stage I have defined:
stages {
stage('E2E Tests') {
steps {
node('Protractor') {
checkout scm
sh '''
npm install
sh 'protractor test/protractor.conf.js --params.underTestUrl http://192.168.132.30:8091'
'''
}
}
}
}
The shell command issues a protractor call which takes a config file argument, but this file fails to be found when protractor tries to retrieve it.
If I take a look at the workspace directory for where the repo is checked out to from the checkout scm step I can see the test directory is present with the config file present the sh step is referencing.
So I'm unsure why the file cannot be found.
I thought about trying to verify the files that can be seen around the time the protractor command is being issued.
So something like:
stages {
stage('E2E Tests') {
steps {
node('Protractor') {
checkout scm
def files = findFiles(glob: 'test/**/*.conf.js')
sh '''
npm install
sh 'protractor test/protractor.conf.js --params.underTestUrl http://192.168.132.30:8091'
'''
echo """${files[0].name} ${files[0].path} ${files[0].directory} ${files[0].length} ${files[0].lastModified}"""
}
}
}
}
But this doesnt work, I dont think findFiles can be used inside a step?
Can anyone offer any suggestions about what may be going on here?
Thanks
to do the debugging you were attempting (to see if the file is actually there) you could wrap the findFiles in a script (making sure your echo is before the step that fails) or use a basic find in an "sh" step like this:
stages {
stage('E2E Tests') {
steps {
node('Protractor') {
checkout scm
// you could use the unix find command instead of groovy's findFiles
sh 'find test -name *.conf.js'
// if you're using a non-dsl-step (like findFiles), you must wrap it in a script
script {
def files = findFiles(glob: 'test/**/*.conf.js')
echo """${files[0].name} ${files[0].path} ${files[0].directory} ${files[0].length} ${files[0].lastModified}"""
sh '''
npm install
sh 'protractor test/protractor.conf.js --params.underTestUrl http://192.168.132.30:8091'
'''
}
}
}
}
}