Skip to content

Commit

Permalink
Branch support in snapshots (angular#12692)
Browse files Browse the repository at this point in the history
* ci: snapshots can push to specific branches

Can take either a branch argument, or use CIRCLE_BRANCH if available.

* ci: run snapshots on all branches
  • Loading branch information
hansl authored and Keen Yee Liau committed Oct 23, 2018
1 parent 9ae9757 commit a53cf44
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ workflows:
- e2e-cli
filters:
branches:
only: master
ignore:
- /pull\/.*/
- publish:
requires:
- test
Expand Down
116 changes: 69 additions & 47 deletions scripts/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,72 @@ function _exec(command: string, args: string[], opts: { cwd?: string }, logger:
return stdout.toString('utf-8');
}

async function _publishSnapshot(
pkg: PackageInfo,
branch: string,
message: string,
logger: logging.Logger,
githubToken: string,
) {
if (!pkg.snapshot) {
logger.warn(`Skipping ${pkg.name}.`);

return;
}

logger.info(`Publishing ${pkg.name} to repo ${JSON.stringify(pkg.snapshotRepo)}.`);

const root = process.cwd();
const publishLogger = logger.createChild('publish');
publishLogger.debug('Temporary directory: ' + root);

const url = `https://${githubToken ? githubToken + '@' : ''}github.com/${pkg.snapshotRepo}.git`;
const destPath = path.join(root, path.basename(pkg.snapshotRepo));

_exec('git', ['clone', url], { cwd: root }, publishLogger);
if (branch) {
_exec('git', ['checkout', '-B', branch], { cwd: destPath }, publishLogger);
}

// Clear snapshot directory before publishing to remove deleted build files.
try {
_exec('git', ['rm', '-rf', './'], {cwd: destPath}, publishLogger);
} catch {
// Ignore errors on delete. :shrug:
}
_copy(pkg.dist, destPath);

if (githubToken) {
_exec('git', ['config', 'commit.gpgSign', 'false'], { cwd: destPath }, publishLogger);
}

// Add the header to the existing README.md (or create a README if it doesn't exist).
const readmePath = path.join(destPath, 'README.md');
let readme = readmeHeaderFn(pkg);
try {
readme += fs.readFileSync(readmePath, 'utf-8');
} catch {}

fs.writeFileSync(readmePath, readme);

// Make sure that every snapshots is unique (otherwise we would need to make sure git accepts
// empty commits).
fs.writeFileSync(path.join(destPath, 'uniqueId'), '' + new Date());

// Commit and push.
_exec('git', ['add', '.'], { cwd: destPath }, publishLogger);
_exec('git', ['commit', '-a', '-m', message], { cwd: destPath }, publishLogger);
_exec('git', ['tag', pkg.snapshotHash], { cwd: destPath }, publishLogger);
_exec('git', ['push', 'origin', branch], { cwd: destPath }, publishLogger);
_exec('git', ['push', '--tags', 'origin', branch], { cwd: destPath }, publishLogger);
}


export interface SnapshotsOptions {
force?: boolean;
githubTokenFile?: string;
githubToken?: string;
branch?: string;
}

export default async function(opts: SnapshotsOptions, logger: logging.Logger) {
Expand All @@ -83,6 +144,12 @@ export default async function(opts: SnapshotsOptions, logger: logging.Logger) {

const root = fs.mkdtempSync(path.join(os.tmpdir(), 'angular-cli-publish-'));
const message = execSync(`git log --format="%h %s" -n1`).toString().trim();
let branch = opts.branch || 'master';

// CIRCLE_BRANCH
if (typeof process.env['CIRCLE_BRANCH'] == 'string') {
branch = '' + process.env['CIRCLE_BRANCH'];
}

const githubToken = (
opts.githubToken
Expand Down Expand Up @@ -129,53 +196,8 @@ export default async function(opts: SnapshotsOptions, logger: logging.Logger) {
}

for (const packageName of Object.keys(packages)) {
const pkg = packages[packageName];

if (!pkg.snapshot) {
logger.warn(`Skipping ${pkg.name}.`);
continue;
}

logger.info(`Publishing ${pkg.name} to repo ${JSON.stringify(pkg.snapshotRepo)}.`);

const publishLogger = logger.createChild('publish');
publishLogger.debug('Temporary directory: ' + root);

const url = `https://${githubToken ? githubToken + '@' : ''}github.com/${pkg.snapshotRepo}.git`;
_exec('git', ['clone', url], { cwd: root }, publishLogger);

const destPath = path.join(root, path.basename(pkg.snapshotRepo));
// Clear snapshot directory before publishing to remove deleted build files.
try {
_exec('git', ['rm', '-rf', './'], {cwd: destPath}, publishLogger);
} catch {
// Ignore errors on delete. :shrug:
}
_copy(pkg.dist, destPath);

if (githubToken) {
_exec('git', ['config', 'commit.gpgSign', 'false'], { cwd: destPath }, publishLogger);
}

// Add the header to the existing README.md (or create a README if it doesn't exist).
const readmePath = path.join(destPath, 'README.md');
let readme = readmeHeaderFn(pkg);
try {
readme += fs.readFileSync(readmePath, 'utf-8');
} catch {}

fs.writeFileSync(readmePath, readme);

// Make sure that every snapshots is unique (otherwise we would need to make sure git accepts
// empty commits).
fs.writeFileSync(path.join(destPath, 'uniqueId'), '' + new Date());

// Commit and push.
_exec('git', ['add', '.'], { cwd: destPath }, publishLogger);
_exec('git', ['commit', '-a', '-m', message], { cwd: destPath }, publishLogger);
_exec('git', ['tag', pkg.snapshotHash], { cwd: destPath }, publishLogger);
_exec('git', ['push', 'origin'], { cwd: destPath }, publishLogger);
_exec('git', ['push', '--tags', 'origin'], { cwd: destPath }, publishLogger);
process.chdir(root);
await _publishSnapshot(packages[packageName], branch, message, logger, githubToken);
}

return 0;
Expand Down

0 comments on commit a53cf44

Please sign in to comment.