Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
build(git): only deploy site if it's a new commit, fixes #3655
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Apr 21, 2021
1 parent 9a01c36 commit 031de3d
Show file tree
Hide file tree
Showing 6 changed files with 32,338 additions and 334 deletions.
6 changes: 6 additions & 0 deletions .cloudbuild/deploy.yaml
Expand Up @@ -8,6 +8,12 @@ steps:
args: ['run', 'cloud-secrets']
env:
- 'PROJECT_ID=$PROJECT_ID'
- name: 'node'
entrypoint: npm
args: ['run', 'HEAD']
env:
- 'PROJECT_ID=$PROJECT_ID'
- 'BUILD_ID=$BUILD_ID'
- name: node
entrypoint: npm
args: ['run', 'build']
Expand Down
59 changes: 59 additions & 0 deletions HEAD.js
@@ -0,0 +1,59 @@
const request = require('request');

/**
* @param {string} url
* @param {string} [method]
* @returns {Promise<*>}
*/
const requestPromise = (url, method = 'GET') => {
return new Promise((resolve, reject) =>
request({url, method}, (e, r) => {
let body;
try {
body = JSON.parse(r.body);
} catch {
body = r.body;
}
if (e) {
reject(e);
} else if (body.error) {
reject(body);
} else {
resolve(body);
}
}),
);
};

(async () => {
let deployedHEAD;
try {
deployedHEAD = await requestPromise('https://web.dev/HEAD');
} catch {
deployedHEAD = "COULDN'T GET DEPLOYED HEAD";
}
const currentHEAD = require('child_process')
.execSync('git rev-parse HEAD')
.toString()
.trim();

console.log(`Current HEAD: ${currentHEAD}`);
console.log(`Deployed HEAD: ${deployedHEAD}`);

if (currentHEAD === currentHEAD) {
console.log('The current and deployed HEADs are the same, stopping build.');
const projectId = process.env.PROJECT_ID;
const id = process.env.BUILD_ID;
const projectsBuildCancel = `https://cloudbuild.googleapis.com/v1/projects/${projectId}/builds/${id}:cancel`;
try {
const cancelledBuild = await requestPromise(projectsBuildCancel, 'POST');
console.log('CANCELLED BUILD', cancelledBuild);
} catch (e) {
console.log('FAILED TO CANCEL BUILD', e);
}
} else {
console.log(
'The current and deployed HEADs are different, continuing build.',
);
}
})();
18 changes: 18 additions & 0 deletions gulp-tasks/write-head.js
@@ -0,0 +1,18 @@
const isProd = process.env.ELEVENTY_ENV === 'prod';

/**
* Write the HEAD SHA to the `dist` on prod builds.
*
* @returns {Promise<void>}
*/
const writeHEAD = async () => {
if (isProd) {
const HEAD = require('child_process')
.execSync('git rev-parse HEAD')
.toString()
.trim();
require('fs').writeFileSync('./dist/HEAD', HEAD);
}
};

module.exports = writeHEAD;
2 changes: 2 additions & 0 deletions gulpfile.js
Expand Up @@ -21,6 +21,7 @@ const copyFonts = require('./gulp-tasks/copy-fonts.js');
const copyGlobalImages = require('./gulp-tasks/copy-global-images.js');
const copyMisc = require('./gulp-tasks/copy-misc.js');
const sassTask = require('./gulp-tasks/sass.js');
const writeHead = require('./gulp-tasks/write-head.js');

gulp.task('copy-content-assets', copyContentAssets);
gulp.task('default-locale', copyDefaultLocale);
Expand All @@ -35,6 +36,7 @@ gulp.task(
copyContentAssets,
copyFonts,
sassTask,
writeHead,
),
);

Expand Down

0 comments on commit 031de3d

Please sign in to comment.