I'm new with docker and jenkins. I'm trying to upload a non maven project to nexus using the jenkins pipeline. Below is a snippet of my jenkinsfile script. I want to do a maven upload of the resulting docker build image. any help?
node {
def app
stage('Clone repository') {
checkout scm
}
stage('Build image') {
app = bat "docker build -t myapp ."
}
stage('Test image') {
bat 'echo "Tests successful"'
}
stage('Deploy image') {
"
}
}
I haven't used this plugin before so this is my best guess. I am thinking the below plugin might help you do what you are looking for.
https://github.com/spotify/dockerfile-maven
Configure your pom.xml to point to nexus repository
Usage - https://github.com/spotify/dockerfile-maven/blob/master/docs/usage.md
dockerfile:build ------> Builds a Docker image from a Dockerfile.
dockerfile:tag ---------> Tags a Docker image.
dockerfile:push -------->Pushes a Docker image to a repository.
Related
I want to configure Jenkins 2.375.2 to build gradle project. But when I configure the pipe using Blue Ocean plugin and I run the pipeline I get error:
+ ./gradlew build
/var/lib/jenkins/workspace/jenkins_master#tmp/durable-dcccf1cd/script.sh: 1: ./gradlew: not found
Jenkins file:
pipeline {
agent any
stages {
stage('Build Image') {
steps {
sh "echo 'building..'"
// configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5 ', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'
// execute Java -jar ... and build docker image
sh './gradlew build'
sh 'docker build -t springio/gs-spring-boot-docker .'
}
}
}
I tried to add Gradle config
But still I get the same error. Do you know how I can fix this issue?
I need to build a docker image using docker-compose.yaml file and then I need to push it on dockerhub.
I used following command to build Dockerfile and push it to dockerhub with jenkins docker plugin.
stage('Building image and Push') {
steps{
script {
customImage = docker.build("my-image:${env.BUILD_ID}")
customImage.push()
}
}
}
Is there a similar method to write this kind of command to build and push docker-compose file?
PS: I heard about about a plugin called Docker compose build step. Can I do same as above with this plugin?
This does not seem supported in a scripted pipeline.
Using purely a declarative pipeline on an agent based on the docker-compose image (so with docker-compose preinstalled), I suppose you can shell script those commands directly:
stage('Build Docker Image') {
steps{
sh 'docker-compose build'
echo 'Docker-compose-build Build Image Completed'
}
}
And then login to DockerHub and push, as described in "Simple Jenkins Declarative Pipeline to Push Docker Image To Docker Hub"
stage('Login to Docker Hub') {
steps{
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | sudo docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
echo 'Login Completed'
}
}
stage('Push Image to Docker Hub') {
steps{
sh 'sudo docker push <dockerhubusername>/<dockerhubreponame>:$BUILD_NUMBER'
echo 'Push Image Completed'
}
}
A manual approach, since an "agent docker-compose" is not directly available (you have docker agent docker/dockerfile/kubernetes, but no docker-compose option)
Question
I have to configure CI/CD for number of Git repositories with help of Jenkins (and DockerHub as CD target). I did that with help of Docker multi-stage build (see Considerations). I'm afraid to misunderstand/overcomplicate a simple idea.
Is Jenkins + Docker multi-stage build = best/good practice? Am I applying the idea in the correct way?
Considerations
From this presentation I assume using Docker inside Jenkins is a good idea. After reading an article Using Multi-Stage Builds to Simplify and Standardize Build Processes, Docker multi-stage builds looks to be the next step of using Jenkins + Docker.
Answers to similar question also say Docker multi-stage makes sense, but doesn't provide an example of realisation.
Implementation
Jenkins creates pipeline from SCM repository.
Git repository
Dockerfile
Jenkinsfile
project-folder
|-src
|-pom.xml
Dockerfile
FROM alpine as source
RUN apk --update --no-cache add git
COPY project-folder repo
FROM maven:3.6.3-jdk-8 as test
COPY --from=source repo repo
WORKDIR repo
RUN mvn clean test
FROM maven:3.6.3-jdk-8 as build
COPY --from=test repo repo
WORKDIR repo
RUN mvn clean package
FROM openjdk:8 as final
MAINTEINER xxx <xxx#gmail.com>
LABEL owner="xxx"
COPY --from=build repo/target/some-lib-1.8.jar /usr/local/some-lib.jar
ENTRYPOINT ["java", "-jar", "/usr/local/some-lib.jar"]
Jenkinsfile
I used docker build --target for more granularity on Jenkins UI.
#!/usr/bin/env groovy
def imageId = "use-name/image-name:1.$BUILD_NUMBER"
pipeline {
agent {
label 'docker' # separate agent (launched as JAR on host machine) to avoid running docker inside docker
}
stages {
stage('Test') {
steps {
script {
sh "docker build --no-cache --target test -t ${imageId} ."
}
}
}
stage('Build') {
steps {
script {
sh "docker build --target build -t ${imageId} ."
}
}
}
stage('Image') {
steps {
script {
sh "docker build --target final -t ${imageId} ."
}
}
}
stage('Deploy') {
steps {
script {
docker.withRegistry('' , 'dockerhub') {
dockerImage = docker.build("${imageId}")
dockerImage.push()
}
}
}
}
stage('Clean') {
steps{
sh "docker rmi ${imageId}"
}
}
}
}
following taleodor's answer I would suggest next jenkinsfile:
pipeline {
agent {
label 'docker' # separate agent (launched as JAR on host machine) to avoid running docker inside docker
}
environment {
imageId = 'use-name/image-name:1.$BUILD_NUMBER'
docker_registry = 'your_docker_registry'
docker_creds = credentials('your_docker_registry_creds')
}
stages {
stage('Docker build') {
steps {
sh "docker build --no-cache --force-rm -t ${imageId} ."
}
}
stage('Docker push') {
steps {
sh'''
docker login $docker_registry --username $docker_creds_USR --password $docker_creds_PSW
docker push $imageId
docker logout
'''
}
}
stage('Clean') {
steps{
sh "docker rmi ${imageId}"
}
}
}
}
Here is my Jenkins Pipeline:
pipeline {
agent {
docker {
image 'node:6-alpine'
args '-p 3000:3000'
}
}
environment {
CI = 'true'
}
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm build'
}
}
stage('Deliver') {
steps {
sh './jenkins/scripts/deliver.sh'
input message: 'Finished using the web site? (Click "Proceed" to continue)'
sh './jenkins/scripts/kill.sh'
}
}
stage('Deploy') {
steps {
sh './jenkins/scripts/deploy.sh'
}
}
} }
I use Docker and jenkinsci/blueocean image to run Jenkins. The first two stages are kind of standard to build a NodeJS app, the third one, however, is the part that I want to Jenkins copy new files to the server. Here is the deploy.sh files:
#!/usr/bin/env sh
set -x
scp -o StrictHostKeyChecking=no -r dist/* deviceappstore:/var/www/my_website/static/
There are two problems, first jenkinsci/blueocean does not have scp (not setup) and second, the ~/.ssh/config does not exist inside of the Jankins docker image then SCP will fail to authenticate. My solution was to build a custom image extends from jenkinsci/blueocean, setup SCP and copy config file and SSH key into it.
There are some plugins like Publish Over SSH but it seems it's not useful for Pipeline projects.
Is there any better solution? It the whole scenario right or I'm doing something wrong? I'm looking for most secure and standard solution for this problem.
OK, I think I found a good solution.
Thanks to SSH Agent plugin I can easily pass the credentials to the SCP command and copy the files to the server. Something like this:
...
stage('Deploy') {
steps {
sshagent(['my SSH']) {
echo 'this works...'
sh 'scp -o StrictHostKeyChecking=no -r dist/* my_server:/var/www/my_site/static/'
}
}
}
...
This is perfect because all the credentials are inside of Jenkins server and there's nothing about it in the repo.
And to be able to use this, there's just one solution. You need to use apk inside of the jenkinsci/blueocean (alpine) image and setup openssh:
apk add openssh
Or better solution create a new Dockerfile and build your own version.
I'm using Jenkins in docker. The /var/jenkins_home is mounted on /var/jenkins-data on my host. My Jenkins can execute docker commands (mount of sockets) and I've installed the git plugin and pipeline plugin.
Now I have a pipeline job named test and the following pipeline:
pipeline {
agent any
stages {
stage('Clone') {
steps {
git branch: 'master', url: 'https://github.com/lvthillo/maven-hello-world.git'
}
}
stage('Build in Docker') {
agent {
docker {
image 'maven:3.5.2'
args '-v /var/jenkins_home/workspace/test:/opt/maven -w /opt/maven'
}
}
steps {
sh 'pwd'
sh 'mvn -v'
sh 'mvn clean install'
}
}
}
}
What I want to achieve is cloning my public repo from github. This works. In the next step I want to start a docker container (maven) and print the current directory, the maven version and perform a clean install.
The output of the 3 commands is:
[test#2] Running shell script
+ pwd
/var/jenkins_home/workspace/test#2
[Pipeline] sh
[test#2] Running shell script
+ mvn -v
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z)
Maven home: /usr/share/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.86-boot2docker", arch: "amd64", family: "unix"
[Pipeline] sh
[test#2] Running shell script
+ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.179 s
[INFO] Finished at: 2018-01-12T12:12:00Z
[INFO] Final Memory: 5M/31M
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/jenkins_home/workspace/test#2). Please verify you invoked Maven from the correct directory. -> [Help 1]
It seems to work because maven is not installed on my host, so it's executed from inside the container, but it's suddenly creating a new workspace (#2) instead of using the existing one from where I cloned the repo. I don't want to clone the repo in my container immediately because I want multiple stages, all with different containers, but all executed on my git repo in my workspace.
What am I doing wrong or how can I fix this?
I was thinking it was maybe because of the agent step. my first step can run on any agent (any slave), the docker step will run in the docker container, but must of course run on that same slave als where the git clone was executed.
pipeline {
agent any
stages {
stage('Clone') {
steps {
git branch: 'master', url: 'https://github.com/lvthillo/maven-hello-world.git'
stash name:'scm', includes:'*'
}
}
stage('Build in Docker') {
steps {
unstash 'scm'
script{
docker.image('maven:3.5.2').inside{
sh 'pwd'
sh 'mvn -v'
sh 'mvn clean install'
}
}
}
}
}
}
You can use this pipeline even with a multi-node setup. Docker plugin mounts your workspace as a docker workspace too.Hence, it is not necessary to mount any volume unless they are outside the workspace.
Thanks, the Previous solution works for me. My version for node container and ${PWD} as param
stage('Build Solution') {
agent {
docker {
image 'node:6-alpine'
args '-v ${PWD}:/usr/src/app -w /usr/src/app'
reuseNode true
}
}
steps {
sh 'npm install'
}
}
My last explanation was helping myself to solve the problem:
This text helped me to solve it. I had to ensure that all the steps on my pipeline were using the same agent as the initial one where I performed my git clone:
Addit reuseNode true solved it:
stage('Build in Docker') {
agent {
docker {
image 'maven:3.5.2'
args '-v /var/jenkins_home/workspace/test:/opt/maven -w /opt/maven'
reuseNode true
}
}
The error message returned by maven indicates that was not able to find the pom.xml file in the directory where the execution was launched.
I had the same issue, and solved by cd to the directory containing my project pom.xml.