ghaction-github-pages/src/main.ts

62 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-10-11 07:41:16 +08:00
import * as child_process from 'child_process';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as fs from 'fs';
2019-10-11 07:41:16 +08:00
async function run() {
try {
2019-11-06 10:02:59 +08:00
const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
2019-10-11 07:41:16 +08:00
const target_branch = core.getInput('target_branch') || 'gh-pages';
const build_dir = core.getInput('build_dir', {required: true});
2019-11-06 10:02:59 +08:00
const comitter_name = core.getInput('comitter_name') || process.env['GITHUB_ACTOR'] || 'github-actions';
const comitter_email = core.getInput('comitter_email') || `${comitter_name}@users.noreply.github.com`;
const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages';
2019-10-11 07:41:16 +08:00
if (!fs.existsSync(build_dir)) {
core.setFailed('⛔️ Build dir does not exist');
return;
}
2019-11-06 10:02:59 +08:00
core.info(`🏃 Deploying ${build_dir} directory to ${target_branch} branch on ${repo} repo`);
2019-10-11 07:41:16 +08:00
process.chdir(build_dir);
await exec.exec('git', ['init']);
2019-11-06 10:02:59 +08:00
await exec.exec('git', ['config', 'user.name', comitter_name]);
await exec.exec('git', ['config', 'user.email', comitter_email]);
2019-10-11 07:41:16 +08:00
try {
child_process.execSync('git status --porcelain').toString();
} catch (err) {
core.info('⚠Nothing to deploy');
return;
}
await exec.exec('git', ['add', '.']);
2019-11-06 10:02:59 +08:00
await exec.exec('git', ['commit', '--allow-empty', '-m', commit_message]);
2019-10-11 07:41:16 +08:00
let gitURL = String('https://');
if (process.env['GITHUB_PAT']) {
2019-10-11 07:46:14 +08:00
gitURL = gitURL.concat(process.env['GITHUB_PAT']);
2019-10-11 07:41:16 +08:00
} else if (process.env['GITHUB_TOKEN']) {
2019-10-11 07:46:14 +08:00
gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']);
2019-10-11 07:41:16 +08:00
} else {
core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT');
return;
}
await exec.exec('git', [
'push',
'--force',
'--quiet',
2019-10-11 07:46:14 +08:00
gitURL.concat('@github.com/', repo, '.git'),
2019-10-11 07:41:16 +08:00
`master:${target_branch}`
]);
process.chdir(process.env['GITHUB_WORKSPACE'] || '.');
2019-10-11 07:47:41 +08:00
core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`);
2019-10-11 07:41:16 +08:00
} catch (error) {
core.setFailed(error.message);
}
}
run();