diff --git a/Jenkinsfile b/Jenkinsfile index 219b771..5d6c01c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,7 +1,6 @@ @Library('podTemplateLib') import net.santiment.utils.podTemplates - properties([buildDiscarder(logRotator(artifactDaysToKeepStr: '30', artifactNumToKeepStr: '', daysToKeepStr: '30', numToKeepStr: ''))]) slaveTemplates = new podTemplates() @@ -11,25 +10,35 @@ slaveTemplates.dockerTemplate { label -> stage('Build') { container('docker') { def scmVars = checkout scm - def gitHead = scmVars.GIT_COMMIT.substring(0,7) if (env.BRANCH_NAME == "main") { def backendUrl = "https://api-stage.santiment.net" def siteUrl = "https://santimentnet-stage.santiment.net" withCredentials([ - string( - credentialsId: 'SECRET_KEY_BASE', - variable: 'SECRET_KEY_BASE' - ), - string( - credentialsId: 'aws_account_id', - variable: 'aws_account_id' - ) + string(credentialsId: 'SECRET_KEY_BASE', variable: 'SECRET_KEY_BASE'), + string(credentialsId: 'aws_account_id', variable: 'aws_account_id') + ]) { + def awsRegistry = "${env.aws_account_id}.dkr.ecr.eu-central-1.amazonaws.com" + docker.withRegistry("https://${awsRegistry}", "ecr:eu-central-1:ecr-credentials") { + sh "docker build --build-arg BACKEND_URL=${backendUrl} --build-arg SITE_URL=${siteUrl} --build-arg SECRET_KEY_BASE=${SECRET_KEY_BASE} -t ${awsRegistry}/santimentnet:${env.BRANCH_NAME} -t ${awsRegistry}/santimentnet:${scmVars.GIT_COMMIT} ." + sh "docker push ${awsRegistry}/santimentnet:${env.BRANCH_NAME}" + sh "docker push ${awsRegistry}/santimentnet:${scmVars.GIT_COMMIT}" + } + } + } + + if (env.BRANCH_NAME == "production") { + def backendUrl = "https://api.santiment.net" + def siteUrl = "https://santiment.net" + + withCredentials([ + string(credentialsId: 'SECRET_KEY_BASE', variable: 'SECRET_KEY_BASE'), + string(credentialsId: 'aws_account_id', variable: 'aws_account_id') ]) { def awsRegistry = "${env.aws_account_id}.dkr.ecr.eu-central-1.amazonaws.com" docker.withRegistry("https://${awsRegistry}", "ecr:eu-central-1:ecr-credentials") { - sh "docker build --build-arg BACKEND_URL=${backendUrl} --build-arg SITE_URL=${siteUrl} -t ${awsRegistry}/santimentnet:${env.BRANCH_NAME} -t ${awsRegistry}/santimentnet:${scmVars.GIT_COMMIT} ." + sh "docker build --build-arg BACKEND_URL=${backendUrl} --build-arg SITE_URL=${siteUrl} --build-arg SECRET_KEY_BASE=${SECRET_KEY_BASE} -t ${awsRegistry}/santimentnet:${env.BRANCH_NAME} -t ${awsRegistry}/santimentnet:${scmVars.GIT_COMMIT} ." sh "docker push ${awsRegistry}/santimentnet:${env.BRANCH_NAME}" sh "docker push ${awsRegistry}/santimentnet:${scmVars.GIT_COMMIT}" } diff --git a/Jenkinsfile-production-deploy b/Jenkinsfile-production-deploy new file mode 100644 index 0000000..6d72b27 --- /dev/null +++ b/Jenkinsfile-production-deploy @@ -0,0 +1,61 @@ +podTemplate(label: 'santimentnet-production-deploy', nodeSelector: 'cpu.credits=no', containers: [ + containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.25.4', command: 'cat', ttyEnabled: true), + containerTemplate(name: 'docker', image: 'docker', ttyEnabled: true, command: 'cat', envVars: [ + envVar(key: 'DOCKER_BUILDKIT', value: '1'), + envVar(key: 'DOCKER_HOST', value: 'tcp://docker-host-docker-host:2375')]), + containerTemplate(name: 'awscli', image: 'mikesir87/aws-cli', ttyEnabled: true, command: 'cat', envVars: [ + envVar(key: 'AWS_DEFAULT_REGION', value: 'eu-central-1'), + secretEnvVar(key: 'AWS_ACCESS_KEY_ID', secretName: 'santimentnet-uploader-env', secretKey: 'awsAccessKeyId'), + secretEnvVar(key: 'AWS_SECRET_ACCESS_KEY', secretName: 'santimentnet-uploader-env', secretKey: 'awsSecretAccessKey'), + ]) +]) { + node('santimentnet-production-deploy') { + stage('Update deployment') { + git url: 'https://github.com/santiment/santiment.net/', credentialsId:'GitHubCheckoutCreds' + def gitCommit = sh(returnStdout: true, script: "git rev-parse HEAD").trim() + + withCredentials([ + string(credentialsId: 'aws_account_id', variable: 'aws_account_id') + ]){ + + def awsRegistry = "${env.aws_account_id}.dkr.ecr.eu-central-1.amazonaws.com" + def sourceImage = "${awsRegistry}/santimentnet" + def taggedSource = "${sourceImage}:${gitCommit}" + + container('docker') { + def timestampTag = "production-${env.TIMESTAMP_IMAGE_TAG}" + def taggedProd = "${sourceImage}:production" + def timestamped = "${sourceImage}:${timestampTag}" + + docker.withRegistry("https://${awsRegistry}", "ecr:eu-central-1:ecr-credentials") { + sh "docker pull ${taggedSource}" + sh "docker tag ${taggedSource} ${taggedProd}" + sh "docker tag ${taggedSource} ${timestamped}" + sh "docker push ${taggedProd}" + sh "docker push ${timestamped}" + + sh "mkdir -p artifacts" + sh "docker create --name santimentnet-temp ${taggedSource}" + sh "docker cp santimentnet-temp:/app/public ./artifacts/" + sh "docker rm santimentnet-temp" + } + } + } + } + + stage('Copy assets to S3') { + container('awscli') { + def bucket = 's3://santimentnet-production.santiment.net' + def sourceDir = './artifacts/public/' + + sh "cd ${sourceDir} && aws s3 sync . ${bucket}/ --delete --exclude index.html" + sh """ + aws s3 cp ${sourceDir}index.html ${bucket}/index.html \ + --metadata-directive REPLACE \ + --cache-control max-age=0,no-cache,no-store,must-revalidate \ + --content-type 'text/html; charset=utf-8' + """ + } + } + } +}