From 489bbd6a6b6d5c2865a7d92552002c29aef5c8bf Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 15 Nov 2019 14:40:43 +0100 Subject: [PATCH] Add keep_history, allow_empty_commit and fqdn inputs (#23) Log latest changes --- README.md | 27 +++++++++++++++------------ action.yml | 8 ++++++++ lib/main.js | 37 ++++++++++++++++++++++++++++--------- src/main.ts | 53 ++++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 89 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index ab0ae7a..352d19f 100644 --- a/README.md +++ b/README.md @@ -58,23 +58,26 @@ 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`) | -| `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 `@users.noreply.github.com`) | -| `commit_message`| String | Commit message (default `Deploy to GitHub pages`) | +| 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 `@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)| +| 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](#warning-limitation) below)| ## :warning: Limitation diff --git a/action.yml b/action.yml index b8e0860..5518aaa 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/lib/main.js b/lib/main.js index bb1c51d..cd0daa5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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']); @@ -42,29 +50,40 @@ function run() { child_process.execSync('git status --porcelain').toString(); } catch (err) { - core.info('โš ๏ธNothing to deploy'); + core.info('โš ๏ธ Nothing to deploy'); 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.`); } diff --git a/src/main.ts b/src/main.ts index 7f9d618..6355c6c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); @@ -27,29 +36,43 @@ async function run() { try { child_process.execSync('git status --porcelain').toString(); } catch (err) { - core.info('โš ๏ธNothing to deploy'); + core.info('โš ๏ธ Nothing to deploy'); return; } await exec.exec('git', ['add', '.']); - await exec.exec('git', ['commit', '--allow-empty', '-m', commit_message]); + + let gitCommitCmd: Array = []; + 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 = []; + 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.`);