Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Created April 24, 2018 09:10
Show Gist options
  • Select an option

  • Save jesperronn/e03601c29f2a730084f60b67fedf17d6 to your computer and use it in GitHub Desktop.

Select an option

Save jesperronn/e03601c29f2a730084f60b67fedf17d6 to your computer and use it in GitHub Desktop.

Revisions

  1. jesperronn created this gist Apr 24, 2018.
    143 changes: 143 additions & 0 deletions Jenkinsfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@
    // example Jenkinsfile with the following features
    //
    // * parallel build
    // * Slack notifications
    // * Clickable links to relevant commits and healthcheck urls
    // * Multisite deployments

    def git_host = "https://github.com/company/project_name"
    def host = [
    integration: [
    DK: "http://example.dk.test/healthcheck",
    SE: "http://example.se.test/healthcheck"
    ],
    production: [
    DK: "https://example.dk/healthcheck",
    SE: "https://example.dk/healthcheck"
    ],
    ]
    // if a branch matches these names, we will trigger a deployment
    def deploy_branches = ['master', 'deployment']

    pipeline {
    agent any

    parameters {
    // choices are newline separated
    choice(choices: 'integration\nproduction', description: 'deploy env (if build success)', name: 'DEPLOY_TO')
    }

    options {
    ansiColor('xterm-color')
    buildDiscarder(logRotator(numToKeepStr: '5'))
    }

    stages {
    stage('build') {
    steps {
    checkout scm
    script {
    sh 'git submodule update --init'
    parallel(
    prebuild: { sh './bin/prebuild.sh' },
    build: { sh './bin/ci-build.sh' },
    )
    }
    }
    } // end stage#build

    stage('test & lint') {
    steps {
    script {
    if (env.BRANCH_NAME == 'master' && params.DEPLOY_TO == 'production' || env.BRANCH_NAME == 'deployment') {
    sh "echo tests already ran, this is deployment job"
    } else {
    parallel('lint': {
    sh './bin/lint.sh'
    }, 'test-dk (unit)': {
    sh './bin/test.sh dk unit'
    }, 'test-dk (acceptance)': {
    sh './bin/test.sh dk acceptance'
    }, 'test-se (unit)': {
    sh './bin/test.sh se unit'
    }, 'test-se (acceptance)': {
    sh './bin/test.sh se acceptance'
    })
    }
    }
    }

    post {
    always {
    script {
    if (env.BRANCH_NAME == 'master' && params.DEPLOY_TO == 'production' || env.BRANCH_NAME == 'deployment') {
    sh "echo tests already ran, this is deployment job"
    } else {
    checkstyle canComputeNew: false, canRunOnFailed: true, defaultEncoding: '', healthy: '', pattern: 'tmp/checkstyles/*.xml', unHealthy: ''
    junit keepLongStdio: false, testResults: 'tmp/reports/**/*.xml'
    }
    }
    }
    }
    } // end stage#test

    // precompile assets. This MUST be done after `npm install`, but before deployment
    // We could also do it before testing, if needed
    stage('Precompile') {
    steps {
    sh './bin/precompile.sh'
    }
    } // end stage#precompile

    // deploy to integration environment. Only branches master
    // and special branch named 'deployment' for testing deploys
    stage('Deploy') {
    steps {
    script {
    if (deploy_branches.contains(env.BRANCH_NAME)) {
    sh "echo Deploying to ${params.DEPLOY_TO}.."
    parallel(
    'Deploy DK': { sh "ENV='${params.DEPLOY_TO}' COUNTRY=dk ./bin/deploy.sh" },
    'Deploy SE': { sh "ENV='${params.DEPLOY_TO}' COUNTRY=dk ./bin/deploy.sh" },
    'SlackSend': {
    slackSend channel: '#status-notifications',
    color: '#819FF7',
    botUser: true,
    message: "Deploy (${params.DEPLOY_TO}) Started - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open - Jenkins>)"
    }
    )
    }
    }
    }

    post {
    success {
    script {
    if (deploy_branches.contains(env.BRANCH_NAME)) {
    slackSend channel: '#status-notifications',
    color: 'good',
    botUser: true,
    message: """
    PROJECT_NAME has been deployed to ${params.DEPLOY_TO} successfully. <${git_host}/commit/${env.GIT_COMMIT}|Open - Github>
    <${host[params.DEPLOY_TO]['DK']}|DK ${params.DEPLOY_TO}>, <${host[params.DEPLOY_TO]['SE']}|SE ${params.DEPLOY_TO}>
    """.stripIndent()
    }
    }
    }
    failure {
    script {
    if (deploy_branches.contains(env.BRANCH_NAME)) {
    slackSend channel: '#status-notifications',
    color: 'danger',
    botUser: true,
    message: """
    PROJECT_NAME build/deploy to to ${params.DEPLOY_TO} failed.
    <${git_host}/commit/${env.GIT_COMMIT}|Open - Github>, <${env.BUILD_URL}|Open - Jenkins>
    """.stripIndent()
    }
    }
    }
    }
    } // end stage#deploy
    }
    }