Add dry-run input (#144)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
1424fdc8bb
commit
d3e101b22a
5
.github/workflows/ci.yml
vendored
5
.github/workflows/ci.yml
vendored
|
@ -7,6 +7,10 @@ on:
|
||||||
branches:
|
branches:
|
||||||
- 'dev'
|
- 'dev'
|
||||||
- 'releases/v*'
|
- 'releases/v*'
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- 'dev'
|
||||||
|
- 'releases/v*'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
ci:
|
ci:
|
||||||
|
@ -92,5 +96,6 @@ jobs:
|
||||||
target_branch: ${{ matrix.target_branch }}
|
target_branch: ${{ matrix.target_branch }}
|
||||||
keep_history: ${{ matrix.keep_history }}
|
keep_history: ${{ matrix.keep_history }}
|
||||||
build_dir: public
|
build_dir: public
|
||||||
|
dry-run: ${{ github.event_name == 'pull_request' }}
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
|
@ -138,6 +138,7 @@ Following inputs can be used as `step.with` keys
|
||||||
| `commit_message` | String | Commit message (default `Deploy to GitHub pages`) |
|
| `commit_message` | String | Commit message (default `Deploy to GitHub pages`) |
|
||||||
| `fqdn` | String | Write the given domain name to the CNAME file |
|
| `fqdn` | String | Write the given domain name to the CNAME file |
|
||||||
| `jekyll` | Bool | Allow Jekyll to build your site (default `true`) |
|
| `jekyll` | Bool | Allow Jekyll to build your site (default `true`) |
|
||||||
|
| `dry-run` | Bool | If enabled, nothing will be pushed (default `false`) |
|
||||||
|
|
||||||
### environment variables
|
### environment variables
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,10 @@ inputs:
|
||||||
description: 'Allow Jekyll to build your site'
|
description: 'Allow Jekyll to build your site'
|
||||||
default: 'true'
|
default: 'true'
|
||||||
required: false
|
required: false
|
||||||
|
dry-run:
|
||||||
|
description: 'If enabled, nothing will be pushed'
|
||||||
|
default: 'false'
|
||||||
|
required: false
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: 'node12'
|
using: 'node12'
|
||||||
|
|
119
dist/index.js
generated
vendored
119
dist/index.js
generated
vendored
|
@ -100,72 +100,74 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||||
exports.push = exports.showStat = exports.commit = exports.add = exports.setConfig = exports.hasChanges = exports.isDirty = exports.checkout = exports.init = exports.clone = exports.remoteBranchExists = exports.defaults = void 0;
|
exports.push = exports.showStat = exports.commit = exports.add = exports.setConfig = exports.hasChanges = exports.isDirty = exports.checkout = exports.init = exports.clone = exports.remoteBranchExists = exports.defaults = void 0;
|
||||||
const exec = __importStar(__webpack_require__(7757));
|
const mexec = __importStar(__webpack_require__(7757));
|
||||||
|
const exec = __importStar(__webpack_require__(1514));
|
||||||
exports.defaults = {
|
exports.defaults = {
|
||||||
targetBranch: 'gh-pages',
|
targetBranch: 'gh-pages',
|
||||||
committer: 'GitHub <noreply@github.com>',
|
committer: 'GitHub <noreply@github.com>',
|
||||||
author: 'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>',
|
author: 'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>',
|
||||||
message: 'Deploy to GitHub pages'
|
message: 'Deploy to GitHub pages'
|
||||||
};
|
};
|
||||||
const git = (args = []) => __awaiter(void 0, void 0, void 0, function* () {
|
|
||||||
return yield exec.exec(`git`, args, true).then(res => {
|
|
||||||
if (res.stderr != '' && !res.success) {
|
|
||||||
throw new Error(res.stderr);
|
|
||||||
}
|
|
||||||
return res.stdout.trim();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
function remoteBranchExists(remoteURL, branch) {
|
function remoteBranchExists(remoteURL, branch) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
return yield git(['ls-remote', '--heads', remoteURL, branch]).then(output => {
|
return yield mexec.exec('git', ['ls-remote', '--heads', remoteURL, branch], true).then(res => {
|
||||||
return output.trim().length > 0;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim().length > 0;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.remoteBranchExists = remoteBranchExists;
|
exports.remoteBranchExists = remoteBranchExists;
|
||||||
function clone(remoteURL, branch, dest) {
|
function clone(remoteURL, branch, dest) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield git(['clone', '--quiet', '--branch', branch, '--depth', '1', remoteURL, dest]);
|
yield exec.exec('git', ['clone', '--quiet', '--branch', branch, '--depth', '1', remoteURL, dest]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.clone = clone;
|
exports.clone = clone;
|
||||||
function init(dest) {
|
function init(dest) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield git(['init', dest]);
|
yield exec.exec('git', ['init', dest]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.init = init;
|
exports.init = init;
|
||||||
function checkout(branch) {
|
function checkout(branch) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield git(['checkout', '--orphan', branch]);
|
yield exec.exec('git', ['checkout', '--orphan', branch]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.checkout = checkout;
|
exports.checkout = checkout;
|
||||||
function isDirty() {
|
function isDirty() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
return yield git(['status', '--short']).then(output => {
|
return yield mexec.exec('git', ['status', '--short'], true).then(res => {
|
||||||
return output.trim().length > 0;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim().length > 0;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.isDirty = isDirty;
|
exports.isDirty = isDirty;
|
||||||
function hasChanges() {
|
function hasChanges() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
return yield git(['status', '--porcelain']).then(output => {
|
return yield mexec.exec('git', ['status', '--porcelain'], true).then(res => {
|
||||||
return output.trim().length > 0;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim().length > 0;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.hasChanges = hasChanges;
|
exports.hasChanges = hasChanges;
|
||||||
function setConfig(key, value) {
|
function setConfig(key, value) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield git(['config', key, value]);
|
yield exec.exec('git', ['config', key, value]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.setConfig = setConfig;
|
exports.setConfig = setConfig;
|
||||||
function add(pattern) {
|
function add(pattern) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
yield git(['add', '--all', pattern]);
|
yield exec.exec('git', ['add', '--all', pattern]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.add = add;
|
exports.add = add;
|
||||||
|
@ -180,14 +182,17 @@ function commit(allowEmptyCommit, author, message) {
|
||||||
args.push('--author', author);
|
args.push('--author', author);
|
||||||
}
|
}
|
||||||
args.push('--message', message);
|
args.push('--message', message);
|
||||||
yield git(args);
|
yield exec.exec('git', args);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.commit = commit;
|
exports.commit = commit;
|
||||||
function showStat() {
|
function showStat() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
return yield git(['show', `--stat-count=2000`, 'HEAD']).then(output => {
|
return yield mexec.exec('git', ['show', `--stat-count=2000`, 'HEAD'], true).then(res => {
|
||||||
return output;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -200,7 +205,7 @@ function push(remoteURL, branch, force) {
|
||||||
args.push('--force');
|
args.push('--force');
|
||||||
}
|
}
|
||||||
args.push(remoteURL, branch);
|
args.push(remoteURL, branch);
|
||||||
yield git(args);
|
yield exec.exec('git', args);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.push = push;
|
exports.push = push;
|
||||||
|
@ -266,6 +271,7 @@ function run() {
|
||||||
const commitMessage = core.getInput('commit_message') || git.defaults.message;
|
const commitMessage = core.getInput('commit_message') || git.defaults.message;
|
||||||
const fqdn = core.getInput('fqdn');
|
const fqdn = core.getInput('fqdn');
|
||||||
const nojekyll = /false/i.test(core.getInput('jekyll'));
|
const nojekyll = /false/i.test(core.getInput('jekyll'));
|
||||||
|
const dryRun = /true/i.test(core.getInput('dry-run'));
|
||||||
if (!fs.existsSync(buildDir)) {
|
if (!fs.existsSync(buildDir)) {
|
||||||
core.setFailed('Build dir does not exist');
|
core.setFailed('Build dir does not exist');
|
||||||
return;
|
return;
|
||||||
|
@ -273,17 +279,17 @@ function run() {
|
||||||
let remoteURL = String('https://');
|
let remoteURL = String('https://');
|
||||||
if (process.env['GH_PAT']) {
|
if (process.env['GH_PAT']) {
|
||||||
core.debug(`Use GH_PAT`);
|
core.debug(`Use GH_PAT`);
|
||||||
remoteURL = remoteURL.concat(process.env['GH_PAT'].trim());
|
remoteURL = remoteURL.concat(process.env['GH_PAT'].trim(), '@');
|
||||||
}
|
}
|
||||||
else if (process.env['GITHUB_TOKEN']) {
|
else if (process.env['GITHUB_TOKEN']) {
|
||||||
core.debug(`Use GITHUB_TOKEN`);
|
core.debug(`Use GITHUB_TOKEN`);
|
||||||
remoteURL = remoteURL.concat('x-access-token:', process.env['GITHUB_TOKEN'].trim());
|
remoteURL = remoteURL.concat('x-access-token:', process.env['GITHUB_TOKEN'].trim(), '@');
|
||||||
}
|
}
|
||||||
else {
|
else if (!dryRun) {
|
||||||
core.setFailed('You have to provide a GITHUB_TOKEN or GH_PAT');
|
core.setFailed('You have to provide a GITHUB_TOKEN or GH_PAT');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
remoteURL = remoteURL.concat('@', domain, '/', repo, '.git');
|
remoteURL = remoteURL.concat(domain, '/', repo, '.git');
|
||||||
core.debug(`remoteURL=${remoteURL}`);
|
core.debug(`remoteURL=${remoteURL}`);
|
||||||
const remoteBranchExists = yield git.remoteBranchExists(remoteURL, targetBranch);
|
const remoteBranchExists = yield git.remoteBranchExists(remoteURL, targetBranch);
|
||||||
core.debug(`remoteBranchExists=${remoteBranchExists}`);
|
core.debug(`remoteBranchExists=${remoteBranchExists}`);
|
||||||
|
@ -293,61 +299,72 @@ function run() {
|
||||||
core.debug(`currentdir=${currentdir}`);
|
core.debug(`currentdir=${currentdir}`);
|
||||||
process.chdir(tmpdir);
|
process.chdir(tmpdir);
|
||||||
if (keepHistory && remoteBranchExists) {
|
if (keepHistory && remoteBranchExists) {
|
||||||
core.info(`🌀 Cloning ${repo}`);
|
core.startGroup(`Cloning ${repo}`);
|
||||||
yield git.clone(remoteURL, targetBranch, '.');
|
yield git.clone(remoteURL, targetBranch, '.');
|
||||||
|
core.endGroup();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
core.info(`✨ Initializing local git repo`);
|
core.startGroup(`Initializing local git repo`);
|
||||||
yield git.init('.');
|
yield git.init('.');
|
||||||
yield git.checkout(targetBranch);
|
yield git.checkout(targetBranch);
|
||||||
|
core.endGroup();
|
||||||
}
|
}
|
||||||
core.info(`🏃 Copying ${path.join(currentdir, buildDir)} contents to ${tmpdir}`);
|
yield core.group(`Copying ${path.join(currentdir, buildDir)} to ${tmpdir}`, () => __awaiter(this, void 0, void 0, function* () {
|
||||||
yield fs_extra_1.copySync(path.join(currentdir, buildDir), tmpdir, {
|
yield fs_extra_1.copy(path.join(currentdir, buildDir), tmpdir, {
|
||||||
overwrite: true,
|
filter: (src, dest) => {
|
||||||
errorOnExist: false,
|
core.info(`${src} => ${dest}`);
|
||||||
dereference: true
|
return true;
|
||||||
});
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
core.error(error);
|
||||||
|
});
|
||||||
|
}));
|
||||||
if (fqdn) {
|
if (fqdn) {
|
||||||
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(tmpdir, 'CNAME')}`);
|
core.info(`Writing ${fqdn} domain name to ${path.join(tmpdir, 'CNAME')}`);
|
||||||
yield fs.writeFileSync(path.join(tmpdir, 'CNAME'), fqdn.trim());
|
yield fs.writeFileSync(path.join(tmpdir, 'CNAME'), fqdn.trim());
|
||||||
}
|
}
|
||||||
if (nojekyll) {
|
if (nojekyll) {
|
||||||
core.info(`🚫 Disabling Jekyll support via ${path.join(tmpdir, '.nojekyll')}`);
|
core.info(`Disabling Jekyll support via ${path.join(tmpdir, '.nojekyll')}`);
|
||||||
yield fs.writeFileSync(path.join(tmpdir, '.nojekyll'), '');
|
yield fs.writeFileSync(path.join(tmpdir, '.nojekyll'), '');
|
||||||
}
|
}
|
||||||
const isDirty = yield git.isDirty();
|
const isDirty = yield git.isDirty();
|
||||||
core.debug(`isDirty=${isDirty}`);
|
core.debug(`isDirty=${isDirty}`);
|
||||||
if (keepHistory && remoteBranchExists && !isDirty) {
|
if (keepHistory && remoteBranchExists && !isDirty) {
|
||||||
core.info('⚠️ No changes to commit');
|
core.info('No changes to commit');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const committerPrs = addressparser_1.default(committer)[0];
|
const committerPrs = addressparser_1.default(committer)[0];
|
||||||
core.info(`🔨 Configuring git committer as ${committerPrs.name} <${committerPrs.address}>`);
|
core.startGroup(`Configuring git committer`);
|
||||||
yield git.setConfig('user.name', committerPrs.name);
|
yield git.setConfig('user.name', committerPrs.name);
|
||||||
yield git.setConfig('user.email', committerPrs.address);
|
yield git.setConfig('user.email', committerPrs.address);
|
||||||
|
core.endGroup();
|
||||||
if (!(yield git.hasChanges())) {
|
if (!(yield git.hasChanges())) {
|
||||||
core.info('⚠️ Nothing to deploy');
|
core.info('Nothing to deploy');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
core.info(`📐 Updating index of working tree`);
|
core.startGroup(`Updating index of working tree`);
|
||||||
yield git.add('.');
|
yield git.add('.');
|
||||||
if (allowEmptyCommit) {
|
core.endGroup();
|
||||||
core.debug(`Allow empty commit`);
|
|
||||||
}
|
|
||||||
const authorPrs = addressparser_1.default(author)[0];
|
const authorPrs = addressparser_1.default(author)[0];
|
||||||
yield core.group(`📦 Committing changes as ${authorPrs.name} <${authorPrs.address}> author`, () => __awaiter(this, void 0, void 0, function* () {
|
yield core.group(`Committing changes`, () => __awaiter(this, void 0, void 0, function* () {
|
||||||
yield git.commit(allowEmptyCommit, `${authorPrs.name} <${authorPrs.address}>`, commitMessage);
|
yield git.commit(allowEmptyCommit, `${authorPrs.name} <${authorPrs.address}>`, commitMessage);
|
||||||
yield git.showStat().then(output => {
|
yield git.showStat().then(output => {
|
||||||
core.info(output);
|
core.info(output);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
core.info(`🏃 Pushing ${buildDir} directory to ${targetBranch} branch on ${repo} repo`);
|
if (!dryRun) {
|
||||||
if (!keepHistory) {
|
core.startGroup(`Pushing ${buildDir} directory to ${targetBranch} branch on ${repo} repo`);
|
||||||
core.debug(`Force push`);
|
if (!keepHistory) {
|
||||||
|
core.debug(`Force push`);
|
||||||
|
}
|
||||||
|
yield git.push(remoteURL, targetBranch, !keepHistory);
|
||||||
|
core.endGroup();
|
||||||
|
core.info(`Content of ${buildDir} has been deployed to GitHub Pages!`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
core.warning(`Push disabled (dry run)`);
|
||||||
}
|
}
|
||||||
yield git.push(remoteURL, targetBranch, !keepHistory);
|
|
||||||
process.chdir(currentdir);
|
process.chdir(currentdir);
|
||||||
core.info(`🎉 Content of ${buildDir} has been deployed to GitHub Pages`);
|
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
|
|
50
src/git.ts
50
src/git.ts
|
@ -1,4 +1,5 @@
|
||||||
import * as exec from './exec';
|
import * as mexec from './exec';
|
||||||
|
import * as exec from '@actions/exec';
|
||||||
|
|
||||||
export const defaults = {
|
export const defaults = {
|
||||||
targetBranch: 'gh-pages',
|
targetBranch: 'gh-pages',
|
||||||
|
@ -7,51 +8,51 @@ export const defaults = {
|
||||||
message: 'Deploy to GitHub pages'
|
message: 'Deploy to GitHub pages'
|
||||||
};
|
};
|
||||||
|
|
||||||
const git = async (args: string[] = []): Promise<string> => {
|
export async function remoteBranchExists(remoteURL: string, branch: string): Promise<boolean> {
|
||||||
return await exec.exec(`git`, args, true).then(res => {
|
return await mexec.exec('git', ['ls-remote', '--heads', remoteURL, branch], true).then(res => {
|
||||||
if (res.stderr != '' && !res.success) {
|
if (res.stderr != '' && !res.success) {
|
||||||
throw new Error(res.stderr);
|
throw new Error(res.stderr);
|
||||||
}
|
}
|
||||||
return res.stdout.trim();
|
return res.stdout.trim().length > 0;
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function remoteBranchExists(remoteURL: string, branch: string): Promise<boolean> {
|
|
||||||
return await git(['ls-remote', '--heads', remoteURL, branch]).then(output => {
|
|
||||||
return output.trim().length > 0;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function clone(remoteURL: string, branch: string, dest: string): Promise<void> {
|
export async function clone(remoteURL: string, branch: string, dest: string): Promise<void> {
|
||||||
await git(['clone', '--quiet', '--branch', branch, '--depth', '1', remoteURL, dest]);
|
await exec.exec('git', ['clone', '--quiet', '--branch', branch, '--depth', '1', remoteURL, dest]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function init(dest: string): Promise<void> {
|
export async function init(dest: string): Promise<void> {
|
||||||
await git(['init', dest]);
|
await exec.exec('git', ['init', dest]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkout(branch: string): Promise<void> {
|
export async function checkout(branch: string): Promise<void> {
|
||||||
await git(['checkout', '--orphan', branch]);
|
await exec.exec('git', ['checkout', '--orphan', branch]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isDirty(): Promise<boolean> {
|
export async function isDirty(): Promise<boolean> {
|
||||||
return await git(['status', '--short']).then(output => {
|
return await mexec.exec('git', ['status', '--short'], true).then(res => {
|
||||||
return output.trim().length > 0;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim().length > 0;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function hasChanges(): Promise<boolean> {
|
export async function hasChanges(): Promise<boolean> {
|
||||||
return await git(['status', '--porcelain']).then(output => {
|
return await mexec.exec('git', ['status', '--porcelain'], true).then(res => {
|
||||||
return output.trim().length > 0;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim().length > 0;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setConfig(key: string, value: string): Promise<void> {
|
export async function setConfig(key: string, value: string): Promise<void> {
|
||||||
await git(['config', key, value]);
|
await exec.exec('git', ['config', key, value]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function add(pattern: string): Promise<void> {
|
export async function add(pattern: string): Promise<void> {
|
||||||
await git(['add', '--all', pattern]);
|
await exec.exec('git', ['add', '--all', pattern]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function commit(allowEmptyCommit: boolean, author: string, message: string): Promise<void> {
|
export async function commit(allowEmptyCommit: boolean, author: string, message: string): Promise<void> {
|
||||||
|
@ -64,12 +65,15 @@ export async function commit(allowEmptyCommit: boolean, author: string, message:
|
||||||
args.push('--author', author);
|
args.push('--author', author);
|
||||||
}
|
}
|
||||||
args.push('--message', message);
|
args.push('--message', message);
|
||||||
await git(args);
|
await exec.exec('git', args);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function showStat(): Promise<string> {
|
export async function showStat(): Promise<string> {
|
||||||
return await git(['show', `--stat-count=2000`, 'HEAD']).then(output => {
|
return await mexec.exec('git', ['show', `--stat-count=2000`, 'HEAD'], true).then(res => {
|
||||||
return output;
|
if (res.stderr != '' && !res.success) {
|
||||||
|
throw new Error(res.stderr);
|
||||||
|
}
|
||||||
|
return res.stdout.trim();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,5 +84,5 @@ export async function push(remoteURL: string, branch: string, force: boolean): P
|
||||||
args.push('--force');
|
args.push('--force');
|
||||||
}
|
}
|
||||||
args.push(remoteURL, branch);
|
args.push(remoteURL, branch);
|
||||||
await git(args);
|
await exec.exec('git', args);
|
||||||
}
|
}
|
||||||
|
|
66
src/main.ts
66
src/main.ts
|
@ -1,5 +1,5 @@
|
||||||
import addressparser from 'addressparser';
|
import addressparser from 'addressparser';
|
||||||
import {copySync} from 'fs-extra';
|
import {copy} from 'fs-extra';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
@ -19,6 +19,7 @@ async function run() {
|
||||||
const commitMessage: string = core.getInput('commit_message') || git.defaults.message;
|
const commitMessage: string = core.getInput('commit_message') || git.defaults.message;
|
||||||
const fqdn: string = core.getInput('fqdn');
|
const fqdn: string = core.getInput('fqdn');
|
||||||
const nojekyll: boolean = /false/i.test(core.getInput('jekyll'));
|
const nojekyll: boolean = /false/i.test(core.getInput('jekyll'));
|
||||||
|
const dryRun: boolean = /true/i.test(core.getInput('dry-run'));
|
||||||
|
|
||||||
if (!fs.existsSync(buildDir)) {
|
if (!fs.existsSync(buildDir)) {
|
||||||
core.setFailed('Build dir does not exist');
|
core.setFailed('Build dir does not exist');
|
||||||
|
@ -28,15 +29,15 @@ async function run() {
|
||||||
let remoteURL = String('https://');
|
let remoteURL = String('https://');
|
||||||
if (process.env['GH_PAT']) {
|
if (process.env['GH_PAT']) {
|
||||||
core.debug(`Use GH_PAT`);
|
core.debug(`Use GH_PAT`);
|
||||||
remoteURL = remoteURL.concat(process.env['GH_PAT'].trim());
|
remoteURL = remoteURL.concat(process.env['GH_PAT'].trim(), '@');
|
||||||
} else if (process.env['GITHUB_TOKEN']) {
|
} else if (process.env['GITHUB_TOKEN']) {
|
||||||
core.debug(`Use GITHUB_TOKEN`);
|
core.debug(`Use GITHUB_TOKEN`);
|
||||||
remoteURL = remoteURL.concat('x-access-token:', process.env['GITHUB_TOKEN'].trim());
|
remoteURL = remoteURL.concat('x-access-token:', process.env['GITHUB_TOKEN'].trim(), '@');
|
||||||
} else {
|
} else if (!dryRun) {
|
||||||
core.setFailed('You have to provide a GITHUB_TOKEN or GH_PAT');
|
core.setFailed('You have to provide a GITHUB_TOKEN or GH_PAT');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
remoteURL = remoteURL.concat('@', domain, '/', repo, '.git');
|
remoteURL = remoteURL.concat(domain, '/', repo, '.git');
|
||||||
core.debug(`remoteURL=${remoteURL}`);
|
core.debug(`remoteURL=${remoteURL}`);
|
||||||
|
|
||||||
const remoteBranchExists: boolean = await git.remoteBranchExists(remoteURL, targetBranch);
|
const remoteBranchExists: boolean = await git.remoteBranchExists(remoteURL, targetBranch);
|
||||||
|
@ -49,71 +50,80 @@ async function run() {
|
||||||
process.chdir(tmpdir);
|
process.chdir(tmpdir);
|
||||||
|
|
||||||
if (keepHistory && remoteBranchExists) {
|
if (keepHistory && remoteBranchExists) {
|
||||||
core.info(`🌀 Cloning ${repo}`);
|
core.startGroup(`Cloning ${repo}`);
|
||||||
await git.clone(remoteURL, targetBranch, '.');
|
await git.clone(remoteURL, targetBranch, '.');
|
||||||
|
core.endGroup();
|
||||||
} else {
|
} else {
|
||||||
core.info(`✨ Initializing local git repo`);
|
core.startGroup(`Initializing local git repo`);
|
||||||
await git.init('.');
|
await git.init('.');
|
||||||
await git.checkout(targetBranch);
|
await git.checkout(targetBranch);
|
||||||
|
core.endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
core.info(`🏃 Copying ${path.join(currentdir, buildDir)} contents to ${tmpdir}`);
|
await core.group(`Copying ${path.join(currentdir, buildDir)} to ${tmpdir}`, async () => {
|
||||||
await copySync(path.join(currentdir, buildDir), tmpdir, {
|
await copy(path.join(currentdir, buildDir), tmpdir, {
|
||||||
overwrite: true,
|
filter: (src, dest) => {
|
||||||
errorOnExist: false,
|
core.info(`${src} => ${dest}`);
|
||||||
dereference: true
|
return true;
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
core.error(error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (fqdn) {
|
if (fqdn) {
|
||||||
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(tmpdir, 'CNAME')}`);
|
core.info(`Writing ${fqdn} domain name to ${path.join(tmpdir, 'CNAME')}`);
|
||||||
await fs.writeFileSync(path.join(tmpdir, 'CNAME'), fqdn.trim());
|
await fs.writeFileSync(path.join(tmpdir, 'CNAME'), fqdn.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nojekyll) {
|
if (nojekyll) {
|
||||||
core.info(`🚫 Disabling Jekyll support via ${path.join(tmpdir, '.nojekyll')}`);
|
core.info(`Disabling Jekyll support via ${path.join(tmpdir, '.nojekyll')}`);
|
||||||
await fs.writeFileSync(path.join(tmpdir, '.nojekyll'), '');
|
await fs.writeFileSync(path.join(tmpdir, '.nojekyll'), '');
|
||||||
}
|
}
|
||||||
|
|
||||||
const isDirty: boolean = await git.isDirty();
|
const isDirty: boolean = await git.isDirty();
|
||||||
core.debug(`isDirty=${isDirty}`);
|
core.debug(`isDirty=${isDirty}`);
|
||||||
if (keepHistory && remoteBranchExists && !isDirty) {
|
if (keepHistory && remoteBranchExists && !isDirty) {
|
||||||
core.info('⚠️ No changes to commit');
|
core.info('No changes to commit');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const committerPrs: addressparser.Address = addressparser(committer)[0];
|
const committerPrs: addressparser.Address = addressparser(committer)[0];
|
||||||
core.info(`🔨 Configuring git committer as ${committerPrs.name} <${committerPrs.address}>`);
|
core.startGroup(`Configuring git committer`);
|
||||||
await git.setConfig('user.name', committerPrs.name);
|
await git.setConfig('user.name', committerPrs.name);
|
||||||
await git.setConfig('user.email', committerPrs.address);
|
await git.setConfig('user.email', committerPrs.address);
|
||||||
|
core.endGroup();
|
||||||
|
|
||||||
if (!(await git.hasChanges())) {
|
if (!(await git.hasChanges())) {
|
||||||
core.info('⚠️ Nothing to deploy');
|
core.info('Nothing to deploy');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
core.info(`📐 Updating index of working tree`);
|
core.startGroup(`Updating index of working tree`);
|
||||||
await git.add('.');
|
await git.add('.');
|
||||||
|
core.endGroup();
|
||||||
if (allowEmptyCommit) {
|
|
||||||
core.debug(`Allow empty commit`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const authorPrs: addressparser.Address = addressparser(author)[0];
|
const authorPrs: addressparser.Address = addressparser(author)[0];
|
||||||
await core.group(`📦 Committing changes as ${authorPrs.name} <${authorPrs.address}> author`, async () => {
|
await core.group(`Committing changes`, async () => {
|
||||||
await git.commit(allowEmptyCommit, `${authorPrs.name} <${authorPrs.address}>`, commitMessage);
|
await git.commit(allowEmptyCommit, `${authorPrs.name} <${authorPrs.address}>`, commitMessage);
|
||||||
await git.showStat().then(output => {
|
await git.showStat().then(output => {
|
||||||
core.info(output);
|
core.info(output);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
core.info(`🏃 Pushing ${buildDir} directory to ${targetBranch} branch on ${repo} repo`);
|
if (!dryRun) {
|
||||||
if (!keepHistory) {
|
core.startGroup(`Pushing ${buildDir} directory to ${targetBranch} branch on ${repo} repo`);
|
||||||
core.debug(`Force push`);
|
if (!keepHistory) {
|
||||||
|
core.debug(`Force push`);
|
||||||
|
}
|
||||||
|
await git.push(remoteURL, targetBranch, !keepHistory);
|
||||||
|
core.endGroup();
|
||||||
|
core.info(`Content of ${buildDir} has been deployed to GitHub Pages!`);
|
||||||
|
} else {
|
||||||
|
core.warning(`Push disabled (dry run)`);
|
||||||
}
|
}
|
||||||
await git.push(remoteURL, targetBranch, !keepHistory);
|
|
||||||
|
|
||||||
process.chdir(currentdir);
|
process.chdir(currentdir);
|
||||||
core.info(`🎉 Content of ${buildDir} has been deployed to GitHub Pages`);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message);
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user