Skip to content

Commit

Permalink
JSUI-3398 create new invalidate cloudfront job in prod (#1875)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibodeauJF authored May 6, 2022
1 parent 610dfb7 commit 89e2cea
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 54 deletions.
13 changes: 13 additions & 0 deletions .deployment.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@
"disabled": false
}
}
},
{
"id": "prod-cloudfront-invalidation",
"team_jenkins": {
"disabled": true,
"job_name": "search_ui/job/cloudfront_invalidation",
"extra_parameters": {
"MAJOR_MINOR_VERSION": "$[MAJOR_MINOR_VERSION]"
},
"prd": {
"disabled": false
}
}
}
],
"certifiers": {
Expand Down
11 changes: 0 additions & 11 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ node('linux && docker') {

sh 'node ./build/deployment-pipeline.deploy.js || true'
}

stage('Cloudfront invalidation') {
withCredentials([
[
$class: "AmazonWebServicesCredentialsBinding",
credentialsId: "CloudfrontCacheInvalidation",
]
]) {
sh 'node invalidate.cloudfront.js'
}
}
}

}
Expand Down
22 changes: 22 additions & 0 deletions JenkinsfileCloudfrontInvalidation
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
node('linux && docker') {
checkout scm

withEnv([
'npm_config_cache=npm-cache'
]){
withDockerContainer(image: 'nikolaik/python-nodejs:python3.8-nodejs14', args: '-u=root') {
stage('Cloudfront invalidation') {
withCredentials([
[
$class: "AmazonWebServicesCredentialsBinding",
credentialsId: "CloudfrontCacheInvalidation",
]
]) {
sh 'yarn install'
sh 'node invalidate.cloudfront.js'
}
}
}

}
}
69 changes: 26 additions & 43 deletions invalidate.cloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,33 @@ const colors = require('colors');
const AWS = require('aws-sdk');

const cloudfront = new AWS.CloudFront();
const pathToInvalidate = `/searchui/v${process.env.PACKAGE_JSON_VERSION}/*`;
const pathToInvalidate = `/searchui/v${process.env.MAJOR_MINOR_VERSION}/*`;

const shouldDoInvalidation = () => {
if (!process.env.TRAVIS) {
return false;
const invalidationRequest = cloudfront.createInvalidation({
DistributionId: 'E2VWLFSCSD1GLA',
InvalidationBatch: {
CallerReference: new Date().getTime().toString(),
Paths: {
Quantity: 1,
Items: [pathToInvalidate]
}
}
if (!process.env.TAG_NAME) {
return false;
});

invalidationRequest.send((error, success) => {
if (error) {
console.log(colors.red('ERROR WHILE INVALIDATING RESSOURCES ON CLOUDFRONT'));
console.log(colors.red('*************'));
console.log(colors.red(error.message));
console.log(colors.red('*************'));
exec('travis_terminate 1');
throw error;
}
if (process.env.TAG_NAME.indexOf('beta') != -1) {
return false;
if (success) {
console.log(colors.green('INVALIDATION ON CLOUDFRONT SUCCESSFUL'));
console.log(colors.green('*************'));
console.log(colors.green(`PATH INVALIDATED: ${pathToInvalidate}`));
console.log(colors.green(`INVALIDATION ID : ${success.Invalidation.Id}`));
console.log(colors.green('*************'));
}
return true;
};

if (shouldDoInvalidation()) {
const invalidationRequest = cloudfront.createInvalidation({
DistributionId: process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID,
InvalidationBatch: {
CallerReference: new Date().getTime().toString(),
Paths: {
Quantity: 1,
Items: [pathToInvalidate]
}
}
});

invalidationRequest.send((error, success) => {
if (error) {
console.log(colors.red('ERROR WHILE INVALIDATING RESSOURCES ON CLOUDFRONT'));
console.log(colors.red('*************'));
console.log(colors.red(error.message));
console.log(colors.red('*************'));
exec('travis_terminate 1');
throw error;
}
if (success) {
console.log(colors.green('INVALIDATION ON CLOUDFRONT SUCCESSFUL'));
console.log(colors.green('*************'));
console.log(colors.green(`PATH INVALIDATED: ${pathToInvalidate}`));
console.log(colors.green(`INVALIDATION ID : ${success.Invalidation.Id}`));
console.log(colors.green('*************'));
}
});
} else {
console.log(colors.white('INVALIDATION FROM CLOUDFRONT SKIPPED BECAUSE THIS IS NOT AN OFFICIAL RELEASE'));
}
});

0 comments on commit 89e2cea

Please sign in to comment.