diff --git a/Jenkinsfile.groovy b/Jenkinsfile.groovy index c0bedcfb..12dad1c8 100644 --- a/Jenkinsfile.groovy +++ b/Jenkinsfile.groovy @@ -17,15 +17,10 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -def dockerHubRepo = "icgcargo/argo-gateway" -def githubRepo = "icgc-argo/platform-api" -def commit = "UNKNOWN" - pipeline { agent { kubernetes { - label 'gateway-executor' - yaml """ + yaml ''' apiVersion: v1 kind: Pod spec: @@ -58,99 +53,184 @@ spec: volumes: - name: docker-graph-storage emptyDir: {} -""" +''' } } + + environment { + gitHubRegistry = 'ghcr.io' + gitHubRepo = 'icgc-argo/platform-api' + gitHubImageName = "${gitHubRegistry}/${gitHubRepo}" + chartsServer = 'https://overture-stack.github.io/charts-server/' + DEPLOY_TO_DEV = false + PUBLISH_IMAGE = false + + commit = sh( + returnStdout: true, + script: 'git describe --always' + ).trim() + + version = sh( + returnStdout: true, + script: + 'cat ./package.json | ' + + 'grep "version" | ' + + 'cut -d : -f2 | ' + + "sed \'s:[\",]::g\'" + ).trim() + } + + parameters { + booleanParam( + name: 'DEPLOY_TO_DEV', + defaultValue: "${env.DEPLOY_TO_DEV}", + description: 'Deploys your branch to argo-dev' + ) + booleanParam( + name: 'PUBLISH_IMAGE', + defaultValue: "${env.PUBLISH_IMAGE ?: params.DEPLOY_TO_DEV}", + description: 'Publishes an image with {git commit} tag' + ) + } + + options { + timeout(time: 30, unit: 'MINUTES') + timestamps() + } + stages { - stage('Prepare') { + stage('Run tests') { steps { - script { - commit = sh(returnStdout: true, script: 'git describe --always').trim() - } - script { - version = sh(returnStdout: true, script: 'cat ./package.json | grep version | cut -d \':\' -f2 | sed -e \'s/"//\' -e \'s/",//\'').trim() + container('node') { + sh 'npm ci' + sh 'DEBUG=testcontainers npm run test-base' } } } - stage('Test') { + + stage('Builds image') { steps { - container('node') { - sh "npm ci" - sh "DEBUG=testcontainers npm run test-base" + container('docker') { + sh "docker build --network=host -f Dockerfile . -t ${gitHubImageName}:${commit}" } } } - stage('Builds Image') { + stage('Publish tag to npm') { + when { + branch 'main' + } steps { - container('docker') { - sh "docker build --network=host -f Dockerfile . -t ${dockerHubRepo}:${commit}" + container('node') { + withCredentials([ + usernamePassword( + credentialsId: 'argoGithub', + passwordVariable: 'GIT_PASSWORD', + usernameVariable: 'GIT_USERNAME' + ), + string( + credentialsId: 'JenkinsFailuresSlackChannelURL', + variable: 'JenkinsTagsSlackChannelURL' + ) + ]) { + script { + // we still want to run the platform deploy even if this fails, hence try-catch + try { + sh "git tag ${version}" + sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${gitHubRepo} --tags" + sh "curl \ + -X POST \ + -H 'Content-type: application/json' \ + --data '{ \ + \"text\":\"New ARGO-Gateway published succesfully: v.${version}\ + \n[Build ${env.BUILD_NUMBER}] (${env.BUILD_URL})\" \ + }' \ + ${JenkinsTagsSlackChannelURL}" + } catch (err) { + echo 'There was an error while publishing packages' + } + } + } } } } - stage('Deploy to argo-dev') { + stage('Publish images') { when { anyOf { - branch "develop" + branch 'develop' + branch 'main' + expression { return params.PUBLISH_IMAGE } } } steps { container('docker') { - withCredentials([usernamePassword(credentialsId:'argoDockerHub', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - sh 'docker login -u $USERNAME -p $PASSWORD' + withCredentials([usernamePassword( + credentialsId:'argoContainers', + passwordVariable: 'PASSWORD', + usernameVariable: 'USERNAME' + )]) { + sh "docker login ${gitHubRegistry} -u $USERNAME -p $PASSWORD" + + script { + if (env.BRANCH_NAME ==~ 'main') { //push edge and commit tags + sh "docker tag ${gitHubImageName}:${commit} ${gitHubImageName}:${version}" + sh "docker push ${gitHubImageName}:${version}" + + sh "docker tag ${gitHubImageName}:${commit} ${gitHubImageName}:latest" + sh "docker push ${gitHubImageName}:latest" + } else { // push commit tag + sh "docker tag ${gitHubImageName}:${commit} ${gitHubImageName}:${commit}" + sh "docker push ${gitHubImageName}:${commit}" + } + + if (env.BRANCH_NAME ==~ 'develop') { // push edge tag + sh "docker tag ${gitHubImageName}:${commit} ${gitHubImageName}:edge" + sh "docker push ${gitHubImageName}:edge" + } + } } - // DNS error if --network is default - sh "docker tag ${dockerHubRepo}:${commit} ${dockerHubRepo}:${version}-${commit}" - sh "docker tag ${dockerHubRepo}:${commit} ${dockerHubRepo}:edge" + } + } + } - sh "docker push ${dockerHubRepo}:${version}-${commit}" - sh "docker push ${dockerHubRepo}:edge" + stage('Deploy to argo-dev') { + when { + anyOf { + branch 'develop' + expression { return params.DEPLOY_TO_DEV } } - build(job: "/ARGO/provision/gateway", parameters: [ - [$class: 'StringParameterValue', name: 'AP_ARGO_ENV', value: 'dev' ], - [$class: 'StringParameterValue', name: 'AP_ARGS_LINE', value: "--set-string image.tag=${version}-${commit}" ] + } + steps { + build(job: '/ARGO/provision/gateway', parameters: [ + string(name: 'AP_ARGO_ENV', value: 'dev'), + string(name: 'AP_ARGS_LINE', value: "--set-string image.tag=${commit}"), ]) } } stage('Deploy to argo-qa') { when { - anyOf{ - branch "master" + anyOf { + branch 'main' } } steps { - container('docker') { - withCredentials([usernamePassword(credentialsId: 'argoGithub', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { - sh "git tag ${version}" - sh "git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${githubRepo} --tags" - } - - withCredentials([usernamePassword(credentialsId:'argoDockerHub', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - sh 'docker login -u $USERNAME -p $PASSWORD' - } - - sh "docker tag ${dockerHubRepo}:${commit} ${dockerHubRepo}:${version}" - sh "docker tag ${dockerHubRepo}:${commit} ${dockerHubRepo}:latest" - - sh "docker push ${dockerHubRepo}:${version}" - sh "docker push ${dockerHubRepo}:latest" - } - build(job: "/ARGO/provision/gateway", parameters: [ - [$class: 'StringParameterValue', name: 'AP_ARGO_ENV', value: 'qa' ], - [$class: 'StringParameterValue', name: 'AP_ARGS_LINE', value: "--set-string image.tag=${version}" ] + build(job: '/ARGO/provision/gateway', parameters: [ + string(name: 'AP_ARGO_ENV', value: 'qa'), + string(name: 'AP_ARGS_LINE', value: "--set-string image.tag=${version}"), ]) } } } + post { unsuccessful { // i used node container since it has curl already - container("node") { + container('node') { script { - if (env.BRANCH_NAME == "master" || env.BRANCH_NAME == "develop") { - withCredentials([string(credentialsId: 'JenkinsFailuresSlackChannelURL', variable: 'JenkinsFailuresSlackChannelURL')]) { + if (env.BRANCH_NAME == 'main' || env.BRANCH_NAME == 'develop') { + withCredentials([string(credentialsId: 'JenkinsFailuresSlackChannelURL', variable: 'JenkinsFailuresSlackChannelURL')]) { sh "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"Build Failed: ${env.JOB_NAME} [${env.BUILD_NUMBER}] (${env.BUILD_URL}) \"}' ${JenkinsFailuresSlackChannelURL}" } } @@ -158,11 +238,11 @@ spec: } } fixed { - container("node") { + container('node') { script { - if (env.BRANCH_NAME == "master" || env.BRANCH_NAME == "develop") { - withCredentials([string(credentialsId: 'JenkinsFailuresSlackChannelURL', variable: 'JenkinsFailuresSlackChannelURL')]) { - sh "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"Build Fixed: ${env.JOB_NAME} [${env.BUILD_NUMBER}] (${env.BUILD_URL}) \"}' ${JenkinsFailuresSlackChannelURL}" + if (env.BRANCH_NAME == 'main' || env.BRANCH_NAME == 'develop') { + withCredentials([string(credentialsId: 'JenkinsFailuresSlackChannelURL', variable: 'JenkinsSucessesSlackChannelURL')]) { + sh "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"Build Fixed: ${env.JOB_NAME} [${env.BUILD_NUMBER}] (${env.BUILD_URL}) \"}' ${JenkinsSuccessesSlackChannelURL}" } } }