Skip to content

Commit

Permalink
Merge pull request #22 from ystv/WEB-108-prod-deploy
Browse files Browse the repository at this point in the history
WEB-108: Prod deploy
  • Loading branch information
rmil authored Aug 8, 2022
2 parents 3a7c247 + b5bf45d commit dd246ed
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 104 deletions.
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.

0 comments on commit dd246ed

Please sign in to comment.