Skip to content

Instantly share code, notes, and snippets.

@SeanHood
Created August 27, 2021 08:56
Show Gist options
  • Save SeanHood/db0ee3e3bb59ab1894342b7ebc543bc5 to your computer and use it in GitHub Desktop.
Save SeanHood/db0ee3e3bb59ab1894342b7ebc543bc5 to your computer and use it in GitHub Desktop.
Painless Puppet CICD: Bitbucket Branch Source Magic + a Puppet/r10k Jenkinsfile

infra_jobs

What does this do?

This Job DSL file is to configure the Bitbucket Branch Source plugin within Jenkins to scan for all repos within the INFRA project, and create jobs for all branches which contain a Jenkinsfile. As it's currently configured, this plugin is set to scan for new repos every 60 minutes.

These jobs will then be built when changes are pushed to Bitbucket, either to run tests, build source code or deploy artifacts etc. Using Jenkinsfile means your CICD processes are tied to your source code rather than managed elsewhere.

Requirements

Plugins

Credentials

  • jenkins-bitbucket username/password for Bitbucket
  • jenkins-bitbucket-ssh ssh private key with it's matching public key set up in Bitbucket under the Project's Access Keys
organizationFolder("INFRA") {
displayName('Infrastructure')
description('This contains branch source jobs for the Infra team')
// scan for new repo's every n minutes
triggers {
periodic(60)
}
organizations {
bitbucket {
serverUrl("https://bitbucket.exmaple.com")
credentialsId('jenkins-bitbucket')
repoOwner('INFRA')
traits {
cloneOptionTrait {
extension {
noTags(false) // We want git tags
shallow(false) // We want all the commits
reference('')
timeout(10)
}
}
}
}
}
configure { node ->
def traits = node / navigators / 'com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator' / traits
traits << 'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait' {
strategyId('3') // Build all branches
}
traits << 'com.cloudbees.jenkins.plugins.bitbucket.SSHCheckoutTrait' {
credentialsId('jenkins-bitbucket-ssh')
}
traits << 'com.cloudbees.jenkins.plugins.bitbucket.OriginPullRequestDiscoveryTrait' {
strategyId('1') // Merging the pull request with the current target branch revision
}
// Only build for these branches, and also PRs
traits << 'jenkins.scm.impl.trait.WildcardSCMHeadFilterTrait' {
includes('master develop production development release/* PR-*')
excludes('')
}
}
// don't keep build jobs for deleted branches
orphanedItemStrategy {
discardOldItems {
numToKeep(0)
}
}
projectFactories {
workflowMultiBranchProjectFactory {
}
}
}
pipeline {
agent any
stages {
stage('Tests') {
parallel {
stage('Validate Puppetfile') {
agent { dockerfile { reuseNode true } }
steps {
sh 'bundle exec rake r10k:syntax'
}
}
stage('Validate Hiera') {
agent { dockerfile { reuseNode true } }
steps {
sh 'bundle exec rake syntax:hiera'
}
}
stage('Validate Syntax') {
agent { dockerfile { reuseNode true } }
steps {
sh 'bundle exec rake syntax'
}
}
stage('Puppet Lint') {
agent { dockerfile { reuseNode true } }
steps {
sh 'bundle exec rake lint'
}
}
}
}
stage('Checks') {
steps {
recordIssues enabledForFailure: true, tool: puppetLint(), qualityGates: [[threshold: 2, type: 'NEW', unstable: true]]
}
}
stage('Deploy') {
when {
not {
changeRequest branch: "hotfix/*", comparator: "GLOB"
}
}
steps {
retry (3) {
sshagent (credentials: ['jenkins-puppet-ssh']) {
sh 'ssh jenkins@puppetserver "/opt/puppetlabs/puppet/bin/r10k deploy environment -v -p ${CHANGE_BRANCH:-$BRANCH_NAME}"'
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment