Skip to content

Instantly share code, notes, and snippets.

@chancez
Last active April 12, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancez/b033ea166e5dea7a3862beb1ff86eb7d to your computer and use it in GitHub Desktop.
Save chancez/b033ea166e5dea7a3862beb1ff86eb7d to your computer and use it in GitHub Desktop.
#!/usr/bin/env groovy
properties([
buildDiscarder(logRotator(
artifactDaysToKeepStr: '',
artifactNumToKeepStr: '',
daysToKeepStr: '14',
numToKeepStr: '20',
)),
disableConcurrentBuilds(),
[$class: 'GithubProjectProperty', displayName: '', projectUrlStr: 'https://github.com/coreos-inc/soy/'],
[$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false],
parameters([
booleanParam(defaultValue: false, description: '', name: 'OVERRIDE_PUSH_ASSETS'),
booleanParam(defaultValue: false, description: '', name: 'OVERRIDE_DEPLOY'),
]),
pipelineTriggers([
[$class: 'PeriodicFolderTrigger', interval: '2h'],
]),
])
def needsDeploy = params.OVERRIDE_DEPLOY
def deployEnvironment = ""
def gitCommit = ""
switch (env.BRANCH_NAME) {
case "master":
needsDeploy = true
deployEnvironment = "dev"
break;
case "staging":
needsDeploy = true
deployEnvironment = "staging"
break;
case "production":
needsDeploy = true
deployEnvironment = "production"
break
default:
// For OVERRIDE_DEPLOY. If it's set, then we'll deploy to dev
deployEnvironment = "dev"
break;
}
notifyBuild('STARTED')
echo """
Branch Name: ${env.BRANCH_NAME}
Override Push Assets: ${params.OVERRIDE_PUSH_ASSETS}
Override Deploy: ${params.OVERRIDE_DEPLOY}
"""
podTemplate(
cloud: 'us-east-1a-k8s-v6',
containers: [
containerTemplate(
name: 'jnlp',
image: 'jenkinsci/jnlp-slave:alpine',
alwaysPullImage: true,
ttyEnabled: false,
),
containerTemplate(
alwaysPullImage: false,
envVars: [],
command: 'dockerd-entrypoint.sh',
args: '--storage-driver=overlay',
volumes: [
emptyDirVolume(
memory: false,
mountPath: '/var/lib/docker',
),
],
image: 'docker:dind',
name: 'docker',
privileged: true,
resourceRequestCpu: '1000m',
resourceRequestMemory: '500Mi',
resourceLimitCpu: '2000m',
resourceLimitMemory: '2048Mi',
ttyEnabled: true,
),
],
instanceCap: 5,
label: 'commerce-build-us-east-v1',
name: 'commerce-build-us-east-v1',
) {
node ('commerce-build-us-east-v1') {
try {
container('docker'){
sh """
apk update
apk add git bash
"""
checkout scm
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
}
echo "Git Commit: ${gitCommit}"
withCredentials([
[$class: 'FileBinding', credentialsId: '5c73c092-c68e-42e5-ab88-44d1117de142', variable: 'DOCKER_CONFIG_JSON'],
[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'S3_CDN_KEY', credentialsId: 'tectonic-com-s3-cdn-upload', secretKeyVariable: 'S3_CDN_SECRET'],
]) {
def dockerConfigDir = ''
container('docker'){
dockerConfigDir = sh(script: "dirname ${DOCKER_CONFIG_JSON}", returnStdout: true).trim()
}
writeFile file: 'config.env', text: """
export DOCKER_CONFIG=${dockerConfigDir}
export SOY_PG_CONTAINER_NAME=soy_pg_${env.BRANCH_NAME}
export DEX_PG_CONTAINER_NAME=dex_pg_${env.BRANCH_NAME}
export SOY_NETWORK_NAME=soy_net_${env.BRANCH_NAME}
export DEX_WORKER_CONTAINER_NAME=dex_worker_${env.BRANCH_NAME}
export DEX_OVERLORD_CONTAINER_NAME=dex_overlord_${env.BRANCH_NAME}
export PULL_PG_IMAGE=true
export S3_CDN_KEY=${env.S3_CDN_KEY}
export S3_CDN_SECRET=${env.S3_CDN_SECRET}
"""
container('docker'){
stage ('Test'){
sh "source config.env && ./scripts/unit-tests-docker.sh"
}
stage ('Build Binaries'){
sh "source config.env && ./scripts/build-binaries.sh"
}
stage ('Build Docker Images'){
sh "source config.env && ./scripts/build-docker-images.sh ${env.BRANCH_NAME}"
}
stage ('Validate'){
sh "source config.env && ./scripts/validate-no-changes.sh"
}
if (params.OVERRIDE_PUSH_ASSETS || needsDeploy) {
stage ('Push Docker Images'){
sh "source config.env && ./scripts/push-images-to-quay.sh ${env.BRANCH_NAME}"
}
stage ('Push Frontend Assets'){
sh 'source config.env && ./scripts/push-assets.sh'
}
} else {
echo 'Non master branch build: Skipping pushing docker images and frontend assets'
}
}
}
if (needsDeploy) {
stage ('Deploy') {
def dryRun = false
def imageTag = gitCommit
def deployJob = "/commerce/deploy/master"
if (deployEnvironment != "production") {
build(
job: deployJob,
parameters: [
string(name: 'OVERRIDE_IMAGE_TAG', value: imageTag),
booleanParam(name: 'DRY_RUN', value: dryRun),
booleanParam(name: 'ENABLE_DEBUG', value: false),
string(name: 'ENVIRONMENT', value: deployEnvironment),
],
quietPeriod: 30,
)
}
}
}
} catch (e) {
// If there was an exception thrown, the build failed
echo "Build failed"
currentBuild.result = "FAILED"
throw e
} finally {
notifyBuild(currentBuild.result)
}
}
}
def notifyBuild(String buildStatus = 'STARTED') {
def buildColor = ''
def buildEmoji = ''
// build status of null means successful
buildStatus = buildStatus ?: 'SUCCESSFUL'
// Override default values based on build status
switch (buildStatus) {
case 'STARTED':
buildColor = 'warning'
buildEmoji = ':open_mouth:'
break;
case 'SUCCESSFUL':
buildColor = 'good'
buildEmoji = ':smirk:'
break;
default:
buildColor = 'danger'
buildEmoji = ':cold_sweat:'
break;
}
notifySlack("${buildColor}",
"""
|*${env.JOB_NAME} build ${env.BUILD_NUMBER}*
|${buildEmoji} Build ${buildStatus}.
|link: ${env.BUILD_URL}
|branch: ${env.BRANCH_NAME}
""".stripMargin())
}
def notifySlack(color, message) {
slackSend(
channel: '#commerce-ci',
color: color,
message: message,
teamDomain: 'coreos',
tokenCredentialId: 'commerce-slack-jenkins-token',
failOnError: true,
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment