How to build a Pipeline basic with Jenkins - jenkins

I try to use Pipeline script and I arrive until last step.
The Pipeline script is the following code
node{
stage ('Build') {
git url: 'https://xxx#bitbucket.org/xxx/01-eureka.git'
withMaven(
// Maven installation declared in the Jenkins "Global Tool Configuration"
maven: 'M3',
// Maven settings.xml file defined with the Jenkins Config File Provider Plugin
// Maven settings and global settings can also be defined in Jenkins Global Tools Configuration
mavenSettingsConfig: 'my-maven-settings',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
} // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs reports...
}
}
I have already been configured the maven and Java.
I don't know the error of the last step , I update all code.

I don't exact ENV as your's, but first thing first, you should check the your Jenkins workspace folder Jenkins\workspace\docker-app, to see if the git clone works as expected
If the source code are there, manual run the maven command to see if Maven finds the pom.xml correctly

Related

Jenkins Unable to Detect the Ant

I have installed jenkins in windows machine, and i configured environment variable as well.
When i am checking ant -version in cmd i can able to get the response from the terminal "Apache Ant version 1.7.1 compiled on June 27 2008".
Jenkins Configuration
Ant plugin installed.
Ant home configured in jenkins
Ant config in jenkins
I am checking ant -version in pipeline script, but i am getting build failed in jenkins with following error message "ant' is not recognized as an internal or external command"
stage('studio'){
steps {
bat 'ant -version'
}
}
Can you please someone help on this issue.
You are mssing some details in the construction .. bat does not know about ant. See How to invoke Ant in a Jenkins Groovy Pipeline
def antVersion = 'Ant1.9.1'
withEnv( ["ANT_HOME=${tool antVersion}"] ) {
bat '%ANT_HOME%/bin/ant.bat target1 target2'`
}
Also, don't name it ANT_HOME, but something relevant like ant-1.7.1

Is there any way to convert maven project to pipeline in an automated way

Having some maven projects. I want to change it to a scripted pipeline in jenkins
To automate the following example you can use the Jenkins API via the groovy script console or an groovy system script to create you jobs programmatically.
Example for scripted Pipeline:
node{
stage ('Build') {
git url: 'https://github.com/cyrille-leclerc/multi-module-maven-project'
withMaven(
// Maven installation declared in the Jenkins "Global Tool Configuration"
maven: 'M3',
// Maven settings.xml file defined with the Jenkins Config File Provider Plugin
// Maven settings and global settings can also be defined in Jenkins Global Tools Configuration
mavenSettingsConfig: 'my-maven-settings',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
} // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs reports...
}
}
You need the Pipeline Maven Plugin: https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin

dsl script for maven type Jenkins jobs

I writing the DSL Script for maven type Jenkins jobs.
https://jenkinsci.github.io/job-dsl-plugin/#path/mavenJob-properties
With help of above link I written almost DSL script, but unable to find the DSL script for "Build and Build setting" step sections(For maven type jobs).
Please help me how to mention the above two steps in DSL script.
I found the solution for build step for maven type jobs
add the build step (goal) after pre-build step like
mavenJob('example-1') {
preBuildSteps {
}
goals('clean install -DskipTests')
postBuildSteps {
}
}

Gradle tool in Jenkins Declarative Pipeline

I defined a Jenkins Declarative pipeline to CI/CD my project. I am using gradle as my build tool. However I don't want to use the Gradle Wrapper and check it int the VCS. So I planed on using the jenkins tools functionality as below so that I can update the version number if I need to in future. But it doesn't seem to work.
pipeline {
agent any
tools {
gradle "gradle-4.0"
}
stage("Compile") {
steps {
sh 'gradle project/build.gradle classes'
}
}
I get the error "script.sh: gradle: not found".
I tried to echo PATH and that doesn't contain the path of this autoinstalled gradle tool. Please help.
Looks like there is an issue on the gradle plugin for Jenkins on plugin version 1.26. Please see the link to the bug reported below.
https://issues.jenkins-ci.org/browse/JENKINS-42381

Auto generate build pipeline for gradle build using Jenkinsfile

I am trying to create a build pipeline based upon the Gradle tasks. I have viewed JenkinsFile configuration Pipeline-as-code-demo but I am unable to create a pipeline for gradle tasks. Please suggest me a possible way so that I can use the Jenkinsfile to automatically show the build pipeline just by reading the configurations from the Jenkinsfile.
Thankyou
In case your project uses Gradle Wrapper you can use the following snippet in your Jenkinsfile:
stage('Gradle Build') {
if (isUnix()) {
sh './gradlew clean build'
} else {
bat 'gradlew.bat clean build'
}
}
If you checkout to subdirectory sub-dir you might want to use
stage('Gradle Build') {
if (isUnix()) {
dir('sub-dir') {sh './gradlew clean build'}
} else {
dir('sub-dir') {bat 'gradlew.bat clean build'}
}
}
In case you're using Artifactory to resolve your build dependencies or to deploy your build artifacts, it is recommended to use the Pipeline DSL for Gradle build with Artifactory.
Here's an example taken from the Jenkins Pipeline Examples page:
node {
// Get Artifactory server instance, defined in the Artifactory Plugin administration page.
def server = Artifactory.server "SERVER_ID"
// Create an Artifactory Gradle instance.
def rtGradle = Artifactory.newGradleBuild()
stage 'Clone sources'
git url: 'https://github.com/jfrogdev/project-examples.git'
stage 'Artifactory configuration'
// Tool name from Jenkins configuration
rtGradle.tool = "Gradle-2.4"
// Set Artifactory repositories for dependencies resolution and artifacts deployment.
rtGradle.deployer repo:'ext-release-local', server: server
rtGradle.resolver repo:'remote-repos', server: server
stage 'Gradle build'
def buildInfo = rtGradle.run rootDir: "gradle-examples/4/gradle-example-ci-server/", buildFile: 'build.gradle', tasks: 'clean artifactoryPublish'
stage 'Publish build info'
server.publishBuildInfo buildInfo
}
Otherwise, you can simply run the gradle command with the sh or bat Pipeline steps.
In jenkins you can creates a jenkins pipeline using a script which is written in Jenkinsfile.
We write a script using 'stages' and 'node' as building block, these building blocks allow you to specify instructions that should be executed as part of jenkins pipeline.
To execute gradle build using JenkinsFile first check for Operating system and call appropriate shell that can execute that gradle task, as below:
Jenkinsfile
stage 'build_Project'
node{
if(isUnix()){
sh 'gradle build --info'
}
else{
bat 'gradle build --info'
}
}
Above code snippet create a stage with name build_project and execute gradle build script of the current project.

Resources