Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEB-108: Prod deploy #22

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 56 additions & 91 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,103 +1,68 @@
String registryEndpoint = 'registry.comp.ystv.co.uk'

def image
String imageName = "ystv/web-api:${env.BRANCH_NAME}-${env.BUILD_ID}"

pipeline {
agent any
agent {
label 'docker'
}

environment {
REGISTRY_ENDPOINT = credentials('docker-registry-endpoint')
LOGGING_ENDPOINT = credentials('logging-endpoint')
}
environment {
DOCKER_BUILDKIT = '1'
}

stages {
stage('Update Components') {
steps {
sh "docker pull golang:1.17-alpine" // Update with current Go image
}
stages {
stage('Build image') {
steps {
script {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
image = docker.build(imageName)
}
}
stage('Build') {
steps {
sh 'docker build -t $REGISTRY_ENDPOINT/ystv/web-api:$BUILD_ID .'
}
}
stage('Registry Upload') {
steps {
sh 'docker push $REGISTRY_ENDPOINT/ystv/web-api:$BUILD_ID' // Uploaded to registry
}
}
stage('Deploy') {
stages {
stage('Staging') {
when {
branch 'master'
not {
expression { return env.TAG_NAME ==~ /v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/ }
}
}
environment {
APP_ENV = credentials('wapi-staging-env')
TARGET_SERVER = credentials('staging-server-address')
TARGET_PATH = credentials('staging-server-path')
}
steps {
sshagent(credentials : ['staging-server-key']) {
script {
sh 'rsync -av $APP_ENV deploy@$TARGET_SERVER:$TARGET_PATH/web-api/.env'
sh 'rsync -av docker-compose.deploy.yml deploy@$TARGET_SERVER:$TARGET_PATH/web-api/'
sh '''ssh -tt deploy@$TARGET_SERVER << EOF
cd $TARGET_PATH/web-api
BUILD_ID=$BUILD_ID \
REGISTRY_ENDPOINT=$REGISTRY_ENDPOINT \
LOGGING_ENDPOINT=$LOGGING_ENDPOINT \
docker-compose -f docker-compose.deploy.yml up -d \
--force-recreate

docker image prune -a -f --filter "label=site=api"
exit 0
EOF'''
}
}
}
}
stage('Production') {
when {
expression { return env.TAG_NAME ==~ /v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/ } // Checking if it is main semantic version release
}
environment {
APP_ENV = credentials('wapi-prod-env')
TARGET_SERVER = credentials('prod-server-address')
TARGET_PATH = credentials('prod-server-path')
}
steps {
sshagent(credentials : ['prod-server-key']) {
script {
sh 'rsync -av $APP_ENV deploy@$TARGET_SERVER:$TARGET_PATH/web-api/.env'
sh 'rsync -av docker-compose.deploy.yml deploy@$TARGET_SERVER:$TARGET_PATH/web-api/'
sh '''ssh -tt deploy@$TARGET_SERVER << EOF
cd $TARGET_PATH/web-api
BUILD_ID=$BUILD_ID \
REGISTRY_ENDPOINT=$REGISTRY_ENDPOINT \
LOGGING_ENDPOINT=$LOGGING_ENDPOINT \
docker-compose -f docker-compose.deploy.yml up -d \
--force-recreate
}
}

docker image prune -a -f --filter "label=site=api"
exit 0
EOF'''
}
}
}
}
stage('Push image to registry') {
steps {
script {
docker.withRegistry('https://' + registryEndpoint, 'docker-registry') {
image.push()
if (env.BRANCH_IS_PRIMARY) {
image.push('latest')
}
}
}
}
}
post {
success {
echo 'Very cash-money'
}
failure {
echo 'That is not ideal, cheeky bugger'

stage('Deploy') {
stages {
stage('Development') {
when {
expression { env.BRANCH_IS_PRIMARY }
}
steps {
build(job: 'Deploy Nomad Job', parameters: [
string(name: 'JOB_FILE', value: 'web-api-dev.nomad'),
text(name: 'TAG_REPLACEMENTS', value: "${registryEndpoint}/${imageName}")
])
}
}
always {
sh "docker image prune -f --filter label=site=api --filter label=stage=builder" // Removing the local builder image
sh 'docker image prune -a -f --filter "label=site=api"' // remove old image

stage('Production') {
when {
// Checking if it is semantic version release.
expression { return env.TAG_NAME ==~ /v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/ }
}
steps {
build(job: 'Deploy Nomad Job', parameters: [
string(name: 'JOB_FILE', value: 'web-api-prod.nomad'),
text(name: 'TAG_REPLACEMENTS', value: "${registryEndpoint}/${imageName}")
])
}
}
}
}
}
}
13 changes: 0 additions & 13 deletions docker-compose.deploy.yml

This file was deleted.