Add keep_history, allow_empty_commit and fqdn inputs (#23)

Log latest changes
This commit is contained in:
CrazyMax 2019-11-15 14:40:43 +01:00 committed by GitHub
parent 4ae7970b02
commit 489bbd6a6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 36 deletions

View File

@ -59,22 +59,25 @@ jobs:
Following inputs can be used as `step.with` keys
| Name | Type | Description |
|-----------------|---------|-------------------------------------------------------------------|
|----------------------|---------|-----------------------------------------------------------------------------|
| `repo` | String | GitHub repository where assets will be deployed (default current) |
| `target_branch` | String | Git branch where assets will be deployed (default `gh-pages`) |
| `keep_history` | Bool | Create incremental commit instead of doing push force (default `false`) |
| `allow_empty_commit` | Bool | Git branch where assets will be deployed (default `true`) |
| `build_dir` | String | Build directory to deploy (**required**) |
| `commit_name` | String | Commit author's name (default [GITHUB_ACTOR](https://help.github.com/en/github/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables) or `github-actions`) |
| `commit_email` | String | Commit author's email (default `<committer_name>@users.noreply.github.com`) |
| `commit_message` | String | Commit message (default `Deploy to GitHub pages`) |
| `fqdn` | String | Write the given domain name to the CNAME file |
### environment variables
Following environment variables can be used as `step.env` keys
| Name | Description |
|----------------|--------------------------------------|
|----------------|---------------------------------------|
| `GITHUB_TOKEN` | GITHUB_TOKEN as provided by `secrets` |
| `GITHUB_PAT` | [Personal Access Token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) (see Limitation section below)|
| `GITHUB_PAT` | [Personal Access Token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) (see [Limitation section](#warning-limitation) below)|
## :warning: Limitation

View File

@ -12,6 +12,12 @@ inputs:
target_branch:
description: 'Git branch where assets will be deployed'
default: 'gh-pages'
keep_history:
description: 'Create incremental commit instead of doing push force'
default: 'false'
allow_empty_commit:
description: 'Allow an empty commit to be created'
default: 'true'
build_dir:
description: 'Build directory to deploy'
required: true
@ -21,6 +27,8 @@ inputs:
description: 'Commit author''s email'
commit_message:
description: 'Commit message'
fqdn:
description: 'Write the given domain name to the CNAME file'
runs:
using: 'node12'

View File

@ -20,19 +20,27 @@ const child_process = __importStar(require("child_process"));
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
const target_branch = core.getInput('target_branch') || 'gh-pages';
const keep_history = /true/i.test(core.getInput('keep_history'));
const allow_empty_commit = /true/i.test(core.getInput('allow_empty_commit'));
const build_dir = core.getInput('build_dir', { required: true });
const commit_name = core.getInput('commit_name') || process.env['GITHUB_ACTOR'] || 'github-actions';
const commit_email = core.getInput('commit_email') || `${commit_name}@users.noreply.github.com`;
const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages';
const fqdn = core.getInput('fqdn');
if (!fs.existsSync(build_dir)) {
core.setFailed('⛔️ Build dir does not exist');
return;
}
if (fqdn) {
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(build_dir, 'CNAME')}`);
fs.writeFileSync(path.join(build_dir, 'CNAME'), fqdn.trim());
}
core.info(`🏃 Deploying ${build_dir} directory to ${target_branch} branch on ${repo} repo`);
process.chdir(build_dir);
yield exec.exec('git', ['init']);
@ -46,25 +54,36 @@ function run() {
return;
}
yield exec.exec('git', ['add', '.']);
yield exec.exec('git', ['commit', '--allow-empty', '-m', commit_message]);
let gitCommitCmd = [];
gitCommitCmd.push('commit');
if (allow_empty_commit) {
core.info(`✅ Allow empty commit`);
gitCommitCmd.push('--allow-empty');
}
gitCommitCmd.push('-m', commit_message);
yield exec.exec('git', gitCommitCmd);
yield exec.exec('git', ['show', '--stat-count=10', 'HEAD']);
let gitURL = String('https://');
if (process.env['GITHUB_PAT']) {
core.info(`✅ Use GITHUB_PAT`);
gitURL = gitURL.concat(process.env['GITHUB_PAT']);
}
else if (process.env['GITHUB_TOKEN']) {
core.info(`✅ Use GITHUB_TOKEN`);
gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']);
}
else {
core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT');
return;
}
yield exec.exec('git', [
'push',
'--force',
'--quiet',
gitURL.concat('@github.com/', repo, '.git'),
`master:${target_branch}`
]);
let gitPushCmd = [];
gitPushCmd.push('push', '--quiet');
if (!keep_history) {
core.info(`✅ Force push`);
gitPushCmd.push('--force');
}
gitPushCmd.push(gitURL.concat('@github.com/', repo, '.git'), `${target_branch}:${target_branch}`);
yield exec.exec('git', gitPushCmd);
process.chdir(process.env['GITHUB_WORKSPACE'] || '.');
core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`);
}

View File

@ -2,21 +2,30 @@ import * as child_process from 'child_process';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as fs from 'fs';
import * as path from 'path';
async function run() {
try {
const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
const target_branch = core.getInput('target_branch') || 'gh-pages';
const build_dir = core.getInput('build_dir', {required: true});
const commit_name = core.getInput('commit_name') || process.env['GITHUB_ACTOR'] || 'github-actions';
const commit_email = core.getInput('commit_email') || `${commit_name}@users.noreply.github.com`;
const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages';
const repo: string = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
const target_branch: string = core.getInput('target_branch') || 'gh-pages';
const keep_history: boolean = /true/i.test(core.getInput('keep_history'));
const allow_empty_commit: boolean = /true/i.test(core.getInput('allow_empty_commit'));
const build_dir: string = core.getInput('build_dir', {required: true});
const commit_name: string = core.getInput('commit_name') || process.env['GITHUB_ACTOR'] || 'github-actions';
const commit_email: string = core.getInput('commit_email') || `${commit_name}@users.noreply.github.com`;
const commit_message: string = core.getInput('commit_message') || 'Deploy to GitHub pages';
const fqdn: string = core.getInput('fqdn');
if (!fs.existsSync(build_dir)) {
core.setFailed('⛔️ Build dir does not exist');
return;
}
if (fqdn) {
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(build_dir, 'CNAME')}`);
fs.writeFileSync(path.join(build_dir, 'CNAME'), fqdn.trim());
}
core.info(`🏃 Deploying ${build_dir} directory to ${target_branch} branch on ${repo} repo`);
process.chdir(build_dir);
@ -32,24 +41,38 @@ async function run() {
}
await exec.exec('git', ['add', '.']);
await exec.exec('git', ['commit', '--allow-empty', '-m', commit_message]);
let gitCommitCmd: Array<string> = [];
gitCommitCmd.push('commit');
if (allow_empty_commit) {
core.info(`✅ Allow empty commit`);
gitCommitCmd.push('--allow-empty');
}
gitCommitCmd.push('-m', commit_message);
await exec.exec('git', gitCommitCmd);
await exec.exec('git', ['show', '--stat-count=10', 'HEAD']);
let gitURL = String('https://');
if (process.env['GITHUB_PAT']) {
core.info(`✅ Use GITHUB_PAT`);
gitURL = gitURL.concat(process.env['GITHUB_PAT']);
} else if (process.env['GITHUB_TOKEN']) {
core.info(`✅ Use GITHUB_TOKEN`);
gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']);
} else {
core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT');
return;
}
await exec.exec('git', [
'push',
'--force',
'--quiet',
gitURL.concat('@github.com/', repo, '.git'),
`master:${target_branch}`
]);
let gitPushCmd: Array<string> = [];
gitPushCmd.push('push', '--quiet');
if (!keep_history) {
core.info(`✅ Force push`);
gitPushCmd.push('--force');
}
gitPushCmd.push(gitURL.concat('@github.com/', repo, '.git'), `${target_branch}:${target_branch}`);
await exec.exec('git', gitPushCmd);
process.chdir(process.env['GITHUB_WORKSPACE'] || '.');
core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`);