Skip to content

Instantly share code, notes, and snippets.

@consolewitch
Created February 6, 2020 01:47
Show Gist options
  • Save consolewitch/0fabf34da669191ac989726abd8f6347 to your computer and use it in GitHub Desktop.
Save consolewitch/0fabf34da669191ac989726abd8f6347 to your computer and use it in GitHub Desktop.
jenkins scripted pipeline for review.
properties([
buildDiscarder(
logRotator(
artifactDaysToKeepStr: '',
artifactNumToKeepStr: '',
daysToKeepStr: '60',
numToKeepStr: ''
)
),
pipelineTriggers([githubPush()])
])
String shortHash=""
String dockerTag=""
node ('pipelines') {
stage('build') {
parallel (
"create-vm": {
node('pipelines'){
git branch: 'pipelines',
changelog: false,
poll: false,
credentialsId: 'removed',
url: '[email protected]:example/ci.git'
withCredentials([
usernamePassword(
credentialsId: 'example',
passwordVariable: 'EXAMPLE',
usernameVariable: 'EXAMPLE'
)
]) {
sh '''
### stand up an empty VM
'''
stash name: "vmAddress", includes: "deploy/created_vm_address.txt"
}
}
},
"build-from-source": {
node('pipelines'){
def scmVars = checkout([
$class: 'GitSCM',
branches: [[name: 'origin/dev']],
userRemoteConfigs: [[url: '[email protected]:example/app.git']]
])
withCredentials([
file(
credentialsId: 'removed',
variable: 'EXAMPLE'
)
]) {
shortHash=scmVars.GIT_COMMIT;
shortHash = shortHash.substring(0,7);
echo "Computed short hash is :${shortHash}";
branchComponents = scmVars.GIT_BRANCH;
branchComponents = branchComponents.split('/');
if (branchComponents[0] == 'origin') {
branchComponents = branchComponents.drop(1);
}
dockerTag = branchComponents.join('--');
dockerTag = dockerTag.plus("-" + shortHash);
echo "Computed docker tag is :${dockerTag}";
withEnv(["DOCKER_TAG=${dockerTag}"]) {
sh '''
### build code and bake docker container
'''
}
}
}
}
)
}
stage('deploy-to-vm') {
git branch: 'pipelines',
changelog: false,
poll: false,
credentialsId: 'removed',
url: '[email protected]:example/ci.git'
withCredentials([
file(
credentialsId: 'removed',
variable: 'EXAMPLE'
)
]) {
unstash "vmAddress"
withEnv(["DOCKER_TAG=${dockerTag}"]) {
sh '''
### deploy newly built container to the VM that runs it
'''
}
}
}
stage('api-test') {
def scmVars = checkout([
$class: 'GitSCM',
branches: [[name: 'origin/dev']],
userRemoteConfigs: [[url: '[email protected]:example/app.git']]
])
unstash "vmAddress"
withCredentials([
file(
credentialsId: 'removed',
variable: 'EXAMPLE'
)
]) {
sh '''
### execute API tests
'''
}
}
}
@kim-at-simspace
Copy link

I decided it is more important to keep more builds on master than other branches, so I updated the buildDiscarder to take the branch name into account ... buildDiscarder(logRotator(numToKeepStr: env.BRANCH_NAME == 'master' ? '100':'10'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment