Skip to content

Commit

Permalink
Implement deploy to another repository
Browse files Browse the repository at this point in the history
  • Loading branch information
ebraminio committed Jun 9, 2020
1 parent b2ee598 commit 68a2623
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Config {
tool: ToolType;
outputFilePath: string;
ghPagesBranch: string;
ghRepository: string | undefined;
benchmarkDataDirPath: string;
githubToken: string | undefined;
autoPush: boolean;
Expand Down Expand Up @@ -207,6 +208,7 @@ export async function configFromJobInput(): Promise<Config> {
const tool: string = core.getInput('tool');
let outputFilePath: string = core.getInput('output-file-path');
const ghPagesBranch: string = core.getInput('gh-pages-branch');
const ghRepository: string = core.getInput('gh-repository');
let benchmarkDataDirPath: string = core.getInput('benchmark-data-dir-path');
const name: string = core.getInput('name');
const githubToken: string | undefined = core.getInput('github-token') || undefined;
Expand Down Expand Up @@ -249,6 +251,7 @@ export async function configFromJobInput(): Promise<Config> {
tool,
outputFilePath,
ghPagesBranch,
ghRepository,
benchmarkDataDirPath,
githubToken,
autoPush,
Expand Down
16 changes: 14 additions & 2 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export async function cmd(...args: string[]): Promise<string> {
return res.stdout;
}

function getRemoteUrl(token: string): string {
function getRemoteUrl(token: string, fullName?: string): string {
/* eslint-disable @typescript-eslint/camelcase */
const fullName = github.context.payload.repository?.full_name;
if (!fullName) fullName = github.context.payload.repository?.full_name;
/* eslint-enable @typescript-eslint/camelcase */

if (!fullName) {
Expand Down Expand Up @@ -87,3 +87,15 @@ export async function pull(token: string | undefined, branch: string, ...options

return cmd(...args);
}

export async function clone(token: string | undefined, ghGepository: string, branch: string, ...options: string[]): Promise<string> {
core.debug(`Executing 'git fetch' to branch '${branch}' of repository '${ghGepository}' with token and options '${options.join(' ')}'`);

const remote = token !== undefined ? getRemoteUrl(token, ghGepository) : 'origin';
let args = ['clone', remote, branch];
if (options.length > 0) {
args = args.concat(options);
}

return cmd(...args);
}
34 changes: 21 additions & 13 deletions src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ async function writeBenchmarkToGitHubPagesWithRetry(
name,
tool,
ghPagesBranch,
ghRepository,
benchmarkDataDirPath,
githubToken,
autoPush,
Expand All @@ -371,7 +372,12 @@ async function writeBenchmarkToGitHubPagesWithRetry(
const dataPath = path.join(benchmarkDataDirPath, 'data.js');
const isPrivateRepo = github.context.payload.repository?.private ?? false;

if (!skipFetchGhPages && (!isPrivateRepo || githubToken)) {
let benchmarkBaseDir = './';

if (!skipFetchGhPages && ghRepository) {
await git.clone(githubToken, ghRepository, ghPagesBranch, 'benchmarkDataRepository');
benchmarkBaseDir = './benchmarkDataRepository/';
} else if (!skipFetchGhPages && (!isPrivateRepo || githubToken)) {
await git.pull(githubToken, ghPagesBranch);
} else if (isPrivateRepo && !skipFetchGhPages) {
core.warning(
Expand All @@ -380,20 +386,20 @@ async function writeBenchmarkToGitHubPagesWithRetry(
);
}

await io.mkdirP(benchmarkDataDirPath);
await io.mkdirP(benchmarkBaseDir + benchmarkDataDirPath);

const data = await loadDataJs(dataPath);
const data = await loadDataJs(benchmarkBaseDir + dataPath);
const prevBench = addBenchmarkToDataJson(name, bench, data, maxItemsInChart);

await storeDataJs(dataPath, data);
await storeDataJs(benchmarkBaseDir + dataPath, data);

await git.cmd('add', dataPath);
await addIndexHtmlIfNeeded(benchmarkDataDirPath);
await git.cmd('commit', '-m', `add ${name} (${tool}) benchmark result for ${bench.commit.id}`);
await git.cmd('add', dataPath, '--work-tree=' + benchmarkBaseDir, '--git-dir=' + benchmarkBaseDir + '.git');
await addIndexHtmlIfNeeded(benchmarkBaseDir + benchmarkDataDirPath);
await git.cmd('commit', '-m', `add ${name} (${tool}) benchmark result for ${bench.commit.id}`, '--work-tree=' + benchmarkBaseDir, '--git-dir=' + benchmarkBaseDir + '.git');

if (githubToken && autoPush) {
try {
await git.push(githubToken, ghPagesBranch);
await git.push(githubToken, ghPagesBranch, '--work-tree=' + benchmarkBaseDir, '--git-dir=' + benchmarkBaseDir + '.git');
console.log(
`Automatically pushed the generated commit to ${ghPagesBranch} branch since 'auto-push' is set to true`,
);
Expand All @@ -407,7 +413,7 @@ async function writeBenchmarkToGitHubPagesWithRetry(

if (retry > 0) {
core.debug('Rollback the auto-generated commit before retry');
await git.cmd('reset', '--hard', 'HEAD~1');
await git.cmd('reset', '--hard', 'HEAD~1', '--work-tree=' + benchmarkBaseDir, '--git-dir=' + benchmarkBaseDir + '.git');

core.warning(
`Retrying to generate a commit and push to remote ${ghPagesBranch} with retry count ${retry}...`,
Expand All @@ -430,11 +436,13 @@ async function writeBenchmarkToGitHubPagesWithRetry(
}

async function writeBenchmarkToGitHubPages(bench: Benchmark, config: Config): Promise<Benchmark | null> {
const { ghPagesBranch, skipFetchGhPages } = config;
if (!skipFetchGhPages) {
await git.cmd('fetch', 'origin', `${ghPagesBranch}:${ghPagesBranch}`);
const { ghPagesBranch, ghRepository, skipFetchGhPages } = config;
if (!ghRepository) {
if (!skipFetchGhPages) {
await git.cmd('fetch', 'origin', `${ghPagesBranch}:${ghPagesBranch}`);
}
await git.cmd('switch', ghPagesBranch);
}
await git.cmd('switch', ghPagesBranch);
try {
return await writeBenchmarkToGitHubPagesWithRetry(bench, config, 10);
} finally {
Expand Down
2 changes: 2 additions & 0 deletions test/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe('configFromJobInput()', function() {
name: string;
tool: string;
ghPagesBranch: string;
ghRepository: string | undefined;
githubToken: string | undefined;
autoPush: boolean;
skipFetchGhPages: boolean;
Expand All @@ -194,6 +195,7 @@ describe('configFromJobInput()', function() {
name: 'Benchmark',
tool: 'cargo',
ghPagesBranch: 'gh-pages',
ghRepository: undefined,
autoPush: false,
skipFetchGhPages: false,
githubToken: undefined,
Expand Down
2 changes: 2 additions & 0 deletions test/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ describe('writeBenchmark()', function() {
tool: 'cargo',
outputFilePath: 'dummy', // Should not affect
ghPagesBranch: 'dummy', // Should not affect
ghRepository: undefined,
benchmarkDataDirPath: 'dummy', // Should not affect
githubToken: undefined,
autoPush: false,
Expand Down Expand Up @@ -892,6 +893,7 @@ describe('writeBenchmark()', function() {
tool: 'cargo',
outputFilePath: 'dummy', // Should not affect
ghPagesBranch: 'gh-pages',
ghRepository: undefined,
benchmarkDataDirPath: 'data-dir', // Should not affect
githubToken: 'dummy token',
autoPush: true,
Expand Down

0 comments on commit 68a2623

Please sign in to comment.