I am trying to find out where Jenkins clone the GIT repo but I can't find it.
In my script in Jenkins pipeline I have:
stage('Do something'){
steps {
sh '''
#!/bin/bash
YAML_FILE="${JENKINS_HOME}/jobs/<JENKINS_JOB_NAME>/workspace/GIT_REPO_NAME/path/to/file.yaml"
...
However when I am trying to buid it, it can't find it in this path:
Error: open /efsmnt/jobs/<JENKINS_JOB_NAME>/workspace/GIT_REPO_NAME/path/to/file.yaml: no such file or directory
I also found that it could be in:
20:13:55 Error: open /home/jenkins/agent/workspace/<JENKINS_JOB_NAME>/GIT_REPO_NAME/path/to/file.yaml: no such file or directory
but with no luck.
The global environment WORKSPACE represents the job workspace where your repo will be clone into.
And Jenkins won't create GIT_REPO_NAME folder in job workspace when run git clone.
You can try below script
sh '''
pwd
ls -l
YAML_FILE="${WORKSPACE}/path/to/file.yaml"
'''
Use double quote in case job workspace path includes space when Jenkins job or Jenkins folder name includes space
Related
I am running a CI/CD pipeline as a test in Jenkins. The first task in this pipeline it to clone a repository
I am getting an error that says
cd /var/lib/jenkins/workspace/MyProjectPipeline-Dev/docker/apache
/var/lib/jenkins/workspace/MyProjectPipeline-Dev#tmp/durable-2f74d056/script.sh: line 9: cd: /var/lib/jenkins/workspace/MyProjectPipeline-Dev/docker/apache: No such file or directory
This pipeline is set up on an AWS EC2 instance. I installed git on this instance so I dont know why the clone isnt working.
Here is the log for the pipeline:
Because when you clone a git hub repo with git clone https://github.com/subsari/snippets.git it clones it into a directory snippets, so your docker/apache directory is actually inside the /var/lib/jenkins/workspace/MyProjectPipeline-Dev/snippets/
You need to cd as
cd /var/lib/jenkins/workspace/MyProjectPipeline-Dev/snippets/docker/apache
or you can also use the dir in your Jenkinsfile sa
dir("snippets/docker/apache"){
sh "pwd"
sh './script.sh'
}
I use Jenkins v2.222.1 and created a simple pipeline for a project. For the deployment I want to set the release/tag name manually. So the plan, how I want it working is:
I change the release name/number in a defined file, e.g. /root/of/my/project/release.txt.
I start deployment by clicking Build Now
Jenkins reads the release name/number from the file.
Jenkins checks out the appropriate tag (and creates and pushes the according branch).
pipeline {
agent {
label 'dev'
}
stages {
stage('build') {
environment {
APP_VERSION = sh (
script: 'eval "cat $REMOTE_ROOT_DIRECTORY/release.txt"', returnStdout: true
)
}
steps {
sh 'git fetch --all --tags'
sh 'git checkout ${APP_VERSION} -b v${APP_VERSION}'
...
}
}
...
}
}
Since I haven't found out, how to get the remote root directory path, I use a workaround: The $WORKSPACE is placed in the folder /workspace/$project_name (where the $project_name is the project name / title defined in Jenkins). So I just use this knowledge and define the path for cat as ../../release.txt. It works, but is a bit dirty because of the hard-coded path in the Jenkinsfile.
How to get / retrieve the project root directory dynamically in a Jenkins pipeline?
I have installed jenkins in my CI server machine and i am creating a pipeline to build my project. Jenkins build fails saying it couldn't find the path. However i have mentioned my workspace path in my pipeline code. Also i am using SVN as my sub version. Kindly assist.
The error i am getting is
+ cd var/lib/jenkins/workspace/ProjectDemo/target
/var/lib/jenkins/workspace/ProjectDemo#tmp/durable-a40648b0/script.sh: line 1: cd: var/lib/jenkins/workspace/ProjectDemo/target: No such file or directory
pipeline {
agent any
stages {enter code here
stage('Code Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'cd var/lib/jenkins/workspace/ProjectDemo/target; mvn clean package'
}
}
}
}
The better solution is use the Jenkins' build in Environment Variable WORKSPACE which represents the job's workspace where source code resides after cloned from SVN or Git.
stage('Build') {
steps {
sh '''
pwd
ls -l
cd ${WORPKSPACE}
echo $PATH
mvn clean package
'''
}
}
Above pwd is to print the path of job's workspace folder, ls -l is to print out files & folders under job's workspace folder. You can remove them if the job's workspace folder is your expected work dir.
I have a step in a Jenkins pipeline to copy some source files to the workspace.
stage('Copy Files') {
script {
echo 'Staging files'
sh "cp -ar /home/dev/src/ ${env.WORKSPACE}"
}
}
Yet, when I rerun the build it uses the old code. The only solution is to delete the workspace prior to the copy. In a normal Linux file system a copy will overwrite the destination. Why does Jenkins behave differently--i.e., old files are not overwritten? From the syntax it seems like it is just running a shell command, so why does this not have the expected behavior?
It is because, Jenkins run on master node and workspace will be on the worker node.
when checkout scm and sh "" code blocks are in different stages, files will not be save from first stage to other. You should use stash & unstash. when you stash a directory path, files in that dir will be available to the unstashed step in later stages.
Jenkins doc - here
I want to checkout a git repo and then run its build so I tried:
sh "git clone --depth 1 -b master git#github.com:user/repo.git"
build './repo'
but that yields:
ERROR: No item named ./repo found
I've tried to use dir('repo') but apparently that errors when you run it from within docker (because kubernetes is stuck on an old version of docker that doesnt support this).
Any idea on how to run the build pipeline from the checked out repo?
The 'build' pipeline steps expect a job name, not a pipeline folder with a Jenkinsfile in its root folder.
The correct way to do this is to set the pipeline job with the Jenkinsfile, as described here ('In SCM' section), and call it by its Job name from your pipeline.
Pipelines are not built for chaining unless you use shared libraries where you put the Pipeline code in a Groovy class or as a step, but that it is a subject for a full article.