[build] Improve ccache freshness

Save a fresh ccache on every build by including (a checksum of) the current revision in the cache key. In the worst case, saving the cache appears to take ~1:30 for android-release; other builds appear to take a small amount of time compared to the time saved by having a fresh cache, versus a cache that's up to a week old.

Restore caches in the following priority order (all scoped to current architecture and job):

1. The cache for the current branch and revision.

2. If that doesn't exist, the most recent cache for the current branch.

3. If that doesn't exist, the cache for the parent branch and merge base, where "parent branch" means the base branch of the pull request specified by $CIRCLE_PULL_REQUEST (obtained from the GitHub API), and "merge base" means the revision returned by `git merge-base` when given the parent branch and $CIRCLE_BRANCH (obtained by executing that shell command). The intent here is to start a newly created branch off with the most relevant cache from its parent branch.

4. If that doesn't exist, the most recent cache for the parent branch.

5. If that doesn't exist, the most recent cache for master.

6. If that doesn't exist, use the most recent cache for any build.

We cannot use the branch names or revisions themselves as key segments, because CircleCI lacks a way to interpolate arbitrary environment variables into the cache key name. Instead, we write the desired value into a file and use the `{{ checksum "filename" }}` syntax.
This commit is contained in:
John Firebaugh
2018-07-05 13:08:28 -07:00
parent 3896a65c8e
commit 74ef93ab4f
2 changed files with 118 additions and 30 deletions

24
scripts/environment.js Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env node
// Output some `export` commands with a few extra environment variables. They'll be evaluated
// into the build environment and available for use in later steps.
const github = require('@octokit/rest')();
const {execSync} = require('child_process');
const pr = process.env['CIRCLE_PULL_REQUEST'];
if (pr) {
const number = +pr.match(/\/(\d+)\/?$/)[1];
return github.pullRequests.get({
owner: 'mapbox',
repo: 'mapbox-gl-native',
number
}).then(({data}) => {
const base = data.base.ref;
const head = process.env['CIRCLE_SHA1'];
const mergeBase = execSync(`git merge-base origin/${base} ${head}`).toString().trim();
console.log(`export CIRCLE_TARGET_BRANCH=${base}`);
console.log(`export CIRCLE_MERGE_BASE=${mergeBase}`);
});
}