diff --git a/.gitignore b/.gitignore index a128c8b..2d9c420 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ /coverage /lib .eslintcache -.work diff --git a/README.ja.md b/README.ja.md index fae0308..94008ed 100644 --- a/README.ja.md +++ b/README.ja.md @@ -88,7 +88,8 @@ ### オプション #### branch -ビルドに使用するブランチ名 +ビルドに使用するブランチ名 +このオプションが指定されていない場合、現在のソースコードが使用されます。 例: ``` diff --git a/README.md b/README.md index e008117..be31f01 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ e.g. ### Option #### branch Branch name to use for build. +If this option is not specified, the current source code will be used. e.g. ``` diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index c61b08f..91d1c5d 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -64,16 +64,14 @@ describe('execute', () => { await execute(); execCalledWith(mockExec, [ + `rm -rdf ${cwd}/__tests__/tmp/.work/build ${cwd}/__tests__/tmp/.work/push`, 'git init \'.\'', 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', 'git fetch --no-tags origin \'refs/heads/gh-actions:refs/remotes/origin/gh-actions\' || :', 'git checkout -b gh-actions origin/gh-actions || :', 'git init \'.\'', 'git checkout --orphan gh-actions', - 'git init \'.\'', - 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', - 'git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\' || :', - 'git checkout -qf FETCH_HEAD', + `rsync -ac -C '--filter=:- .gitignore' --exclude '.git' --exclude '.work' --exclude '.github' --delete './' '${cwd}/__tests__/tmp/.work/build'`, 'rm -rdf node_modules', 'npm install --production', `mv -f '${cwd}/__tests__/tmp/.work/build/action.yaml' '${cwd}/__tests__/tmp/.work/push/action.yml' > /dev/null 2>&1 || :`, @@ -108,6 +106,8 @@ describe('execute', () => { 'git push --tags \'https://test-owner:token@github.com/test-owner/test-repo.git\' \'gh-actions:refs/heads/gh-actions\' > /dev/null 2>&1 || :', ]); stdoutCalledWith(mockStdout, [ + '[command]rm -rdf ', + ' >> stdout', '::group::Fetching...', '[command]git init \'.\'', ' >> stdout', @@ -127,13 +127,8 @@ describe('execute', () => { '[command]git checkout --orphan gh-actions', ' >> stdout', '::endgroup::', - '::group::Cloning the remote repo for build...', - '[command]git init \'.\'', - ' >> stdout', - '[command]git remote add origin', - '[command]git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\'', - ' >> stdout', - '[command]git checkout -qf FETCH_HEAD', + '::group::Copying current source to build directory...', + '[command]rsync -ac -C \'--filter=:- .gitignore\' --exclude \'.git\' --exclude \'.work\' --exclude \'.github\' --delete \'./\' \'\'', ' >> stdout', '::endgroup::', '::group::Running build for release...', @@ -206,6 +201,8 @@ describe('execute', () => { 'token', '-t', 'test/v1.2.3', + '-b', + 'release/v1.2.3', '-p', '__tests__/fixtures/test11', '-w', @@ -216,6 +213,7 @@ describe('execute', () => { await execute(); execCalledWith(mockExec, [ + `rm -rdf ${cwd}/__tests__/tmp/.work/build ${cwd}/__tests__/tmp/.work/push`, 'git init \'.\'', 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', 'git fetch --no-tags origin \'refs/heads/gh-actions:refs/remotes/origin/gh-actions\' || :', @@ -224,7 +222,7 @@ describe('execute', () => { 'git checkout --orphan gh-actions', 'git init \'.\'', 'git remote add origin \'https://test-owner:token@github.com/test-owner/test-repo.git\' > /dev/null 2>&1 || :', - 'git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\' || :', + 'git fetch --no-tags origin \'refs/heads/release/v1.2.3:refs/remotes/origin/release/v1.2.3\' || :', 'git checkout -qf FETCH_HEAD', 'rm -rdf node_modules', 'npm install --production', @@ -247,6 +245,8 @@ describe('execute', () => { 'git show \'--stat-count=10\' HEAD', ]); stdoutCalledWith(mockStdout, [ + '[command]rm -rdf ', + ' >> stdout', '::group::Fetching...', '[command]git init \'.\'', ' >> stdout', @@ -270,7 +270,7 @@ describe('execute', () => { '[command]git init \'.\'', ' >> stdout', '[command]git remote add origin', - '[command]git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\'', + '[command]git fetch --no-tags origin \'refs/heads/release/v1.2.3:refs/remotes/origin/release/v1.2.3\'', ' >> stdout', '[command]git checkout -qf FETCH_HEAD', ' >> stdout', diff --git a/package.json b/package.json index 2388f2c..8e5d3aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/release-github-actions-cli", - "version": "1.0.1", + "version": "1.1.0", "description": "Release GitHub Actions Cli", "author": { "name": "Technote", diff --git a/src/config.ts b/src/config.ts index 1c32ab7..d23db94 100644 --- a/src/config.ts +++ b/src/config.ts @@ -12,10 +12,9 @@ export const normalizeConfigKeys = (config: { [key: string]: string }): { [key: }))); /* istanbul ignore next */ -const getActionSettingFile = (): string => - existsSync(resolve(__dirname, '../../release-github-actions/action.yml')) ? - resolve(__dirname, '../../release-github-actions/action.yml') : - resolve(__dirname, '../node_modules/@technote-space/release-github-actions/action.yml'); +const getActionSettingFile = (): string => existsSync(resolve(__dirname, '../../release-github-actions/action.yml')) ? + resolve(__dirname, '../../release-github-actions/action.yml') : + resolve(__dirname, '../node_modules/@technote-space/release-github-actions/action.yml'); export const getActionDefaultInputs = (): { [key: string]: string } => { const actionSetting = yaml.safeLoad(readFileSync(getActionSettingFile(), 'utf8')); @@ -35,6 +34,11 @@ export const getConfig = (dir: string): Config | never => { delete config.OWNER; } + if ('REPO' in config) { + config.repo = config.REPO; + delete config.REPO; + } + if (!('owner' in config)) { config.owner = owner; } @@ -42,11 +46,6 @@ export const getConfig = (dir: string): Config | never => { if (!('repo' in config)) { config.repo = repo; } - - if ('REPO' in config) { - config.repo = config.REPO; - delete config.REPO; - } } return {...config, inputs: {...getActionDefaultInputs(), ...normalizeConfigKeys(config.inputs ?? config.INPUTS ?? {})}}; diff --git a/src/env.ts b/src/env.ts index b176f04..47d6464 100644 --- a/src/env.ts +++ b/src/env.ts @@ -4,7 +4,6 @@ import { Config } from './types'; export const setEnv = (config: Config, token: string, workspace: string): void => { process.env.INPUT_GITHUB_TOKEN = token; process.env.GITHUB_ACTOR = config.owner; - // eslint-disable-next-line no-magic-numbers process.env.GITHUB_WORKSPACE = resolve(process.cwd(), workspace); if (config.inputs) { diff --git a/src/index.ts b/src/index.ts index fd46423..d63f36e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export const execute = async(): Promise => { commander .requiredOption('--token ', 'token') .requiredOption('-t, --tag ', 'tag name') - .option('-b, --branch [branch]', 'branch name', 'master') + .option('-b, --branch [branch]', 'branch name') .option('-w, --workspace [workspace]', 'working directory name', '.') .option('-p, --package [package]', 'package file directory name', process.cwd()) .option('-n, --dry-run', 'show what would have been pushed') diff --git a/src/misc.ts b/src/misc.ts index 9d9f4e9..64726d7 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -1,5 +1,6 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; +import { Context } from '@actions/github/lib/context'; import { GitHelper, Logger } from '@technote-space/github-action-helper'; import { Config, ContextArgs } from './types'; @@ -21,9 +22,9 @@ export const getRepository = (dir: string): { owner: string; repo: string } | ne }; }; -export const getContextArgs = (tagName: string, branch: string, config: Config): ContextArgs => ({...config, tagName, branch}); +export const getContextArgs = (tagName: string, branch: string | undefined, config: Config): ContextArgs => ({...config, tagName, branch}); -export const getContext = (args: ContextArgs): object => ({ +export const getContext = (args: ContextArgs): Context => ({ payload: { action: 'published', release: { @@ -31,7 +32,7 @@ export const getContext = (args: ContextArgs): object => ({ }, }, eventName: 'release', - ref: `refs/heads/${args.branch}`, + ref: `refs/heads/${args.branch || 'master'}`, sha: 'FETCH_HEAD', workflow: '', action: '', diff --git a/src/types.ts b/src/types.ts index 18828e5..b196653 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,7 +2,7 @@ export type ContextArgs = Readonly<{ owner: string; repo: string; tagName: string; - branch: string; + branch?: string; }>; export type Config = Readonly<{ diff --git a/src/wrapper.ts b/src/wrapper.ts index c3f2525..07fbe00 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -1,4 +1,6 @@ -import { GitHelper } from '@technote-space/github-action-helper'; +import { mkdirSync } from 'fs'; +import { Context } from '@actions/github/lib/context'; +import { GitHelper, Logger, Command } from '@technote-space/github-action-helper'; import * as command from '@technote-space/release-github-actions/lib/utils/command'; import * as misc from '@technote-space/release-github-actions/lib/utils/misc'; import { getContext } from './misc'; @@ -6,7 +8,51 @@ import { ContextArgs } from './types'; export const isValidContext = (args: ContextArgs): boolean => misc.isValidContext(getContext(args)); -export const prepare = async(helper: GitHelper, args: ContextArgs): Promise => command.prepareCommit(helper, getContext(args)); +export const prepareFiles = async(logger: Logger, com: Command, helper: GitHelper, args: ContextArgs, context: Context): Promise => { + const {buildDir, pushDir} = misc.getParams(); + mkdirSync(buildDir, {recursive: true}); + + if (args.branch) { + logger.startProcess('Cloning the remote repo for build...'); + await helper.checkout(buildDir, context); + } else { + logger.startProcess('Copying current source to build directory...'); + await com.execAsync({ + command: 'rsync', + args: [ + '-ac', + '-C', + '--filter=:- .gitignore', + '--exclude', + '.git', + '--exclude', + '.work', + '--exclude', + '.github', + '--delete', + './', + buildDir, + ], + }); + } + + logger.startProcess('Running build for release...'); + await helper.runCommand(buildDir, misc.getBuildCommands(buildDir, pushDir)); +}; + +export const prepare = async(helper: GitHelper, args: ContextArgs): Promise => { + const context = getContext(args); + const {buildDir, pushDir} = misc.getParams(); + const logger = new Logger(command.replaceDirectory); + const com = new Command(logger); + + await com.execAsync({command: `rm -rdf ${buildDir} ${pushDir}`}); + await command.clone(helper, context); + await command.checkBranch(await helper.getCurrentBranchName(misc.getParams().pushDir), helper); + await prepareFiles(logger, com, helper, args, context); + await command.createBuildInfoFile(context); + await command.copyFiles(); +}; export const commit = async(helper: GitHelper): Promise => { await command.config(helper);