Jenkins Pipeline : docker not found with Maven docker image - maven

I have a Jenkins pipeline which is running fine but it depends upon JDK and maven installed tools. There were few instances in the past that these JDK and maven tool's name was changed(e.g. Maven 3.6.2 -> Maven 3.6.3 and it results in my pipeline failure.
stage ("build") {
withMaven(jdk: 'Java SE 8u221', maven: 'Maven 3.6.3', tempBinDir: '') {
sh 'mvn clean package jib:dockerBuild verify'
}
}
I want my pipeline to be independent of what tools are installed. So I rewrite my Jenkins pipeline like below to provide docker image of maven(since JDK is bundled with it)
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Checkout') {
steps {
git branch: "master", url: "repo url", credentialsId: 'id'
}
}
stage ("build") {
steps {
sh 'mvn clean package jib:dockerBuild verify'
}
}
}
}
But now I am getting an error Failed to execute goal com.google.cloud.tools:jib-maven-plugin:2.3.0:dockerBuild (default-cli) : Build to Docker daemon failed, perhaps you should make sure Docker is installed and you have correct privileges to run it
It seems that docker daemon is not visible after I provided a maven docker image.

I did solve this by adding docker agent inside of my maven docker image
pipeline {
agent any
stages {
stage('build Dockerfile') {
steps {
sh '''echo "FROM maven:3-alpine
RUN apk add --update docker openrc
RUN rc-update add docker boot" >/var/lib/jenkins/workspace/Dockerfile'''
}
}
stage('run Dockerfile') {
agent{
dockerfile {
filename '/var/lib/jenkins/workspace/Dockerfile'
args '--user root -v $HOME/.m2:/root/.m2 -v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps {
sh 'docker version'
sh 'mvn -version'
sh 'java -version'
}
}
}
}

Related

Pipeline created by Job-DSL fails to run maven

I am trying to setup Jenkins. I am using the docker image BlueOcean. I am trying to create a Jenkins pipeline using a Job-DSL. When I create the pipeline myself, and run it, it works. But when I run the pipeline created by the Job-DSL, it fails because of maven.
I looked on internet, but I couldn't find a solution proper to my case.
He is the Jenkinsfile
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
}
and this is the job-DSL
job('PROJ-unit-tests') {
scm {
git('git://github.com/Jouda-Hidri/Transistics.git') { node ->
node / gitConfigName('DSL User')
node / gitConfigEmail('hxxxa#gmail.com')
}
}
triggers {
scm('*/15 * * * *')
}
steps {
maven('-e clean test')
}
}

Require Jenkins Pipeline script for Maven build and Maven Test from Pom.xml?

I am new to Jenkins and I have created the Maven+TestNG project in eclipse I which I have executed the sample test case to print hello world from POM.xml,which is running fine. I have pushed the project in GIT as well Now I need to execute this from Jenkins using Pipeline script for CI/CD.
I have created below Pipleline script in Jenkins pipeline tab but I am getting error which executing the script. Can you please guide me or provide me the tutorial link where I can get Jenkins pipeline example.
The script is as follow:
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jitsolution19/RestAssured_Framework.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'Maven'
}
stage('Build') {
// Run the maven build
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
stage('Test') {
//Perform test
echo 'Execute the script'
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore test"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore test/)
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archive 'target/*.jar'
}
}

jenkins declarative pipeline not working for xl-deploy using maven command

i wanna create jenkins declarative pipeline for deploying on xl-deploy using maven command. i am not using xl-deploy plugin i am just using maven command for this.
pipeline {
agent {
label 'java8'
}
tools {
maven 'M3'
}
options {
skipDefaultCheckout()
timestamps()
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: "${env.BRANCH_NAME}"=='master'?'10':''))
}
environment {
ARTIFACTORY = credentials('artifactory-credentials')
CF = credentials('cf-credentials')
SONAR = credentials('Sonar_Credentials')
}
stages {
stage ('Checkout') {
steps {
checkout scm
sh "git rev-parse HEAD > .git/commit-id"
script {
commit_id = readFile('.git/commit-id').trim()
pom = readMavenPom file: 'pom.xml'
currentBuild.displayName = commit_id.take(7) + "-" + pom.version
}
}
}
stage ('Build') {
steps {
sh "mvn -U -s settings.xml -gs settings.xml clean install -DskipTests=true"
}
}
stage('Publish Artifacts') {
when {
branch 'master'
}
steps {
sh "echo 'Publish JAR to Artifactory !'"
sh "mvn -s settings.xml -gs settings.xml versions:set -DnewVersion=$commit_id"
sh "mvn -s settings.xml -gs settings.xml deploy -DskipTests=true"
}
}
stage('Deploy') {
steps {
sh "wget --user ${ARTIFACTORY_USR} --password ${ARTIFACTORY_PSW} -O ${pom.artifactId}.war -nv <repo url>/${pom.artifactId}/${commit_id}/${pom.artifactId}-${commit_id}.war --server-response --"
sh "mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=<app package>-$commit_id:war -DoutputDirectory=target -Dmdep.useBaseVersion=true"
}}
}
post {
always {
deleteDir()
}
}
}
i am getting following exception:
Failed to execute goal com.xebialabs.xldeploy:xldeploy-maven-plugin:5.0.2:generate-deployment-package.
till publish, it is working fine. but it is giving exception while executing deploy stage
I would suggest upgrading to version 6.0.1 of the plugin as that version fixes some connectivity issues.
The problem might also be related to an incorrect pom.xml file, however for that to exclude as the root cause, you should at least share your pom.xml, your XL Deploy version and the loaded plugins in XL Deploy.

jhipster mvnw command not working in jenkins only

I'm trying to do Continuous Integration for the Jhipster project using Jenkins CI. In Jenkins I created as a pipeline project and tried to build with the following Jenkinsfile,
node {
stage('checkout') {
checkout scm
}
// uncomment these 2 lines and edit the name 'node-4.6.0' according to what you choose in configuration
// def nodeHome = tool name: 'node-4.6.0', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
// env.PATH = "${nodeHome}/bin:${env.PATH}"
stage('check tools') {
sh "node -v"
sh "npm -v"
sh "bower -v"
sh "gulp -v"
}
stage('npm install') {
sh "npm install"
}
stage('clean') {
sh "mvnw clean"
}
stage('backend tests') {
try {
sh "mvnw test"
} catch(err) {
throw err
} finally {
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
stage('frontend tests') {
try {
sh "gulp test"
} catch(err) {
throw err
} finally {
step([$class: 'JUnitResultArchiver', testResults: '**/target/test-results/karma/TESTS-*.xml'])
}
}
stage('packaging') {
sh "mvnw package -Pprod -DskipTests"
}
}
It works fine upto npm install, on clean stage it get failure with error mvnw: command not found. I have tried with mvn clean it works but mvnw command is not working.
Also I have tried with './mvnw clean', this tries to download something from maven and fails because of connection timeout.
Any help would be appreciated.

Gradle Docker plugin connection timed out

I am trying to create and build an image by using bmuschko/gradle-docker-plugin. The Dockerfile is created but I can't seem to be able to build an image from it. I am on Centos 7.
Create the Dockerfile:
task createBaseImage(type: Dockerfile) {
destFile = project.file('docker/base/Dockerfile')
from 'java:8'
runCommand 'apt-get update'
runCommand 'apt-get -qq -y install python3 python3-dev python3-pip'
}
Build the image:
task buildBaseImage(type: DockerBuildImage) {
dependsOn createBaseImage
inputDir = createBaseImage.destFile.parentFile
tag = 'the/tag'
}
When running the buildBaseImage task ./gradlew buildBaseImage --info, the execution hangs and finally fails with:
org.apache.http.conn.ConnectTimeoutException: Connect to 192.168.59.103:2376 [/192.168.59.103] failed: Connection timed out
I suspect there's a problem with my docker closure which is copied from the examples:
docker {
url = 'http://192.168.59.103:2376'
registryCredentials {
url = 'https://index.docker.io/v1'
username = '${docker_user}'
password = '${docker_password}'
email = 'email#example.com'
}
}
I've tried different urls, ports etc. but the problem persists. Any ideas about what is causing this problem?
Worked around this by using a different Gradle plugin. The images can be built in a similar and simple fashion:
task buildBaseImage(type: Docker) {
baseImage 'java:8'
applicationName = "app-name"
tagVersion = 'latest'
runCommand "apt-get update"
runCommand "apt-get -qq -y install python3 python3-dev python3-pip"
}
And then pushed to Docker Hub via:
task pushBaseImage(type: Exec, dependsOn: 'buildBaseImage') {
executable 'docker'
args = ['push','-f', getGroup() + '/my-base']
}
Using force to skip the push confirmation.
docker push --help
-f, --force=false Push to public registry without confirmation
This was my way of working around the connection issues.
Here's the required configurations:
apply plugin: 'docker'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'se.transmode.gradle:gradle-docker:1.2'
}
}
group = "myGroup"
docker {
maintainer 'Maintainer "maintainer#example.com"'
}

Resources