filesystem scm load stuck in jenkinfile-runner - jenkins

I need to run jenkinsfile via the jenkinsdile-runner.
My jenkinsfile contains load shared library via filesystem scm plugin.
pipeline {
agent any
stages {
stage('Load Library') {
steps {
library identifier: 'my#TestCase',
retriever: legacySCM(
filesystem(clearWorkspace: false, copyHidden: false, path: "/workspace/my"))
}
}
stage('Test') {
steps {
my_method_to_run a: "aaaa"
}
}
}
}
Via execution the load shared lib is stuck.
Loading library my#TestCase
FSSCM.checkout /workspace/my_shared_library to /tmp/jenkinsfileRunner.tmp/jfr5221246900865917223.run/workspace/job#libs/my
I can't find any errors.
What can be the problem?

You can see jira ticket
I found WA.
Before I call the library command I manually create the /tmp/jenkinsfileRunner.tmp/jfr408537468992298463.run/workspace/job#libs folder and copy files to there.
After it my jenkinsfile run successfully via docker.
stage('Load Library') {
steps {
sh "echo 'WA - when running via docker the load library stuck, do the preparation manually'"
sh "mkdir -p ${WORKSPACE}/../job#libs/"
sh "rm -rf /workspace/my_shared_library/.git"
sh "cp -r /workspace/my_shared_library ${WORKSPACE}/../job#libs"
library identifier: 'my_shared_library#main',
retriever: legacySCM(
filesystem(clearWorkspace: false, copyHidden: false, path: '/workspace/my_shared_library'))
}
}

Related

Jenkins Pipeline: Run the step when you see a new file or when it changes

I have a laravel application that requires the "yarn" command at initialization, and later only if certain files are changed.
Using the code below, I manage to detect when that file has changed, but I need a suggestion to be able to run it at the first initialization (practically, that file together with all the others seem to be new files from the perspective of the Jenkinsfile).
Thanks!
Current try:
stage("Install NodeJS dependencies") {
when {
changeset "package.json"
}
agent {
docker {
image 'node:14-alpine'
reuseNode true
}
}
steps {
sh 'yarn --silent'
sh 'yarn dev --silent'
}
}

Jenkins pipeline how to change to another folder and run npm tests

Currently, I am using Jenkins pipeline script.
For running my tests, I need to access my code which is sitting on the desktop.
I tried this:
pipeline {
agent any
tools {nodejs "node"}
stages {
stage('Tests') {
steps {
sh 'cd users/tests/'
sh 'npm run shopfloor.shopfloor'
}
}
}
}
How I can change to my test folder and then run "npm run test"
I tried the answer below however i am getting this error now:
Running in users/tests/
[Pipeline] {
[Pipeline] sh
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Operation not permitted
+ npm run shopfloor.shopfloor
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Operation not permitted
Error: EPERM: operation not permitted, uv_cwd
at process.wrappedCwd (internal/bootstrap/switches/does_own_process_state.js:129:28)
at process.cwd (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/node_modules/graceful-fs/polyfills.js:10:19)
at Conf.loadPrefix (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/load-prefix.js:46:24)
at load_ (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:109:8)
at Conf.<anonymous> (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:96:5)
at Conf.emit (events.js:315:20)
at ConfigChain._resolve (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/node_modules/config-chain/index.js:281:34)
at ConfigChain.add (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/node_modules/config-chain/index.js:259:10)
at Conf.add (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:338:27)
at Conf.<anonymous> (/Users/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/node/lib/node_modules/npm/lib/config/core.js:314:25)
internal/bootstrap/switches/does_own_process_state.js:129
cachedCwd = rawMethods.cwd();
Use the dir step to switch the directory and execute the commands in that context:
pipeline {
agent any
tools {nodejs "node"}
stages {
stage('Tests') {
steps {
dir('users/tests/') { // <<------------
sh 'npm run shopfloor.shopfloor'
}
}
}
}
}
Please try once using double quotes.
dir("folder")
in groovy Single quotes are a standard Java String while Double quotes are a templatable String.

jenkins pipeline script to deal module in subdirectory

I have a git url maven project which I want to only deal one of its submodule.
I write in pipeline script :
...
stage("mvn build") {
steps {
script {
sh "mvn package -DskipTests=true"
}
}
}
error arise: The goal you specified requires a project to execute but there is no POM in this directory (/xx/jenkins/workspace/biz-commons_deploy). so I add command :
sh "cd cmiot-services/comm" # subdir of biz-commons_deploy
def PWD = pwd();
echo "##=${PWD} "
sh "mvn package -DskipTests=true"
not work, print ##=/root/.jenkins/workspace/biz-commons_deploy, the error is the same as before .
how can I solve this problem and why the echo and error use different user space?
I make it using sh "mvn -f cmiot-services/comm/pom.xml package -DskipTests=true",still not know where this two user path come from and why sh cd not work.
steps {
sh '''
# list items in current directory to see where is your pom.xml
ls -l
# run job by comment out following two lines, if you don't know the
# relative path of folder where pom.xml insides exactly
cd <folder where pom.xml insides>
mvn package -DskipTests=true
'''
}
As Yong answered, every sh steps are independent, imagine Jenkins is opening a new ssh connection on your slave each time.
For your script, instead of a workaround with sh, why not using build in dir step ?
Something like this should do it :
stage("mvn build") {
steps {
script {
dir('cmiot-services/comm') {
sh "mvn package -DskipTests=true"
}
}
}
}
when you are executing Jenkins Pipline, the current directory is the Jenkins workspace directory.
You can add a step to clone the repo that your code is in (granted that the environment you are running the Jenkins instance is able to connect to your repo and clone).
You can then navigate into the directory that has the pom.xml. And finally execute the maven command.
...
stage("Clone Repo") {
steps {
script {
sh "git clone ssh://git#bitbucket.org:repo/app.git"
}
}
}
stage("mvn build") {
steps {
script {
sh "cd app/"
sh "pwd"
sh "mvn package -DskipTests=true"
}
}
}

How to configure a Jenkinsfile to build docker image and push it to a private registry

I have two questions. But maybe I don't know how to add tags for this question so that I added the tag for both. First question is related to Jenkins plugin usage to bake and push docker image using this. Below is my Jenkinsfile script. I finished building jar file in target directory. Then I want to run this docker plugin to bake with this artifact. As you know, we needed to have a Dockerfile so I put it in a root directory where git cloned the source. How I configure this? I don't know how to this. If I run below, Jenkins told that there is no steps.
pipeline {
agent any
stages {
stage('build') {
steps {
git branch: 'master', credentialsId: 'e.joe-gitlab', url: 'http://70.121.224.108/gitlab/cicd/spring-petclinic.git'
sh 'mvn clean package'
}
}
stage('verify') {
steps {
sh 'ls -alF target'
}
}
stage('build-docker-image') {
steps {
docker.withRegistry('https://sds.redii.net/', 'redii-e.joe') {
def app = docker.build("sds.redii.net/e-joe/spring-pet-clinic-demo:v1",'.')
app.push()
}
}
}
}
}
UPDATE
this is another Jenkins Pipeline Syntax sniffet generator. But it doesn't work neither.
pipeline {
agent any
stages {
stage('build') {
steps {
git branch: 'master', credentialsId: 'e.joe-gitlab', url: 'http://70.121.224.108/gitlab/cicd/spring-petclinic.git'
sh 'mvn clean package'
}
}
stage('verify') {
steps {
sh 'ls -alF target'
}
}
stage('docker') {
withDockerRegistry([credentialsId: 'redii-e.joe', url: 'https://sds.redii.net']) {
def app = docker.build("sds.redii.net/e-joe/spring-pet-clinic-demo:v1",'.')
app.push()
}
}
}
}
Dokerfile is like the below. If I try baking image in my local, I got the following error.
container_linux.go:247: starting container process caused "chdir to cwd (\"/usr/myapp\") set in config.json failed: not a directory"
oci runtime error: container_linux.go:247: starting container process caused "chdir to cwd (\"/usr/myapp\") set in config.json failed: not a directory"
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
DockerFile
FROM openjdk:7
COPY ./target/spring-petclinic-1.5.1.jar /usr/myapp
WORKDIR /usr/myapp
RUN java spring-petclinic-1.5.1.jar
You are writing your .jar to /usr/myapp. Which means that /usr/myapp will be the jar file and not a directory, resulting in that error. Change your docker copy line to COPY ./target/spring-petclinic-1.5.1.jar /usr/myapp/ (with the trailing slash) and your Dockerfile should work.

Pipeline step having trouble resolving a file path

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'
'''
}
}
}
}
}

Resources