This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(git): only deploy site if it's a new commit, fixes #3655
- Loading branch information
1 parent
9a01c36
commit 031de3d
Showing
6 changed files
with
32,338 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.', | ||
); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.