How do I retrieve the remote FS root from declarative Jenkinsfile? - jenkins

I have a heavyweight base project that rarely changes over time and has a lot of header files. I've set it up to be built in a custom workspace with a relative path base. So on a remote node with FS root set to C:\Jenkins, the resulting path on that particular node will be C:\Jenkins\base.
The reason for this setup is that I don't want to copy or unpack the whole base project for every dependent project to save build time. Also, I don't want to use absolute paths because I like the idea of a self-contained jenkins installation.
Now I have a second project project that uses base. I need to specify the path of base to the build system of project so that it will find the base headers it needs.
Is there any way to retrieve the remote FS root through the environment? I've tried using ${env.JENKINS_HOME} but this always resolves to the home folder of the Jenkins master. My build system expects to find the path to the base project in the PATH_TO_BASE environment variable:
pipeline {
agent none
stages {
stage('Build') {
parallel {
stage('Build Linux x64') {
agent {
label "Debian9_x64"
}
steps {
withEnv(["PATH_TO_BASE=${env.JENKINS_HOME}/base"]) {
sh '''mkdir -p _build
cd _build
cmake ..
cmake --build .'''
}
}
}
stage('Build Windows10') {
agent {
label "Windows10"
}
steps {
withEnv(["PATH_TO_BASE=${env.JENKINS_HOME}/base"]) {
bat '''if not exist _build mkdir _build
cd _build
cmake ..
cmake --build .'''
}
}
}
}
}
}
}

I usually configure slave with a set of environment variables.
Go to the slave configuration
Set Environment varaible under "node properties" section.

Related

jenkins workspace folder cut in multibranch pipeline

I note that in multibranch pipeline the workspace folder is cut.
For example a project named:
Sample09-Netbeans-MultiBranch-Pipeline-Maven-Svn
that comes from a subversion repository like
https://my-favourite-repo/svn/ProjectsJava/DevOps/Jenkins/Test/test-jenkins-java-maven-multibranch/
with a project folder like
D:\ProjectsJava\DevOps\Jenkins\Test\test-jenkins-java-maven-multibranch\trunk\myproject
produce a workspace folder like this
D:\Jenkins.jenkins\workspace\peline-Maven-Svn_trunk_myproject
other types of project whit similar names doesn't have this problem
I found a workaround adding
-a node definition
-a customWorkspace
but when i use it maven doesn't see
the settings.xml file and i must directly specify it in the maven command passing
a jenkins-global property.
No other action can provide it to the command (define a jenkins-config-file,
define it in jenkins-maven configuration or in project-maven configuration)
pipeline {
agent{
node{
label 'my-node'
customWorkspace "${JENKINS_HOME}/Workspace/${JOB_NAME}/${BUILD_NUMBER}"
}
}
stages {
stage('Build-And-Test') {
steps {
withMaven {
bat "mvn clean package test -B -s ${MAVEN_SETTINGS}"
}
}
}
}
}
Why Jenkins cut the folder name only in multibranch pipelines?
There is another way to define the workspace-job-folder-name outside of the jenkinsfile ?
OR
There is a way to let maven see the settings.xml configured in one of the Jenkins configuration?
Most operating systems have an upper bound on the length of a file name and the length of a directory path. Jenkins pipeline jobs that use full length strings were encountering operating system path limitations (especially the 256 character default limit on Windows).
Pipeline job names were intentionally changed to shorter forms so that they would reduce the likelihood of encountering an operating system or file system limit on path length.

Creating a Python Pipeline on Jenkins but get access denied from docker

I've created a Jenkinsfile in my Git repository that is defined as this:
pipeline {
//None parameter in the agent section means that no global agent will be allocated for the entire Pipeline’s
//execution and that each stage directive must specify its own agent section.
agent none
stages {
stage('Build') {
agent {
docker {
//This image parameter (of the agent section’s docker parameter) downloads the python:3.8
//Docker image and runs this image as a separate container. The Python container becomes
//the agent that Jenkins uses to run the Build stage of the Pipeline project.
image 'python:3.8.3'
}
}
steps {
//This sh step runs the Python command to compile the application
sh 'pip install -r requirements.txt'
}
}
}
}
When I tried to run the job with this Pipeline, I've got the following error:
I also tried to use image python:latest but this option didn't work either.
Can someone explain me :)?
Go to Computer Management -> Local Users and Groups and make sure the user used by jenkins is added to the docker-users group

SCP from Windows to Linux using Jenkins pipeline script

I want to do SCP from Windows Jenkins node to Linux server. In this set up, Windows machine is a Jenkins slave and the target server where i want to copy is Linux.
Below is my Jenkins pipeline script. Before the below script runs, i am cloning the repository and then building the project which finally creates a .jar file. I want to copy this file to Linux server.
stage('SCP JAR file') {
steps {
bat 'scp /c/Jenkins/workspace/migration/test-project/build/libs/ssupservice-0.0.1-SNAPSHOT.jar rxp096p#server:/home/rxp096p/testing'
}
}
}
My working directory is /c/Jenkins/workspace/migration/test-project/. Inside the given directory, build/libs folder gets created where the required .jar file is present.
Running above script gives the following error:
/c/Jenkins/workspace/migration/test-project/build/libs/ssupservice-0.0.1-SNAPSHOT.jar: No such file or directory
Give this a shot:
pipeline {
agent any
stages {
stage('SCP JAR file') {
steps {
bat '"c:\\Program Files\\git\\usr\\bin\\scp.exe" -i "c:\\Users\\tom\\.ssh\\azure\\id_rsa" C:\\Users\\tom\\.jenkins\\workspace\\scp-to-linux\\abc.jar tom#xy.xyz.xy.xz:abc.jar'
bat '"c:\\Program Files\\git\\usr\\bin\\ssh.exe" -i "c:\\Users\\tom\\.ssh\\azure\\id_rsa" tom#xy.xyz.xy.xz ls -ltr'
}
}
}
}
Note: While doing scp, if you do not specify the destination file name, it will create file on remote server with the complete source path name. For example, in my case, it would have created file with the name C:\Users\tom\.jenkins\workspace\scp-to-linux\abc.jar on remote server had i not specified this syntax: tom#xy.xyz.xy.xz:abc.jar

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?

Go not found in Docker-based Jenkins agent

I have created a Docker-based Jenkins agent that uses docker:stable-dind (which is based on Alpine 3.10) as its base. Full Dockerfile.
In the Dockerfile I install Go: RUN apk add go
When running locally, e.g. go version, go env GOROOT, etc... I get results, e.g. 1.12.6, /usr/lib/go.
I then attempt using this agent in Jenkins, and print env to verify the above environment variables are, but they not there and also go version fails`.
So, I update the Docker agent template in Jenkins with:
GOROOT: /usr/lib/go
PATH:/bin/sonar-scanner/bin/:/usr/local/bin:$GOROOT/bin:$PATH
Now when checking env they are there...
GOROOT=/usr/lib/go
PATH=/bin/sonar-scanner/bin/:/usr/local/bin:/usr/lib/go/bin:/bin:/usr/bin:/sbin:/usr/sbin
GOPATH=/home/jenkins/workspace/go test
... but go version still fails.
GOPATH is set to the current $WORKSPACE as that's where I will clone the Go project source if this actually works.
This is the Jenkins job:
#!groovy
pipeline {
agent {
label 'cli-agent'
}
stages {
stage ("test") {
steps {
script {
withEnv(["GOPATH=${WORKSPACE}"]) {
sh """
env
go version
"""
}
}
}
}
}
}

Resources