From 079ec0e1308a93e85966f0172461c9e62ccf0e94 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Sun, 17 Jul 2022 15:33:00 +0200 Subject: [PATCH] Adding tag option to action (#1142) * Adding tag option to action * Avoiding adding tag if the remote name is not used. * Adding unit tests * Update readme. * Update readme part 2 * Adding changes from the code review * removing references to deleted parameters * removing space --- README.md | 4 +++- __tests__/git.test.ts | 22 ++++++++++++++++++++++ src/constants.ts | 5 ++++- src/git.ts | 19 ++++++++++++++++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d5b19ce9..56af86ac 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,8 @@ run({ folder: 'build', repositoryName: 'JamesIves/github-pages-deploy-action', silent: true, - workspace: 'src/project/location' + workspace: 'src/project/location', + tag: 'v0.1' }) ``` @@ -170,6 +171,7 @@ By default, the action does not need any token configuration and uses the provid | `force` | Force-push new deployments to overwrite the previous version; otherwise, attempt to rebase new deployments onto any existing ones. This option is turned on by default and can be toggled off by setting it to `false`, which may be useful if there are multiple deployments in a single branch. | `with` | **No** | | `silent` | Silences the action output preventing it from displaying git messages. | `with` | **No** | | `workspace` | This should point to where your project lives on the virtual machine. The GitHub Actions environment will set this for you. It is only necessary to set this variable if you're using the node module. | `with` | **No** | +| `tag` | Add a tag to the commit. Only works when `dry-run` is not used. | `with` | **No** | With the action correctly configured you should see the workflow trigger the deployment under the configured conditions. diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 8d566b06..5b64456a 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -449,5 +449,27 @@ describe('git', () => { ) } }) + + it('should add a tag to the commit', async () => { + Object.assign(action, { + hostname: 'github.com', + silent: false, + folder: 'assets', + branch: 'branch', + token: '123', + repositoryName: 'JamesIves/montezuma', + tag: 'v0.1', + pusher: { + name: 'asd', + email: 'as@cat' + }, + isTest: TestFlag.NONE + }) + + const response = await deploy(action) + expect(execute).toBeCalledTimes(13) // normally 11 runs, +2 of the tag + expect(response).toBe(Status.SUCCESS) + }) + }) }) diff --git a/src/constants.ts b/src/constants.ts index e0ce8075..85c83122 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -58,6 +58,8 @@ export interface ActionInterface { tokenType?: string /** The folder where your deployment project lives. */ workspace: string + /** GitHub tag name */ + tag?: string | null } /** The minimum required values to run the action as a node module. */ @@ -138,7 +140,8 @@ export const action: ActionInterface = { ? true : getInput('ssh-key'), targetFolder: getInput('target-folder'), - workspace: process.env.GITHUB_WORKSPACE || '' + workspace: process.env.GITHUB_WORKSPACE || '', + tag: getInput('tag') } /** Types for the required action parameters. */ diff --git a/src/git.ts b/src/git.ts index 1bf788bd..3d23ff3c 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,4 +1,4 @@ -import {info} from '@actions/core' +import { info, warning } from '@actions/core' import {mkdirP, rmRF} from '@actions/io' import fs from 'fs' import { @@ -316,6 +316,23 @@ export async function deploy(action: ActionInterface): Promise { info(`Changes committed to the ${action.branch} branch… đŸ“¦`) + if (action.tag) { + info(`Adding a tag '${action.tag}' to the commit`) + await execute( + `git tag ${action.tag}`, + `${action.workspace}/${temporaryDeploymentDirectory}`, + action.silent + ) + info(`Pushing tag '${action.tag}' to repository.`) + await execute( + `git push origin ${action.tag}`, + `${action.workspace}/${temporaryDeploymentDirectory}`, + action.silent + ) + + info(`Tag '${action.tag}' created and pushed to the ${action.branch} branch… đŸ“¦`) + } + return Status.SUCCESS } catch (error) { throw new Error(