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

@ -58,23 +58,26 @@ jobs:
Following inputs can be used as `step.with` keys Following inputs can be used as `step.with` keys
| Name | Type | Description | | Name | Type | Description |
|-----------------|---------|-------------------------------------------------------------------| |----------------------|---------|-----------------------------------------------------------------------------|
| `repo` | String | GitHub repository where assets will be deployed (default current) | | `repo` | String | GitHub repository where assets will be deployed (default current) |
| `target_branch` | String | Git branch where assets will be deployed (default `gh-pages`) | | `target_branch` | String | Git branch where assets will be deployed (default `gh-pages`) |
| `build_dir` | String | Build directory to deploy (**required**) | | `keep_history` | Bool | Create incremental commit instead of doing push force (default `false`) |
| `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`) | | `allow_empty_commit` | Bool | Git branch where assets will be deployed (default `true`) |
| `commit_email` | String | Commit author's email (default `<committer_name>@users.noreply.github.com`) | | `build_dir` | String | Build directory to deploy (**required**) |
| `commit_message`| String | Commit message (default `Deploy to GitHub pages`) | | `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 ### environment variables
Following environment variables can be used as `step.env` keys Following environment variables can be used as `step.env` keys
| Name | Description | | Name | Description |
|----------------|--------------------------------------| |----------------|---------------------------------------|
| `GITHUB_TOKEN` | GITHUB_TOKEN as provided by `secrets`| | `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 ## :warning: Limitation

View File

@ -12,6 +12,12 @@ inputs:
target_branch: target_branch:
description: 'Git branch where assets will be deployed' description: 'Git branch where assets will be deployed'
default: 'gh-pages' 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: build_dir:
description: 'Build directory to deploy' description: 'Build directory to deploy'
required: true required: true
@ -21,6 +27,8 @@ inputs:
description: 'Commit author''s email' description: 'Commit author''s email'
commit_message: commit_message:
description: 'Commit message' description: 'Commit message'
fqdn:
description: 'Write the given domain name to the CNAME file'
runs: runs:
using: 'node12' using: 'node12'

View File

@ -20,19 +20,27 @@ const child_process = __importStar(require("child_process"));
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const fs = __importStar(require("fs")); const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || ''; const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
const target_branch = core.getInput('target_branch') || 'gh-pages'; 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 build_dir = core.getInput('build_dir', { required: true });
const commit_name = core.getInput('commit_name') || process.env['GITHUB_ACTOR'] || 'github-actions'; 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_email = core.getInput('commit_email') || `${commit_name}@users.noreply.github.com`;
const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages'; const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages';
const fqdn = core.getInput('fqdn');
if (!fs.existsSync(build_dir)) { if (!fs.existsSync(build_dir)) {
core.setFailed('⛔️ Build dir does not exist'); core.setFailed('⛔️ Build dir does not exist');
return; 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`); core.info(`🏃 Deploying ${build_dir} directory to ${target_branch} branch on ${repo} repo`);
process.chdir(build_dir); process.chdir(build_dir);
yield exec.exec('git', ['init']); yield exec.exec('git', ['init']);
@ -42,29 +50,40 @@ function run() {
child_process.execSync('git status --porcelain').toString(); child_process.execSync('git status --porcelain').toString();
} }
catch (err) { catch (err) {
core.info('⚠️Nothing to deploy'); core.info('⚠️ Nothing to deploy');
return; return;
} }
yield exec.exec('git', ['add', '.']); 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://'); let gitURL = String('https://');
if (process.env['GITHUB_PAT']) { if (process.env['GITHUB_PAT']) {
core.info(`✅ Use GITHUB_PAT`);
gitURL = gitURL.concat(process.env['GITHUB_PAT']); gitURL = gitURL.concat(process.env['GITHUB_PAT']);
} }
else if (process.env['GITHUB_TOKEN']) { else if (process.env['GITHUB_TOKEN']) {
core.info(`✅ Use GITHUB_TOKEN`);
gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']); gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']);
} }
else { else {
core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT'); core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT');
return; return;
} }
yield exec.exec('git', [ let gitPushCmd = [];
'push', gitPushCmd.push('push', '--quiet');
'--force', if (!keep_history) {
'--quiet', core.info(`✅ Force push`);
gitURL.concat('@github.com/', repo, '.git'), gitPushCmd.push('--force');
`master:${target_branch}` }
]); gitPushCmd.push(gitURL.concat('@github.com/', repo, '.git'), `${target_branch}:${target_branch}`);
yield exec.exec('git', gitPushCmd);
process.chdir(process.env['GITHUB_WORKSPACE'] || '.'); process.chdir(process.env['GITHUB_WORKSPACE'] || '.');
core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`); 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 core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
async function run() { async function run() {
try { try {
const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || ''; const repo: string = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
const target_branch = core.getInput('target_branch') || 'gh-pages'; const target_branch: string = core.getInput('target_branch') || 'gh-pages';
const build_dir = core.getInput('build_dir', {required: true}); const keep_history: boolean = /true/i.test(core.getInput('keep_history'));
const commit_name = core.getInput('commit_name') || process.env['GITHUB_ACTOR'] || 'github-actions'; const allow_empty_commit: boolean = /true/i.test(core.getInput('allow_empty_commit'));
const commit_email = core.getInput('commit_email') || `${commit_name}@users.noreply.github.com`; const build_dir: string = core.getInput('build_dir', {required: true});
const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages'; 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)) { if (!fs.existsSync(build_dir)) {
core.setFailed('⛔️ Build dir does not exist'); core.setFailed('⛔️ Build dir does not exist');
return; 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`); core.info(`🏃 Deploying ${build_dir} directory to ${target_branch} branch on ${repo} repo`);
process.chdir(build_dir); process.chdir(build_dir);
@ -27,29 +36,43 @@ async function run() {
try { try {
child_process.execSync('git status --porcelain').toString(); child_process.execSync('git status --porcelain').toString();
} catch (err) { } catch (err) {
core.info('⚠️Nothing to deploy'); core.info('⚠️ Nothing to deploy');
return; return;
} }
await exec.exec('git', ['add', '.']); 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://'); let gitURL = String('https://');
if (process.env['GITHUB_PAT']) { if (process.env['GITHUB_PAT']) {
core.info(`✅ Use GITHUB_PAT`);
gitURL = gitURL.concat(process.env['GITHUB_PAT']); gitURL = gitURL.concat(process.env['GITHUB_PAT']);
} else if (process.env['GITHUB_TOKEN']) { } else if (process.env['GITHUB_TOKEN']) {
core.info(`✅ Use GITHUB_TOKEN`);
gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']); gitURL = gitURL.concat('x-access-token:', process.env['GITHUB_TOKEN']);
} else { } else {
core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT'); core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT');
return; return;
} }
await exec.exec('git', [
'push', let gitPushCmd: Array<string> = [];
'--force', gitPushCmd.push('push', '--quiet');
'--quiet', if (!keep_history) {
gitURL.concat('@github.com/', repo, '.git'), core.info(`✅ Force push`);
`master:${target_branch}` 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'] || '.'); process.chdir(process.env['GITHUB_WORKSPACE'] || '.');
core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`); core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`);