Jenkins configuration for a project with local project dependencies - jenkins-pipeline

I have a automation project which makes use of .jar of a util project. I am very new to Jenkins so can somebody guide me as to how to create a jenkin job and handle local project jar dependencies aswell.

Since this is jenkins pipeline you need to do the following.
Create a repo with a jenkinsfile*
Create a multi branch pipeline job in jenkins and point to your repo.
When a build is started it will interpret your jenkinsfile and execute all the defined steps.
By making use of a .jar what do you mean? Is it an executable on the same machine? In that case add it to your path and use it when executing a shell or a bat script in the jenkinsfile.
More info here: https://www.jenkins.io/doc/book/pipeline/
*Example jenkinsfile:
pipeline {
agent any
options {
//Discard old builds, not necessary but nice to have
buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '20'))
}
environment {
// Example of how to retrieve credentials and set them as environment variables
EXAMPLE_CREDENTIAL= credentials('EXAMPLE_CREDENTIAL')
}
stages {
stage('A build step') {
steps {
// do your stuff here, this can also be divided into several stages like one for building the code and one for executing it
}
}
}
// Post build actions, e.g archiving, clean up etc
post {
always {
archiveArtifacts artifacts: '**/*.*', fingerprint: true
deleteDir()
}
}
}

I would not build in dependencies to local projects. If it's just an executable .jar file you can add it as a secret file the same way you add credentials and while executing the job copy it to your workspace like this:
environment {
FILE = credentials('my_file')
}
stages {
stage('Preperation'){
steps {
// Copy your fie to the workspace
sh "cp ${FILE} ${WORKSPACE}"
// Verify the file is copied.
sh "ls -la"
}
}
}
An alternative could be adding the file to your path and access the executable via the command line when executing the job.
If you want the whole project I would definitely check that out. I.e you need to add a a shell command for checking the project out.

Thanks Jan for your direction. I achieved output with the following code:
pipeline
agent any <br/>
stages {<br/>
stage('Building dependent project') { <br/>
steps {<br/>
echo "Building dependent project"<br/>
git (
url: 'https:**********.git',
credentialsId: '***********************',
branch: "master"
)
sh "mvn clean install -DskipTests"
}
}
stage('Building and testing main Project') {
steps {
git (
url: 'https://************.git',
credentialsId: '******************',
branch: "exp3"
)
sh "mvn clean install -Dapp=${App} -Denv=${Envir} -Dversion=${Version}"
}
}
}
}

Related

Jenkins maven project violation report in pipeline project

I am trying to convert jenkins maven project to pipeline project, we have mvn clean install step and next violation plugin can someone help me How to include violation report in pipeline project (check style and findbugs)
In declarative style, using the new Warnings Next Generation plugin, you would do something like
pipeline {
agent any
stages {
... pre-conditions & other stuff previously handled by your jenkins maven job ...
stage('Build') {
steps {
withMaven {
sh 'mvn clean install'
}
}
}
... post-conditions previously handled your jenkins maven job ...
}
post {
always {
recordIssues(
enabledForFailure: true, aggregatingResults: true,
tools: [java(), checkStyle(pattern: 'checkstyle-result.xml', reportEncoding: 'UTF-8'), findBugs(pattern: 'findbugs.xml')]
)
}
}
}
See the pipeline documentation page for more details about syntax etc

Split Jenkins pipeline into environment part and stages part as a template

We use a Jenkins server to create multiple iOS/Android projects with Unity and Xcode. At the moment we have a single Jenkins file in each root directory of our project repositories with the entire configuration and build steps in one file:
pipeline {
agent any
environment {
REPOSITORY_URL = 'ssh://git#XYZ.git'
PROJECT_SUBDIR = ''
BUILD_FOR_ANDROID = 'true'
BUILD_FOR_IOS = 'true'
UNITY_VERSION = '2018.4.0f1'
}
// how to load this from a template file? -->
stages {
stage('Checkout') {
steps {
script {
if (env.PROJECT_SUBDIR) {
env.PROJECT_DIR = sh(
script: "echo ${env.WORKSPACE}/${env.PROJECT_SUBDIR}",
returnStdout: true
).trim()
} else {
env.PROJECT_DIR = sh(
script: "echo ${env.WORKSPACE}",
returnStdout: true
).trim()
}
}
// more stuff ...
}
}
}
// <--
}
Is it possible to load the part "stages" from another file (e.g. a template) during the build process? This would separate the project-dependent configuration part from the common "stages" part and I could update the build process for all project repositories in a single or versioned template file?
How would it work with the environment variables? Are they still in the same context or do I have to pass them?
That was a good advice.
I managed it by using the last approach mentioned on this page:
https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-declarative-pipelines
So I copied my pipeline in my groovy file called
/vars/run_pipeline_template.groovy
in my new repository. Before that I called it pipeline.groovy which wasn't my best idea because that is a protected keyword. I left the definitions of my environment variables in the former Jenkinsfile. They can still be accessed by env.VAR_NAME without the need to forward them to the library script. The library is in its own Git repository and I added this library to the Jenkins configuration in section Global Pipeline Libraries. And this solves the version problem as well, because you can define which branch/tag/hash is used when importing the library template in the Jenkinsfile.
Thank you.

Why is Gradle Plugin not collecting build scan in Jenkins file

I have a Jenkinsfile based pipeline which does a build using gradle, which then produces build scan that goes in console output. I found a Jenkins plugin (https://wiki.jenkins.io/display/JENKINS/Gradle+Plugin) that scans the console and nicely displays all build scan links.
When I integrated in my Jenkinsfile based pipeline it does not work.
Here the Jenkins file:
node {
// This displays colors using the 'xterm' ansi color map.
try {
wrap([$class: 'BuildScanBuildWrapper']) {
stage "Create build output"
println "Doing gradle build"
sh "cd projects/ospackage-plugin/ && ./gradlew -I ./init.gradle tasks"
}
}
catch (err) {
println "FAILURE: ${err}"
throw err
}
}
Starting with the plugin version 1.33, it is now possible to collect build scan links from pipeline Jobs: https://plugins.jenkins.io/gradle#GradlePlugin-CapturingbuildscansfromJenkinsPipeline
Add findBuildScans() to the end of your pipeline script:
node {
...
}
findBuildScans()

Jenkins Declarative Pipeline with custom settings.xml

I'm trying to set up a Jenkins Declarative Pipeline with maven. So far I can get maven to run, but I can't get it to use my defined Maven Settings.xml.
pipeline{
agent any
tools{
maven 'Apache Maven 3.3'
// without mavenSettingsConfig, my settings.xml is not used. With it, this blows up
mavenSettingsConfig: 'Global Maven Settings'
jdk 'jdk9
}
stages {
stage('Preparation'){
steps{
//code checkout stuff here--this works fine
}
}
stage('Build'){
steps{
sh "mvn clean install -P foo"
}
}
}
}
The problem seems to be mavenSettingsConfig. Without that property, I can't figure out how to set the settings.xml, and my custom maven stuff doesn't work. (Profile foo, for example.) With the mavenSettingsConfig, it blows up:
BUG! exception in phase 'canonicalization' in source unit 'WorkflowScript' unexpected NullpointerException....
The documentation has a big TODO in it where it would provide an example for this! So how do I do it?
(Documentation TODO at https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin. It actually says "TODO provide a sample with Jenkins Declarative Pipeline")
my advice is to use the Config File Provider plugin: https://wiki.jenkins.io/display/JENKINS/Config+File+Provider+Plugin
With it, you define your config file once in Jenkins' "Config File Management" screen and then have code like this in your pipeline:
stage('Build'){
steps{
configFileProvider([configFile(fileId: 'my-maven-settings-dot-xml', variable: 'MAVEN_SETTINGS_XML')]) {
sh 'mvn -U --batch-mode -s $MAVEN_SETTINGS_XML clean install -P foo'
}
}
}
Hope it helps
you have to declare and maven installation in your jenkins
Managed Jenkins > Global Tools configuration and add maven installation named like M3.
declare a maven installation
After you have to registry your settings file :
manage jenkins > Managed files
And add your setting File
After this you can use the WithMaven function with your registry file like this:
steps {
withMaven(maven: 'M3', mavenSettingsConfig: 'mvn-setting-xml') {
sh "mvn clean install "
}
}
Also possible, to use the secret file credentials from Credentials Binding Plugin
Create a secret file in jenkins:
Then you can use this settings file like this
pipeline {
environment {
MVN_SET = credentials('maven_settings')
}
agent {
docker 'maven:3-alpine'
}
stages {
stage('mvn test settings') {
steps {
sh 'mvn -s $MVN_SET help:effective-settings'
}
}
}
}
I had this issue all you have to do is add this small piece of code in your line
def mvnSettings = 'Location of the file'
sh "mvn clean install --settings ${mvnSettings} -P foo"
So now whenever maven runs it will locate the settings.xml file in the PATH that you specified
P.S. its a maven command which you can use to run on command Line
Hope it helps :)
Combining the accepted answer of #Francois Marot and the link provided by the OP, we get:
pipeline {
stages {
stage ('Build') {
steps {
withMaven() {
bat 'mvn clean install'
}
}
}
}
}
This uses both the "Managed Files" plugin and the "Global Tool Configuration" plugin (Maven configuration, Maven installation, JDK installation) to specify the settings.xml file implicitly.

Jenkinsfile error- java.lang.NoSuchMethodError: No such DSL method 'withMaven' found among steps

I am currently trying to implement pipeline in jenkins using jenkinsfile and i am executing a maven project on windows machine. I am creating a pipeline job in jenkins and i have checked in this file in my github repository and when i am running the job in jenkins , i am getting following error.
My jenkinsfile:
pipeline {
agent any
stages {
stage('Compile stage') {
steps {
maven(maven : 'Maven_3.5.2'){
bat "mvn clean compile"
}
}
}
stage('testing stage') {
steps {
maven(maven : 'Maven_3.5.2'){
bat "mvn test"
}
}
}
stage('deployment stage') {
steps {
maven(maven : 'Maven_3.5.2'){
bat "mvn deploy"
}
}
}
}
}
I am getting below error when i am running it through jenkins job-
Jenkins error:
java.lang.NoSuchMethodError: No such DSL method 'withMaven' found
among steps [archive, bat, build, catchError, checkout, deleteDir,
dir, dockerFingerprintFrom, dockerFingerprintRun, echo, emailext,
emailextrecipients, envVarsForTool, error, fileExists, getContext,
git, input, isUnix, library, libraryResource, load, mail, milestone,
node, parallel, powershell, properties, pwd, readFile, readTrusted,
resolveScm, retry, script, sh, sleep, stage, stash, step, svn,
timeout, timestamps, tm, tool, unarchive, unstash,
validateDeclarativePipeline, waitUntil, withContext, withCredentials,
withDockerContainer, withDockerRegistry, withDockerServer, withEnv,
wrap, writeFile, ws] or symbols [all, allOf, always, ant,
antFromApache, antOutcome, antTarget, any, anyOf, apiToken,
architecture, archiveArtifacts, artifactManager, authorizationMatrix,
batchFile, booleanParam, branch,
Any help?
This means you don't have withMaven as an available DSL method. Most of the time this means you don't have a plugin installed. In this case, the Pipeline Maven Integration plugin is required. https://plugins.jenkins.io/pipeline-maven/
Try this:
pipeline {
agent any
tools {
maven 'Maven_3.5.2'
}
stages {
stage('Compile stage') {
steps {
bat "mvn clean compile"
}
}
stage('testing stage') {
steps {
bat "mvn test"
}
}
stage('deployment stage') {
steps {
bat "mvn deploy"
}
}
}
}
Reference: https://jenkins.io/doc/book/pipeline/syntax/
On top of Rob Hales' answer, it is called "Pipeline Maven Integration Plugin" in Jenkins ver. 2.73.3 or later
You need to install all listed "pipeline" plugin and error no longer will be there.

Resources