Skip to content

Commit

Permalink
Support a custom working directory (#21)
Browse files Browse the repository at this point in the history
* Support a custom working directory

This change allows users to optionally specify
which directory has the git repository.

In rare cases, the directory containing the
desired repository does not exist within
the github workspace on a runner. When this
occurs, this action is no longer useable.

The `working-directory` option within a workflow
is only supported for `run` steps and not `uses`:
https://stackoverflow.com/questions/67299058/running-github-actions-for-uses-in-another-directory

* [SQUASH] Fix nonasync func

Co-authored-by: Federico Grandi <fgrandi30@gmail.com>

* chore: update build

* chore: make husky hook executable

* fix: fix argument order in `GitArgs` instancing

---------

Co-authored-by: Federico Grandi <fgrandi30@gmail.com>
  • Loading branch information
qubitz and EndBug committed Sep 10, 2023
1 parent c57966a commit 9cdefcf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
description: 'Force-update a branch rather than a tag'
required: false
default: 'false'
git-directory:
description: 'Directory to use when executing git commands'
required: false
default: '${{ github.workspace }}'

runs:
using: 'node16'
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

56 changes: 37 additions & 19 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,73 @@ import * as core from '@actions/core'
import * as util from 'util'
import * as child_process from 'child_process'

const { GITHUB_ACTOR } = process.env
class GitArgs {
readonly command: string

constructor(readonly ref: string, readonly directory: string) {
this.command = `git -C ${this.directory}`
}
}

async function exec(command: string) {
const { stdout, stderr } = await util.promisify(child_process.exec)(command)
if (stderr) console.error(stderr)
return stdout
}

function annotatedTag(message: string, ref: string) {
function annotatedTag(message: string, git: GitArgs) {
core.info('Creating annotated tag...')
return exec(`git tag -a -f -m "${message}" ${ref}`)
return exec(`${git.command} tag -a -f -m "${message}" ${git.ref}`)
}

function lightweightTag(ref: string) {
function lightweightTag(git: GitArgs) {
core.info('Creating lightweight tag...')
return exec(`git tag -f ${ref}`)
return exec(`${git.command} tag -f ${git.ref}`)
}

function forceBranch(ref: string) {
function forceBranch(git: GitArgs) {
core.info('Updating branch...')
return exec(`git branch -f ${ref}`)
return exec(`${git.command} branch -f ${git.ref}`)
}

async function setupUser(git: GitArgs) {
core.info('Setting up git user...')

const { GITHUB_ACTOR } = process.env

await exec(`${git.command} config user.name "${GITHUB_ACTOR}"`)
await exec(
`${git.command} config user.email "${GITHUB_ACTOR}@users.noreply.github.com"`
)
}

async function run() {
try {
core.info('Setting up git user...')
await exec(`git config user.name "${GITHUB_ACTOR}"`)
await exec(
`git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"`
const git = new GitArgs(
core.getInput('ref') || core.getInput('tag-name') || 'latest',
core.getInput('git-directory')
)

const message = core.getInput('description')
const ref = core.getInput('ref') || core.getInput('tag-name') || 'latest'
core.info(`Using '${ref}' as tag name.`)

const branch = core.getBooleanInput('force-branch', { required: true })
const message = core.getInput('description')

if (branch && message)
core.warning(
"You can't set a message when updating a branch, the message will be ignored."
)

if (branch) await forceBranch(ref)
else if (message) await annotatedTag(message, ref)
else await lightweightTag(ref)
core.info(`Running git commands within ${git.directory}`)
core.info(`Using '${git.ref}' as tag name.`)

setupUser(git)

if (branch) await forceBranch(git)
else if (message) await annotatedTag(message, git)
else await lightweightTag(git)

if (branch) core.info('Force-pushing updated branch to repo...')
else core.info('Pushing updated tag to repo...')
return await exec(`git push --force origin ${ref}`)
return await exec(`${git.command} push --force origin ${git.ref}`)
} catch (error) {
core.setFailed(error instanceof Error ? error.message : error)
}
Expand Down

0 comments on commit 9cdefcf

Please sign in to comment.