Posts

Showing posts with the label Jenkins Pipeline

"Build Periodically" With A Multi-branch Pipeline In Jenkins

Answer : If you use a declarative style Pipeline and only want to trigger the build on a specific branch you can do something like this: String cron_string = BRANCH_NAME == "master" ? "@hourly" : "" pipeline { agent none triggers { cron(cron_string) } stages { // do something } } Found on Jenkins Jira If you are using a declarative style Jenkinsfile then you use the triggers directive. pipeline { agent any triggers { cron('H 4/* 0 0 1-5') } stages { stage('Example') { steps { echo 'Hello World' } } } } I was able to find an example illustrating this an discarding old builds, which is also something I wanted. Jenkinsfile in jenkins-infra/jenkins.io: properties( [ [ $class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '10'] ], ...

Cobertura Code Coverage Report For Jenkins Pipeline Jobs

Answer : There is a way to add a pipeline step to publish your coverage report but it doesn't show under the BlueOcean interface. It will show fine in the normal UI. pipeline { agent any stages { ... } post { always { junit '**/nosetests.xml' step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/coverage.xml', failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false]) } } } Note that one of the parameters to the Cobertura plugin is the XML that it will use ('**/coverage.xml' in the example). If you are using python, you will want to use something like: nosetests --with-coverage --cover-xml --cover-package=pkg1,pkg2 --with-xunit test Nowadays you can also use the cobertura command directly in a Jenkinsfile stage ("Extract...

Can I Define Multiple Agent Labels In A Declarative Jenkins Pipeline?

Answer : You can see the 'Pipeline-syntax' help within your Jenkins installation and see the sample step "node" reference. You can use exprA||exprB : node('small||medium') { // some block } This syntax appears to work for me: agent { label 'linux && java' } EDIT: I misunderstood the question. This answer is only if you know which specific agent you want to run for each stage. If you need multiple agents you can declare agent none and then declare the agent at each stage. https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents From the docs: pipeline { agent none stages { stage('Build') { agent any steps { checkout scm sh 'make' stash includes: '**/target/*.jar', name: 'app' } } stage('Test on Linux') { agent { label ...

Checkout Jenkins Pipeline Git SCM With Credentials?

Answer : You can use the following in a pipeline: git branch: 'master', credentialsId: '12345-1234-4696-af25-123455', url: 'ssh://git@bitbucket.org:company/repo.git' If you're using the ssh url then your credentials must be username + private key. If you're using the https clone url instead of the ssh one, then your credentials should be username + password. To explicitly checkout using a specific credentials stage('Checkout external proj') { steps { git branch: 'my_specific_branch', credentialsId: 'my_cred_id', url: 'ssh://git@test.com/proj/test_proj.git' sh "ls -lat" } } To checkout based on the configred credentials in the current Jenkins Job stage('Checkout code') { steps { checkout scm } } You can use both of the stages within a single Jenkins file. If you want...