Jenkins | DSL| Workspace DIR issue - jenkins

I have a Jenkin DSL JOB. It's for java build. I am stuck in a strange problem.
jobname is DSL, I saw a workspace with the name of DSL is created, But when the job runs it added another workspace with the name of DSL#2. The problem I can not get final jar file from DSL workspace
pipeline
{
agent any
stages
{
stage('Build')
{
agent {
docker { image 'maven:latest'
args '-v /home/ubuntu/jenkins/jenkins_home/.m2:/root/.m2'
}
}
steps {
git branch: "${params.branch}", url: "git#github.org/repo.git"
sh 'mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true'
sh "ls -la target/name.jar "
}
}
stage('Copy Artifects')
{
steps {
//print "$params.IP"
// sh '${params.IP}"
sh "ls -la && pwd "
sh "scp target/name.jar ubuntu#${params.IP}:/home/ubuntu/target/name.jar_2"
}
}
}
}
OUT Of the JOB
Compiling 19 source files to /var/jenkins_home/workspace/dsl#2/auth-client/target/classes

DSL#2 means you either have a concurrent job configured and two builds runnning at the same time, OR you got a bug https://issues.jenkins-ci.org/browse/JENKINS-30231
To address your issue:
you are building stage('Build') inside a docker container created from maven image.
However, stage('Copy Artifects') is run OUTSIDE of that container
To fix it, you need to move agent{} to pipeline{} level like this:
pipeline
{
agent {
docker {
image 'maven:latest'
args '-v /home/ubuntu/jenkins/jenkins_home/.m2:/root/.m2'
}
}
stages
{
stage('Build')
{
steps {
git branch: "${params.branch}", url: "git#github.org/repo.git"
sh 'mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true'
sh "ls -la target/name.jar "
}
}
stage('Copy Artifects')
{
steps {
sh "ls -la && pwd "
sh "scp target/name.jar ubuntu#${params.IP}:/home/ubuntu/target/name.jar_2"
}
}
}
}

Related

Jenkins docker build No such directory error

i am trying to build a docker image with jenkins bbut i keep getting ADD failed: no source files were specified error but it builds when using the command line.
my pipeline script is below:
pipeline {
agent any
environment {
dockerImage=''
}
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "maven3"
}
stages {
stage('Clone') {
steps {
// Get some code from a GitHub repository
git branch: 'main', credentialsId: '9e078e67-9fad-43ca-b0c5-feadb03fc060', poll: false, url: 'https://github.com/phelumie/Birthday-Scheduling-App.git'
}
}
stage('Build jar') {
steps {
sh "mvn -f scheduler/pom.xml package -DskipTests -X"
echo 'Build jar Completed'
}
}
stage('Build image') {
steps {
echo 'Starting to build docker image'
script {
def dockerImage=docker.build("phelumiess/demo","-f scheduler/Dockerfile .")
echo 'Build Image Completed'
}
}
}
}
}
Below is my Dockerfile
FROM openjdk:8-jdk-alpine
EXPOSE 8080
ADD target/birthday-scheduler*.jar birthday-scheduler.jar
ENTRYPOINT ["java","-jar","/birthday-scheduler.jar" ]
Change the directory before executing the docker build command. Refer the following.
stage('Build image') {
steps {
echo 'Starting to build docker image'
dir('Birthday-Scheduling-App/scheduler') {
script {
def dockerImage=docker.build("phelumiess/demo")
echo 'Build Image Completed'
}
}
}
}

How to run the build inside docker container in Jenkins?

In my application I have a build script in package.json.
The build makes dist folder and inside I have my application.
I set Jenkins master and Jenkins agent as say in boxboat setup jenkins with docker and watch the video in youtube.
But now after I did this, I don't think my bash commands running inside a container.
I want to clone the repo and run npm i and npm run build - inside the docker container.
How I modify this configuration to able to do that?
throttle(['throttleDocker']) {
node('docker') {
wrap([$class: 'AnsiColorBuildWrapper']) {
try{
stage('Build') {
checkout scm
sh '''
echo "in Setup"
docker ps -a
echo "after docker"
# ./ci/docker-down.sh
# ./ci/docker-up.sh
'''
}
stage('Test'){
parallel (
"unit": {
sh '''
echo "in unit"
# ./ci/test/unit.sh
'''
},
"functional": {
sh '''
echo "in functional"
# ./ci/test/functional.sh
'''
}
)
}
stage('Capacity Test') {
sh '''
echo "in Capacity Test"
# ./ci/test/stress.sh
'''
}
}
finally {
stage('Cleanup') {
sh '''
echo "in Cleanup"
# ./ci/docker-down.sh
'''
}
}
}
}
}
I tried to this codes but they don't work. I also add agent after try.
stage('Build') {
agent {
docker {
label 'docker'
image 'node:latest'
}
}
steps {
checkout scm
sh 'node -v'
}
...
You can try below scripted pipeline
node {
docker.image('yourimage').inside {
stage('Build'){
sh 'echo "Build stage inside container"'
}
stage('Test'){
sh 'echo "Test Stage inside container"'
}
}
}

Multiple agents in Jenkins pipeline but use one agent for certain stages

I have this Jenkinsfile:
#!groovy
pipeline
{
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
agent {
label 'docker && new'
}
stages
{
stage('Docker build')
{
when {
branch 'dev'
}
steps
{
sh "echo ${env.BUILD_NUMBER}"
sh "./scripts/push.sh Docker http://xxxxx.xxxx ${env.BUILD_NUMBER} ${env.GIT_BRANCH}"
sh "echo ${env.BUILD_NUMBER}"
sh "echo ${env.GIT_BRANCH}"
}
}
stage("Initialise")
{
agent {
dockerfile {
filename 'Dockerfile'
label 'docker && new'
args '--entrypoint ""'
}
}
steps {
sh "terraform init -input=false"
}
}
stage("WorkspaceDev") {
agent {
dockerfile {
filename 'Dockerfile'
label 'docker && new'
args '--entrypoint ""'
}
}
when {
branch 'dev'
}
steps {
sh "terraform workspace select dev || terraform workspace new dev"
}
}
}
}
It builds a container from my Dockerfile, However when running this job it is creating a new docker container to run the next stage called WorkspaceDev. I need to use a separate agent for the very first stage and then dockerfile agent for all other stages
How can I use the same container built for the Initialise stage?
Problem:
When running this pipeline the "Docker build" stage is executing on the agent itself as expected.
It then gets to the "initialisation" stage. This build a new docker container (docker build (my Dockerfile I have specified in the agent section for this stage). This stage completes inside this container.
Next it gets to the "WorkspaceDev" stage - this then AGAIN rebuilds the container with docker build.
I want to use the same container built in the "Initialisation" stage
Can't you use agent { label 'NODE_LABEL' }?
Your question is not very clear, but it seem you are defining a global agent in the upper part of your Jenkinsfile. If the stage you are trying to run on a different agent is the "Docker build" stage, you just have to specify an agent like in your other stages.
There is the agent documentation:
https://jenkins.io/doc/book/pipeline/syntax/#agent
I think that the label has no meaning (so it is ignored) when you're using a Dockerfile-agent, so it creates a new container for each stage.
therefore, try the following:
#!groovy
pipeline
{
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
agent {
dockerfile {
filename 'Dockerfile'
args '--entrypoint ""'
}
}
stages
{
stage('Docker build')
{
agent {
dockerfile {
filename 'Dockerfile'
args '--entrypoint ""'
}
}
when {
branch 'dev'
}
steps
{
sh "echo ${env.BUILD_NUMBER}"
sh "./scripts/push.sh Docker http://xxxxx.xxxx ${env.BUILD_NUMBER} ${env.GIT_BRANCH}"
sh "echo ${env.BUILD_NUMBER}"
sh "echo ${env.GIT_BRANCH}"
}
}
stage("Initialise")
{
steps {
sh "terraform init -input=false"
}
}
stage("WorkspaceDev") {
when {
branch 'dev'
}
steps {
sh "terraform workspace select dev || terraform workspace new dev"
}
}
}
}

How can I use agent docker in a declartive pipeline running on jenkins ssh-slave node?

I am running jenkins master and slave as docker container. I have setup a slave node using jenkins/ssh-slave image with label 'worker'. I can successfully run my pipeline on the worker node. However, when I am trying to run docker build command using the Jenkinsfile, I am getting error docker: not found.
pipeline {
agent { label 'worker' }
tools {nodejs "node"}
stages {
stage ('Build APP') {
steps {
echo 'BUILDING APPLICATION'
sh 'npm install'
}
}
stage ('Create Package') {
steps {
script{
echo 'BUILDING DOCKER IMAGE'
docker.build("package${env.BUILD_NUMBER}")
}
}
}
stage('Package Test') {
agent { docker }
steps {
echo 'RUNNING IMAGE IN CONATAINER'
sh "docker run -p 5050:4000 -d package${env.BUILD_NUMBER}"
echo 'CHECKING HEALTH STATUS'
script {
try {
sh "curl -s --head --request GET http://127.0.0.1:5050/ | grep '200'"
echo 'Health Check Passed!'
} catch(Exception e) {
echo "Health Check Failed!"
}
}
}
}
In the third step 'package test' I have placed agent docker in the file but it doesn't seem to work. How can I place agent docker in a declarative pipeline?

How to change the Agent label in Jenkins depending on Branch Name

I am creating a Jenkin Pipeline for below task.
Pull the latest code from vsts
Build the code and create .jar file out of it
creating a Docker image on the basis of the jar
tag the image
push the image into Docker registry
for this, I have written below Jenkinsfile
pipeline {
agent {
label "master"
}
stages {
stage('Build') {
steps {
echo '..........................Building Jar..........................'
sh 'npm install'
}
}
stage('Build-Image') {
steps {
echo '..........................Building Image..........................'
sh 'sudo docker build -t some-org/admin-portal:v0.1 --build-arg PORT=9007 --build-arg ENVIRONMENT=develop .'
}
}
stage('Tag-Image') {
steps {
echo '..........................Taging Image..........................'
sh 'sudo docker login some-repo -u username001 -p password'
sh 'sudo docker tag some-org/admin-portal:v0.1 some.dtr.io/some-org/admin-portal:v0.1'
}
}
stage('Push-Image') {
steps {
echo '..........................Pushing Image..........................'
sh 'sudo docker push some.dtr.io/some-org/admin-portal:v0.1'
}
}
}
}
Below is Jenkins job configuration snapshot for Pipeline
My Question is how can I change the agent label depending upon branch name or some conditions.
e.g if the branch is develop I want to use slave1 node and if the branch is production I want to use master
Any Help will be appreciable.
Thanks in Advance.
You can assign the agent labels inside the stage, so that you can execute the stages with required agents.
eg:
pipeline {
agent none
stages {
stage('Build') {
agent {
label "master"
}
steps {
echo '..........................Building Jar..........................'
sh 'npm install'
}
}
stage('Build-Image') {
agent {
label "master"
}
steps {
echo '..........................Building Image..........................'
sh 'sudo docker build -t some-org/admin-portal:v0.1 --build-arg PORT=9007 --build-arg ENVIRONMENT=develop .'
}
}
stage('Tag-Image') {
agent {
label "slave1"
}
steps {
echo '..........................Taging Image..........................'
sh 'sudo docker login some-repo -u username001 -p password'
sh 'sudo docker tag some-org/admin-portal:v0.1 some.dtr.io/some-org/admin-portal:v0.1'
}
}
stage('Push-Image') {
agent {
label "slave1"
}
steps {
echo '..........................Pushing Image..........................'
sh 'sudo docker push some.dtr.io/some-org/admin-portal:v0.1'
}
}
}
}

Resources