Jenkins docker build No such directory error - docker

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

Related

Jenkins | DSL| Workspace DIR issue

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

Getting error Jenkin pipeline docker: command not found

Dockerfile:
pipeline {
agent any
stages {
stage ('Compile') {
steps {
withMaven(maven: 'maven_3_6_3') {
sh 'mvn clean compile'
}
}
}
stage ('unit test and Package') {
steps {
withMaven(maven: 'maven_3_6_3') {
sh 'mvn package'
}
}
}
stage ('Docker build') {
steps {
sh 'docker build -t dockerId/cakemanager .'
}
}
}
}
docker build -t dockerId/cakemanager .
/Users/Shared/Jenkins/Home/workspace/CDCI-Cake-Manager_master#tmp/durable-e630df16/script.sh:
line 1: docker: command not found
First install docker plugin from Manage Jenkins >> Manage Plugins >> Click on available and search for Docker and install it.
and then configure it on Manage Jenkins >> Global tool configuration.
You need to manually install docker on your Jenkins master or on agents if you're running builds on them.
Here's the doc to install docker on OS X https://docs.docker.com/docker-for-mac/install/

How to build stage based on code change with Jenkins?

Let's assume that we have git repo with some code in it.
Now we want to build few docker containers with jenkisfile like this:
stage('Building image 1') {
steps{
script {
'sh docker build -f 1'
}
}
}
stage('Building image 2') {
steps{
script {
'sh docker build -f 2'
}
}
}
How can I trigger a build only on one stage based on change in some files.
With another words: Let's say that I have 2 files and I want to build stage 1 only IF file 1 have changes, or I want to build stage 2 only IF if file 2 is changed?
Resource: jenkins.io/doc/book/pipeline/syntax look for changeset
Example would be something like this:
stage('Building image 1') {
when {
changeset "some_folder/some_file1"
}
steps{
script {
'sh docker build -f 1'
}
}
}
stage('Building image 2') {
when {
changeset "some_folder/some_file2"
}
steps{
script {
'sh docker build -f 2'
}
}
}

Jenkins not building docker image, can't find the docker command

I'm trying to make Jenkins build and publish a docker image of my project after every successful build of my Maven Java project.
I have a Dockerfile and a Jenkinsfile at the root of the project.
Here's my Jenkinsfile?
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
options {
skipStagesAfterUnstable()
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
stage('Build image') {
steps {
script {
docker.withRegistry('http://localhost:5000') {
def customImage = docker.build("my-image:${env.BUILD_ID}")
customImage.push()
}
}
}
}
}
post {
always {
archiveArtifacts artifacts: 'target/**/*.jar', fingerprint: true
}
}
}
When Jenkins tr to build this project, it returns the given error message:
+ docker build -t my-image:13 .
/var/jenkins_home/workspace/powertiss-eureka_master#tmp/durable-b9e45059/script.sh: line 1: docker: not found
What am I doing wrong here?
*edit: My Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY target/projectName-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
The maven:3-alpine image most likely does not include Docker. You need to choose an image that has Docker included.

How to push the docker image to DTR using Jenkins multibranch pipeline

My problem statement is to push the docker image to DTR using Jenkins multibranch pipeline.
I want to configure my Jenkins file in such a way that it will build the image and then Push it.
for building the image I will use 10.1.2.3 machine and DTR will be https://something.dtr01.eastus2.cloudapp.azure.com/
I am totally new to Jenkins as per the instruction I have put the sample Jenkins file in the git repo.
Please suggest me the configuration changes in Jenkisfile
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
checkout scm
}
stage ('Build') {
"echo 'shell scripts to build project...'"
}
stage ('Tests') {
parallel 'static': {
sh "echo 'shell scripts to run static tests...'"
},
'unit': {
sh "echo 'shell scripts to run unit tests...'"
},
'integration': {
sh "echo 'shell scripts to run integration tests...'"
}
}
stage ('Deploy') {
sh "echo 'shell scripts to deploy to server...'"
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
Any help will be appreciable

Resources