Declarative Jenkisfile trigger downstream pipeline when dependency is built - maven

Is there any way to use the trigger downstream pipeline function when a dependency is built from the Pipeline Maven Integration plugin in a declarative Jenkisfile?
There is a promising TODO on the Jenkis plugin site.
Using "withMaven" with "docker.image(...).inside{...}" to a Jenkins Declarative Pipeline
TODO provide a sample with Jenkins Declarative Pipeline
Has anybody done that yet?
In my use case, I would like to trigger all downstream pipelines when a snapshot is built without using the GUI (I would prefer an infrastructure as code approach).

I'm not entirely sure, where the exact problem should be. Theoretically it should be
pipeline {
agent {
docker {
image 'openjdk:8-jdk'
}
}
stages {
stage('Test') {
withMaven() {
sh 'mvn clean install'
}
}
}
}

Correct syntax would be to use $MVN_CMD in the withMaven(). That will load the maven spy of the Jenkins Maven plugin.
pipeline {
agent {
docker {
image 'openjdk:8-jdk'
}
}
stages {
stage('Test') {
withMaven() {
sh '$MVN_CMD clean install'
}
}
}
}

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

Sonar branch plugin does not show anything

I've installed the opensource branch plugin on sonarqube 7.0.0 (the 1.0.1 release): https://github.com/msanez/sonar-branch-community
I've configured a pipeline step in Jenkins for a multibranch pipeline:
stage('Sonar Analyse'){
tools {
jdk 'ORACLE-JDK8-x86_64'
}
steps {
withSonarQubeEnv('SonarQube Test') {
dir('path') {
sh 'mvn -B sonar:sonar -Dsonar.branch.name=my-multi-branch'
}
}
}
}
After a run I can see the new branch in sonarqube. I can switch between the master and my multi-branch. While the master contains info about vulnerabilities, coverage, tests, code smells etc I can't see anything for my new branch:
We couldn't find any results matching selected criteria. Try to change
filters to get some results.
This is showing up when I click on my-multi-branch while all filters are reset. Am I missing some configuration in sonarqube/jenkins/maven?
I've switched between short and long living branches but same issue.
Have you tried using the "when" condition?
For example:
stage ('sonar-branch'){
when {
not {
branch 'master'
}
}
steps {
sh 'mvn -B sonar:sonar -Dsonar.branch=${env.BRANCH_NAME}'
}
}
Take a look at this link.

How to run two methods in Gradle in parallel mode?

I got a Gradle task which calls two methods. The methods are independent of each other so I want to start them parallel. What is the best way to achieve this?
Example:
task cucumber(dependsOn: 'testClasses') {
doLast {
// ...
// I want to call the next 2 methods in parallel
runSequentialTests(testEnvironment, tags)
runParallelTests(testEnvironment, tags)
}
}
def runSequentialTests(testEnvironment, tags) {
// execute cucumber tests via javaexec
}
def runParallelTests(testEnvironment, tags) {
// execute cucumber tests via exec sh script for each feature file (parallelism is done via GParsPool.withPool(5) {...}
}
Worth researching groovy parallel systems library gpars
GParsPool.withPool {
GParsPool.executeAsyncAndWait({runSequentialTests(testEnvironment, tags)}, {runParallelTests(testEnvironment, tags)})
}
Or maybe create a #ParallelizableTask
But I'm not sure what version of gradle you are using. And parallel is/was an incubating feature.
And needs to run build with --parallel if those have no dependencies between themselves Gradle should run them independently.
Alternatively, you can specify a property in your gradle.properties
C:\Users\<user>\.gradle\gradle.properties
org.gradle.parallel=true
I used the asynchronous invocations of GPars: http://www.gpars.org/webapp/guide/#_asynchronous_invocations
I got it to work with this. Thanks
GParsPool.withPool {
GParsPool.executeAsyncAndWait({runSequentialTests(testEnvironment, tags)}, {runParallelTests(testEnvironment, tags)})
}

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.

Is there a Maven plugin to create Jenkins job

I have about a hundred maven projects for which I would like to create Jenkins jobs. Instead of creating the jobs by hand, with the likely attendant mistakes, is there a way to create from a maven plugin?
There is the job-dsl plugin which allows you to build jobs in a groovy DSL
This works by providing a build step in a separate job
You can loop through your jobs as explained here
def giturl = 'https://github.com/quidryan/aws-sdk-test.git'
for(i in 0..10) {
job("DSL-Tutorial-1-Test-${i}") {
scm {
git(giturl)
}
steps {
maven("test -Dtest.suite=${i}")
}
}
}

Resources