Accessing workspace files from running docker image in jenkins - docker

I have a jenkins pipeline that runs a script saved in the Dockerimage. The script from the Dockerimage needs to have a cloned github repository in order to run. So desired repo is cloned and then I launch the script (it takes a parameter of the path to the local copy of the repo). My question is after I am in dockerExecute section running the container, can I access the local copy of the repo in my jenkins pipeline workspace?
node() {
stage('Clone GITHUB repo') {
git branch: 'main', credentialsId:'GITHUB_TOKEN_CRED_ID', url: 'https://${GH_HOSTNAME}/${GH_ORG_NAME}/${GH_REPO_NAME}.git'
}
stage('launch script') {
dockerExecute(script: this, dockerImage: 'Docker_image_repository'){
withCredentials([usernamePassword(credentialsId: GITHUB_TOKEN_CRED_ID, usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_AUTH_TOKEN')]) {
sh '''
script -d "path to cloned repo"
'''
}
}
}
}

It seems there is a property called dockerVolumeBind, so you should be able to mount the workspace to your container with this.

Related

Where Jenkins clone GIT repository?

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

How to use a Jenkinsfile for these build steps?

I'm learning how to use Jenkins and working on configuring a Jenkins file instead of the build using the Jenkins UI.
The source code management step for building from Bitbucket:
The build step for building a Docker container:
The build is of type multi configuration project:
Reading the Jenkins file documentation at https://www.jenkins.io/doc/book/pipeline/jenkinsfile/index.html and creating a new build using Pipeline :
I'm unsure how to configure the steps I've configured via the UI: Source Code Management & Build. How to convert the config for Docker and Bitbucket that can be used with a Jenkinsfile ?
The SCM will not be changed, regardless if you are using UI configuration or a pipeline, although in theory you can do the git clone from the steps in the pipeline, if you really insist convert the SCM steps in pure pipeline steps.
The pipeline will can have multiple stages, and each of the stages can have different execution environment. You can use the Docker pipeline plug-in, or you can use plain sh to issue the docker commands on the build agent.
Here is small sample from one of my manual build pipelines:
pipeline {
agent none
stages {
stage('Init') {
agent { label 'docker-x86' }
steps {
checkout scm
sh 'docker stop demo-001c || true'
sh 'docker rm demo-001c || true'
}
}
stage('Build Back-end') {
agent { label 'docker-x86' }
steps {
sh 'docker build -t demo-001:latest ./docker'
}
}
stage('Test') {
agent {
docker {
label 'docker-x86'
}
}
steps {
sh 'docker run --name demo-001c demo-001:latest'
sh 'cd test && make test-back-end'
}
}
}
}
You need to create a Pipeline type of a project and specify the SCM configuration in the General tab. In the Pipeline tab, you will have option to select Pipeline script or Pipeline script from SCM. It's always better to start with the Pipeline script while you are building and modifying your workflow. Once it's stabilized, you can add it to the repository.

How to get the project root directory in a Jenkins pipeline?

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?

Jenkins build failing in CI machine

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.

How to make subsequent checkout scm stages use local repo in a Jenkins pipeline?

We use Jenkins ECS plugin to spawn Docker containers for "each" job we build. So our pipelines look like
node ('linux') {
stage('comp-0') {
checkout scm
}
parallel(
"comp-1": {
node('linux') {
checkout scm
...
}
}
"comp-2": {
node('linux') {
checkout scm
...
}
}
)
}
The above pipeline will spawn 3 containers, one for each node('linux') call.
We set up a 'linux' node in our Jenkins configuration page to tell Jenkins the Docker repo/image we want to spawn. Its setup has a notion of 'Container mount points' which I assume are mounts on the host that the container can access.
So in above pipeline, I want the "first" checkout scm to clone the our repo onto a host path mounted by our containers, say /tmp/git. I then want the succeeding 'checkout scm' lines to clone the repo in my host's /tmp/git path.
I'm looking at How to mount Jenkins workspace in docker container using Jenkins pipeline to see how to mount a local path onto my docker
Is this possible?
You can stash the code from your checkout scm step and then unstash it in subsequent steps. Here's an example from the Jenkins pipeline documentation.
// First we'll generate a text file in a subdirectory on one node and stash it.
stage "first step on first node"
// Run on a node with the "first-node" label.
node('first-node') {
// Make the output directory.
sh "mkdir -p output"
// Write a text file there.
writeFile file: "output/somefile", text: "Hey look, some text."
// Stash that directory and file.
// Note that the includes could be "output/", "output/*" as below, or even
// "output/**/*" - it all works out basically the same.
stash name: "first-stash", includes: "output/*"
}
// Next, we'll make a new directory on a second node, and unstash the original
// into that new directory, rather than into the root of the build.
stage "second step on second node"
// Run on a node with the "second-node" label.
node('second-node') {
// Run the unstash from within that directory!
dir("first-stash") {
unstash "first-stash"
}
// Look, no output directory under the root!
// pwd() outputs the current directory Pipeline is running in.
sh "ls -la ${pwd()}"
// And look, output directory is there under first-stash!
sh "ls -la ${pwd()}/first-stash"
}
Jenkins Documentation on Stash/Unstash

Resources