2020-05-11 20:59:39 +08:00
|
|
|
import addressparser from 'addressparser';
|
2019-11-15 23:20:34 +08:00
|
|
|
import {copySync} from 'fs-extra';
|
2019-11-06 09:46:35 +08:00
|
|
|
import * as fs from 'fs';
|
2019-11-15 22:57:27 +08:00
|
|
|
import * as os from 'os';
|
2019-11-15 21:40:43 +08:00
|
|
|
import * as path from 'path';
|
2020-05-11 20:59:39 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as git from './git';
|
2019-10-11 07:41:16 +08:00
|
|
|
|
|
|
|
async function run() {
|
|
|
|
try {
|
2019-11-15 21:40:43 +08:00
|
|
|
const repo: string = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
|
2020-05-11 20:59:39 +08:00
|
|
|
const targetBranch: string = core.getInput('target_branch') || git.defaults.targetBranch;
|
|
|
|
const keepHistory: boolean = /true/i.test(core.getInput('keep_history'));
|
|
|
|
const allowEmptyCommit: boolean = /true/i.test(core.getInput('allow_empty_commit'));
|
|
|
|
const buildDir: string = core.getInput('build_dir', {required: true});
|
|
|
|
const committer: string = core.getInput('committer') || git.defaults.committer;
|
|
|
|
const author: string = core.getInput('author') || git.defaults.author;
|
|
|
|
const commitMessage: string = core.getInput('commit_message') || git.defaults.message;
|
2019-11-15 21:40:43 +08:00
|
|
|
const fqdn: string = core.getInput('fqdn');
|
2020-06-25 15:38:28 +08:00
|
|
|
const nojekyll: boolean = /false/i.test(core.getInput('jekyll'));
|
2019-10-11 07:41:16 +08:00
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
if (!fs.existsSync(buildDir)) {
|
|
|
|
core.setFailed('Build dir does not exist');
|
2019-10-11 07:41:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
let remoteURL = String('https://');
|
|
|
|
if (process.env['GH_PAT']) {
|
|
|
|
core.info(`✅ Use GH_PAT`);
|
|
|
|
remoteURL = remoteURL.concat(process.env['GH_PAT'].trim());
|
2019-11-15 22:25:50 +08:00
|
|
|
} else if (process.env['GITHUB_TOKEN']) {
|
|
|
|
core.info(`✅ Use GITHUB_TOKEN`);
|
2020-05-11 20:59:39 +08:00
|
|
|
remoteURL = remoteURL.concat('x-access-token:', process.env['GITHUB_TOKEN'].trim());
|
2019-11-15 22:25:50 +08:00
|
|
|
} else {
|
2020-05-11 20:59:39 +08:00
|
|
|
core.setFailed('You have to provide a GITHUB_TOKEN or GH_PAT');
|
2019-11-15 22:25:50 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-05-11 20:59:39 +08:00
|
|
|
remoteURL = remoteURL.concat('@github.com/', repo, '.git');
|
|
|
|
core.debug(`remoteURL=${remoteURL}`);
|
2019-11-15 22:25:50 +08:00
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
const remoteBranchExists: boolean = await git.remoteBranchExists(remoteURL, targetBranch);
|
|
|
|
core.debug(`remoteBranchExists=${remoteBranchExists}`);
|
2019-11-15 22:57:27 +08:00
|
|
|
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-pages-'));
|
2020-05-11 20:59:39 +08:00
|
|
|
core.debug(`tmpdir=${tmpdir}`);
|
2019-11-15 23:29:46 +08:00
|
|
|
const currentdir = path.resolve('.');
|
2020-05-11 20:59:39 +08:00
|
|
|
core.debug(`currentdir=${currentdir}`);
|
|
|
|
|
2019-11-15 22:57:27 +08:00
|
|
|
process.chdir(tmpdir);
|
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
if (keepHistory && remoteBranchExists) {
|
2020-07-20 00:29:36 +08:00
|
|
|
core.info(`🌀 Cloning ${repo}`);
|
2020-05-11 20:59:39 +08:00
|
|
|
await git.clone(remoteURL, targetBranch, '.');
|
2019-11-15 22:25:50 +08:00
|
|
|
} else {
|
2020-05-11 20:59:39 +08:00
|
|
|
core.info(`✨ Initializing local git repo`);
|
|
|
|
await git.init('.');
|
|
|
|
await git.checkout(targetBranch);
|
2019-11-15 22:25:50 +08:00
|
|
|
}
|
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
core.info(`🏃 Copying ${path.join(currentdir, buildDir)} contents to ${tmpdir}`);
|
2020-08-06 21:04:46 +08:00
|
|
|
await copySync(path.join(currentdir, buildDir), tmpdir, {
|
2020-11-12 05:44:04 +08:00
|
|
|
overwrite: true,
|
|
|
|
errorOnExist: false,
|
2020-08-06 21:04:46 +08:00
|
|
|
dereference: true
|
|
|
|
});
|
2019-11-15 23:36:31 +08:00
|
|
|
|
|
|
|
if (fqdn) {
|
2019-11-26 15:31:04 +08:00
|
|
|
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(tmpdir, 'CNAME')}`);
|
2020-05-11 20:59:39 +08:00
|
|
|
await fs.writeFileSync(path.join(tmpdir, 'CNAME'), fqdn.trim());
|
2019-11-15 23:36:31 +08:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:38:28 +08:00
|
|
|
if (nojekyll) {
|
2020-06-25 15:24:31 +08:00
|
|
|
core.info(`🚫 Disabling Jekyll support via ${path.join(tmpdir, '.nojekyll')}`);
|
|
|
|
await fs.writeFileSync(path.join(tmpdir, '.nojekyll'), '');
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
const isDirty: boolean = await git.isDirty();
|
|
|
|
core.debug(`isDirty=${isDirty}`);
|
|
|
|
if (keepHistory && remoteBranchExists && !isDirty) {
|
|
|
|
core.info('⚠️ No changes to commit');
|
2020-02-11 20:38:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
const committerPrs: addressparser.Address = addressparser(committer)[0];
|
|
|
|
core.info(`🔨 Configuring git committer as ${committerPrs.name} <${committerPrs.address}>`);
|
|
|
|
await git.setConfig('user.name', committerPrs.name);
|
|
|
|
await git.setConfig('user.email', committerPrs.address);
|
2019-10-11 07:41:16 +08:00
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
if (!(await git.hasChanges())) {
|
2019-11-15 21:40:43 +08:00
|
|
|
core.info('⚠️ Nothing to deploy');
|
2019-10-11 07:41:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
core.info(`📐 Updating index of working tree`);
|
|
|
|
await git.add('.');
|
2019-11-15 21:40:43 +08:00
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
if (allowEmptyCommit) {
|
2019-11-15 21:40:43 +08:00
|
|
|
core.info(`✅ Allow empty commit`);
|
|
|
|
}
|
2020-11-12 05:44:04 +08:00
|
|
|
|
2020-05-11 20:59:39 +08:00
|
|
|
const authorPrs: addressparser.Address = addressparser(author)[0];
|
2020-11-12 05:44:04 +08:00
|
|
|
core.startGroup(`📦 Committing changes as ${authorPrs.name} <${authorPrs.address}> author`);
|
2020-05-11 20:59:39 +08:00
|
|
|
await git.commit(allowEmptyCommit, `${authorPrs.name} <${authorPrs.address}>`, commitMessage);
|
2020-11-12 05:44:04 +08:00
|
|
|
await git.showStat().then(output => {
|
2020-05-11 20:59:39 +08:00
|
|
|
core.info(output);
|
|
|
|
});
|
2020-11-12 05:44:04 +08:00
|
|
|
core.endGroup();
|
2020-05-11 20:59:39 +08:00
|
|
|
|
|
|
|
core.info(`🏃 Pushing ${buildDir} directory to ${targetBranch} branch on ${repo} repo`);
|
|
|
|
if (!keepHistory) {
|
2019-11-15 21:40:43 +08:00
|
|
|
core.info(`✅ Force push`);
|
|
|
|
}
|
2020-05-11 20:59:39 +08:00
|
|
|
await git.push(remoteURL, targetBranch, !keepHistory);
|
2019-10-11 07:41:16 +08:00
|
|
|
|
2019-11-15 23:29:46 +08:00
|
|
|
process.chdir(currentdir);
|
2020-05-11 21:10:43 +08:00
|
|
|
core.info(`🎉 Content of ${buildDir} has been deployed to GitHub Pages`);
|
2019-10-11 07:41:16 +08:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|