#!/usr/bin/env groovy // load helpers library 'jenkins-pipeline-libs' version = null is_master = ( "${env.BRANCH_NAME}" == "master" ) node('docker && !windows') { String gh_cid = scm.getUserRemoteConfigs()[0].getCredentialsId() String scmUrl = scm.getUserRemoteConfigs()[0].getUrl() String repo_name = getRepoFromURL(scmUrl) properties([ // dont keep builds in Jenkins. buildDiscarder( logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '3', daysToKeepStr: '', numToKeepStr: '30') ), // set url for diff links to gh [ $class: 'GithubProjectProperty', displayName: '', projectUrlStr: "${getGitHubURL(scmUrl)}" ] ]) try { stage('Checkout') { deleteDir() checkout scm } //invoke build steps stage('Builds') { //set display name currentBuild.displayName = getVersion() ansiColor('xterm') { sh 'gem build svcbase.gemspec' } // ansiColor } // stage //unit tests stage('Unit Tests') { String packages = "tzdata ruby-dev zlib-dev xz-dev build-base libxml2-dev libxslt-dev git" String env_cmd = "apk update && apk upgrade && apk add ${packages}" String test_cmd = "gem install bundler; cd /usr/src/app; bundle install --with development test; bundle exec rake" String full_cmd = "${env_cmd}; ${test_cmd}" String test_container = "svcbase-test-${env.BRANCH_NAME}-${env.BUILD_NUMBER}" String docker_img = "ruby:2.5.1-alpine" String container_dir = "/usr/src/app" sh """ #!/bin/bash -le docker pull ${docker_img} docker run -d -it --name ${test_container} ${docker_img} docker cp $WORKSPACE ${test_container}:${container_dir} docker exec ${test_container} ash -c "${env_cmd}" docker exec ${test_container} ash -c "${test_cmd}" # copy results back docker cp ${test_container}:/usr/src/app/coverage . # now stop container and image docker stop ${test_container} docker rm ${test_container} """ // publish ut results publishHTML([ allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'coverage', reportFiles: 'index.html', reportName: 'RCov Report' ]) } //stage // invoke any steps specific to the master(release) branch if ( is_master ) { stage('Tag & Publish') { withCredentials([usernameColonPassword(credentialsId: 'bf127b02-43c5-4ca9-8523-c2f22372ea7a', variable: 'ART_KEY')]) { String art_url = "https://artifactory.secureauth.com/artifactory/api/gems/rubygems-local" // set creds and push to artifactory (is there a better way :/ ) sh """ mkdir -p ~/.gem curl -s -u${ART_KEY} ${art_url}/api/v1/api_key.yaml > ~/.gem/credentials chmod 600 ~/.gem/credentials gem push svcbase-${getVersion()}.gem --host ${art_url} """ } applyTag(gh_cid, "${getVersion()}", scmUrl) } } } catch (e) { // set status to failed currentBuild.result = "FAILED" throw e } finally { // wrap it up stage('Archive, Clean & Notify') { // remove credentials sh "rm -f ~/.gem/credentials" String recipients = '#cloudteam_notify' String status = currentBuild.result ?: 'SUCCESS' String msg = "${env.JOB_NAME} - <${env.BUILD_URL}|${getVersion()}" + "> - ${status}\n\n${getChangeString()}" //send via library notifySlack { buildStatus = status channel = recipients message = msg } deleteDir() } } } // note - must be called after scm checkout String getVersion(){ if (!version) { if ( is_master ) { def matches = readFile('lib/svcbase/version.rb') =~ /VERSION *= *['"]?([0-9\.]+)['"]?/ version = matches ? matches[0][1] : null } else { version = "${env.BUILD_NUMBER}" } } return version }