Hello All
I have different triggers for UAT and PROD docker builds and deploy. I recently discovered that when i build for my candidate branch its also building my production image as well which is worrying.
I have the targets specified in the stage but seems to ignore somewhere.
Here is my code:
pipeline {
agent {
label "aws"
}
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
timestamps()
}
environment {
SLACK_CHANNEL = "digital-dealership-cdci"
XDEBUG_MODE = "coverage"
AWS_CREDS = credentials('aws_cdk_deploy')
REPO_ADDRESS = "blah blah"
REPO = "blah"
AWS_ECR_REPO = "${REPO_ADDRESS}/${REPO}"
PHP_BUILD = "${AWS_ECR_REPO}:php_${env.GIT_COMMIT}"
NGINX_BUILD = "${AWS_ECR_REPO}:nginx_${env.GIT_COMMIT}"
max = 9999
randomPort = "${Math.abs(new Random().nextInt(max+1))}"
}
stages {
stage('Testing') {
when {
anyOf {
branch 'PR-*'
branch 'candidate'
branch 'master'
}
beforeAgent true
}
steps {
script {
checkout scm
docker.image('mysql:latest').withRun("-p ${env.randomPort}:3306 -e 'MYSQL_PASSWORD=password' -e 'MYSQL_ROOT_PASSWORD=password' -e 'MYSQL_ROOT_HOST=%' -e 'MYSQL_DATABASE=test'") {
c ->
docker.image('mysql:latest').inside("--link ${c.id}:db") {
/* Wait until mysql service is up */
sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
}
def app = docker.build("${BUILD_NUMBER}", "--build-arg ENV=testing --target digital_dealer_php .")
app.inside("--link ${c.id}:db -e 'DB_HOST=db'") {
echo 'Running PHP tests inside the container...'
sh 'php -v'
echo 'Running composer install...'
sh 'composer install --ignore-platform-reqs'
echo 'Running PHPUnit tests...'
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
sh '''
./vendor/bin/phpunit \
--coverage-html ./report/clover \
--coverage-clover ./report/clover.xml \
--testdox-html ./report/testdox.html \
--log-junit ./report/junit.xml
'''
echo 'Building JUnit report...'
junit "report/*.xml"
echo 'Publishing the coverage report and test repost HTML...'
publishHTML(target: [
allowMissing : true,
alwaysLinkToLastBuild: true,
keepAll : true,
reportDir : "report",
reportFiles : 'clover/dashboard.html,clover/index.html,testdox.html,',
reportName : 'CI-Build-HTML-Report',
reportTitles : 'Dashboard,Coverage Report,Test Case Report'
])
// If PHP unit fails then abort the build process
script {
if (currentBuild.result == 'FAILURE') {
error('Tests failed. Aborting.')
}
}
}
}
}
}
}
}
stage('Build UAT Docker Images') {
when {
branch 'candidate'
}
steps {
sh """
aws ecr get-login-password --region af-south-1 | docker login --username AWS --password-stdin ${REPO_ADDRESS}
docker build --target digital_dealer_nginx_uat -t ${NGINX_BUILD} .
docker build --target digital_dealer_php_uat -t ${PHP_BUILD} .
"""
}
}
stage('Build PROD Docker Images') {
when {
branch 'master'
}
steps {
sh """
aws ecr get-login-password --region af-south-1 | docker login --username AWS --password-stdin ${REPO_ADDRESS}
docker build --target digital_dealer_nginx_prod -t ${NGINX_BUILD} .
docker build --target digital_dealer_php_prod -t ${PHP_BUILD} .
"""
}
}
stage('Pushing To ECR') {
when {
anyOf {
branch 'candidate'
branch 'master'
}
}
steps {
sh """
docker tag ${NGINX_BUILD} ${AWS_ECR_REPO}:nginx
docker push ${AWS_ECR_REPO}:nginx
docker tag ${PHP_BUILD} ${AWS_ECR_REPO}:php
docker push ${AWS_ECR_REPO}:php
docker push ${NGINX_BUILD}
docker push ${PHP_BUILD}
"""
}
}
stage('Deploy UAT') {
when {
branch 'candidate'
}
steps {
echo 'Deploying UAT CDK'
dir("aws/cdk") {
sh """
export PHP_TAG=php_${env.GIT_COMMIT}
export NGINX_TAG=nginx_${env.GIT_COMMIT}
npm ci
cdk deploy --hotswap dealeros-uat-web -c config=uat --require-approval=never
"""
}
}
}
stage('Deploy Production') {
when {
branch 'master'
}
steps {
echo 'Deploying PROD CDK'
dir("aws/cdk") {
sh """
export PHP_TAG=php_${env.GIT_COMMIT}
export NGINX_TAG=nginx_${env.GIT_COMMIT}
npm ci
cdk deploy --hotswap dealeros-prod-web -c config=production --require-approval=never
"""
}
}
}
}
post {
always {
sendSlackNotifcation()
}
}
}
def sendSlackNotifcation() {
if (currentBuild.currentResult == "SUCCESS") {
buildSummary = "Job: ${env.JOB_NAME}\n Status: *SUCCESS*\n Build Report: ${env.BUILD_URL}CI-Build-HTML-Report"
slackSend notifyCommitters: true, color: "good", message: "${buildSummary}", channel: "${SLACK_CHANNEL}"
} else {
buildSummary = "Job: ${env.JOB_NAME}\n Status: *FAILURE*\n Error description: \nBuild Report :${env.BUILD_URL}CI-Build-HTML-Report"
slackSend notifyCommitters: true, color: "danger", message: "${buildSummary}", channel: "${SLACK_CHANNEL}"
}
}
Dockerfile:
#UAT
FROM digital_dealer_php AS digital_dealer_php_uat
RUN composer install --ignore-platform-reqs --optimize-autoloader --no-dev
RUN echo "building UAT with composer and npm"
COPY --chown=${USERNAME}:${USERNAME} .env.uat $APP_HOME/.env
RUN php artisan optimize:clear
RUN php artisan storage:link
RUN npm ci && npm run prod
RUN php artisan migrate --force
#Prod
FROM digital_dealer_php AS digital_dealer_php_prod
RUN composer install --ignore-platform-reqs --optimize-autoloader --no-dev
RUN echo "building PROD with composer and npm"
COPY --chown=${USERNAME}:${USERNAME} .env.production $APP_HOME/.env
RUN php artisan optimize:clear
RUN php artisan storage:link
RUN npm ci && npm run prod
RUN php artisan migrate --force