#!/usr/bin/env groovy internalRepo = "git@bitbucket.org:affinipay/affinipay-wordpress-plugin.git" publicRepo = "git@github.com:affinipay/affinipay-wordpress-plugin.git" targetBranch = "master" // Build parameterts properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'StringParameterDefinition', name: 'UserEmail', defaultValue: 'usharma@affinipay.com'], [$class: 'StringParameterDefinition', name: 'TestName', defaultValue: '*']] ]]) /* * Entry point: Select build type */ try { if(isReleaseBuild()){ performRelease() } else { performBuild() } currentBuild.result = 'SUCCESS' notifyBuildResult(currentBuild.result) bitbucketStatusNotify(buildState: 'SUCCESSFUL', credentialsId: '6e598f39-dc2c-45c3-8e2e-20eb59439759') } // Catch an handle errors in build pipeline catch(e) { currentBuild.result = 'FAILED' notifyBuildResult(currentBuild.result) emailFailed("uharma@affinipay.com", e) bitbucketStatusNotify(buildState: 'FAILED', credentialsId: '6e598f39-dc2c-45c3-8e2e-20eb59439759') throw(e) } /* * Build flows */ def performBuild(){ node("mono"){ lock('wordpress_plugin_docker') { stage_Prepare() stage_Build() // stage_Test() if (env.BRANCH_NAME == targetBranch) { stage_Tag() } } } } def performRelease(){ node("mono"){ commitMessageInput = stage_Confirm() stage_Prepare(true) stage_Publish(commitMessageInput) } } /* * Build stages */ def stage_Prepare(isReleaseBuild = false){ stage("Checkout"){ // Define environment variables env.TEST_RESULTS = 'reports/junit.xml' // Clear directory sh "rm -rf *" // Check out source // https://stackoverflow.com/a/38255364/123336 def scmUrl = scm.getUserRemoteConfigs()[0].getUrl() def scmCredentialsId = scm.getUserRemoteConfigs()[0].getCredentialsId() checkout([$class: 'GitSCM', branches: [[name: "${env.BRANCH_NAME}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', trackingSubmodules: false]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: scmCredentialsId, url: scmUrl]]]) withCredentials([file(credentialsId: 'afpDockerRootCA.key', variable: 'rootCAKeyFile')]) { // some block sh "cat $rootCAKeyFile > docker-resources/ssl-certificates/afpDockerRootCA.key" } withCredentials([file(credentialsId: 'afpDockerRootCA.pem', variable: 'rootCAPemFile')]) { // some block sh "cat $rootCAPemFile > docker-resources/ssl-certificates/afpDockerRootCA.pem" } // Clean up tags sh "git fetch" sh "git tag -l | xargs git tag -d && git fetch -t" if (!isReleaseBuild) { sh "git reset --hard origin/${env.BRANCH_NAME}" } // Check out common functions library identifier: 'jenkins-pipeline-library@client-libraries', retriever: modernSCM( [$class: 'GitSCMSource', remote: 'git@bitbucket.org:affinipay/jenkins-pipeline-library.git', credentialsId: scm.getUserRemoteConfigs()[0].getCredentialsId()]) // Update build status bitbucketStatusNotify(buildState: 'INPROGRESS', credentialsId: '6e598f39-dc2c-45c3-8e2e-20eb59439759') } } def stage_Build() { stage('Install') { sh ''' #!/bin/bash -l # Create a tarball of the plugin, create a site directory touch affinipay-plugin.tar.gz tar -czf affinipay-plugin.tar.gz --exclude=affinipay-plugin.tar.gz --exclude='./docker-resources' . mkdir site && cd site cp -R ../docker-resources/* . # Launch docker environment echo "y" | ./destroy.sh ./setup.sh --environment localhost --domain localhost # Install AffinPay Wordpress Plugin docker exec --user www-data wordpress mkdir wp-content/plugins/affinipay-wordpress docker cp run_tests.sh wordpress:/var/www/html/run_tests.sh mv ../affinipay-plugin.tar.gz wordpress/wp-content/plugins/affinipay-wordpress cd wordpress/wp-content/plugins/affinipay-wordpress tar -xzf affinipay-plugin.tar.gz && rm affinipay-plugin.tar.gz # Install AffiniPay PHP client library composer install ''' } } def stage_Test(){ stage("Test"){ sh ''' #!/bin/bash -l docker exec --user www-data wordpress ./run_tests.sh ''' junit 'site/wordpress/wp-content/plugins/affinipay-wordpress/junit.xml' sh ''' #!/bin/bash -l # Destroy docker environment cd site echo "y" | ./destroy.sh ''' } } def stage_Tag(){ stage("Tag"){ def versionName = getVersion() gitTag versionName } } def stage_Confirm(){ def inputMessage = "" stage("Confirm"){ inputMessage = input( id: 'CommitMessage', message: 'input your commit message: ', ok: 'Ok', parameters: [string(defaultValue: '', description: 'You can press Ok to use the default commit message "Changes for "', name: 'COMMIT_MSG')] ) } return inputMessage } def stage_Publish(commitMessage){ stage("Publish"){ // def message = commitMessage // def releaseVersion = getVersion(false) // if (message.length() == 0) { // message = "Changes for ${releaseVersion}" // } // gitPublishLib(publicRepo, targetBranch, message) // gitTag "Released-${env.BRANCH_NAME}" } } /* * Helpers */ boolean isReleaseBuild() { return env.BRANCH_NAME ==~ /^v[(\d+\.\d)]+\-b\d+$/ } def getVersion(includeBuildNumber = true){ // Get version def outputVersion = sh( returnStdout: true, script: "cat lib/chargeio/version.rb | grep -o '\".*\"' | sed 's@.*\"\\(.*\\)\".*@\\1@' " ).replace('version:','').replace('\n','') def versionString = "v${outputVersion}" if (includeBuildNumber) versionString = versionString + "-b${env.BUILD_NUMBER}" return versionString } def notifyBuildResult(String result) { def colorCode = (result == 'SUCCESS') ? '#36A64F' : '#D00000' def msg = "${env.JOB_NAME} [${env.BUILD_NUMBER}] ${result} (<${env.BUILD_URL}|Open>)" // slackSend (channel: "#devtools", color: colorCode, message: msg) } def emailFailed(email_to, error) { def msg = null if (!error) { msg = 'Error during build. You should fix it.\n${env.BUILD_URL}' } else { msg = "Error during ${env.STAGE_NAME} stage. You should fix it.\n${env.BUILD_URL} \n\n Error:\n ${error.getMessage()}" } mail subject: "Something is wrong with ${env.JOB_NAME} ${env.BUILD_ID}", to: email_to, body: msg }