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 23, 2021
1 parent 9a01c36 commit a018f5c
Show file tree
Hide file tree
Showing 6 changed files with 32,343 additions and 334 deletions.
12 changes: 12 additions & 0 deletions .cloudbuild/deploy.yaml
@@ -1,23 +1,35 @@
timeout: 2700s # set build timeout to 45 mins
steps:
- name: node
id: 'Install dependencies'
entrypoint: npm
args: ['ci']
- name: node
id: 'Generate .env file'
entrypoint: npm
args: ['run', 'cloud-secrets']
env:
- 'PROJECT_ID=$PROJECT_ID'
- name: 'node'
id: 'Verify new commit'
entrypoint: npm
args: ['run', 'HEAD']
env:
- 'BUILD_ID=$BUILD_ID'
- name: node
id: 'Build site'
entrypoint: npm
args: ['run', 'build']
env:
- 'ELEVENTY_ENV=prod'
- name: node
id: 'Configure firebase.json'
entrypoint: npm
args: ['run', 'firebase-config']
- name: 'gcr.io/$PROJECT_ID/firebase'
id: 'Deploy site to Firebase'
args: ['deploy']
- name: node
id: 'Algolia index site'
entrypoint: npm
args: ['run', 'algolia']
36 changes: 36 additions & 0 deletions HEAD.js
@@ -0,0 +1,36 @@
const request = require('request');
const {execSync} = require('child_process');
const {CloudBuildClient} = require('@google-cloud/cloudbuild');

const client = new CloudBuildClient();

/**
* @returns {Promise<string>}
*/
const getDeployedHead = () => {
return new Promise((resolve) =>
request('https://web.dev/HEAD', (e, r) => {
resolve(r.statusCode >= 400 || e ? '' : r.body);
}),
);
};

(async () => {
const deployedHEAD = await getDeployedHead();
const currentHEAD = 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 BUILD_ID = process.env.BUILD_ID;
client.cancelBuild({
id: BUILD_ID,
});
} else {
console.log(
'The current and deployed HEADs are different, continuing build.',
);
}
})();
17 changes: 17 additions & 0 deletions gulp-tasks/write-head.js
@@ -0,0 +1,17 @@
const {execSync} = require('child_process');

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 = 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 a018f5c

Please sign in to comment.