Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a custom working directory #21

Merged
merged 5 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}'
EndBug marked this conversation as resolved.
Show resolved Hide resolved

runs:
using: 'node16'
Expand Down
56 changes: 38 additions & 18 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,75 @@ 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 run() {
try {
function setupUser(git: GitArgs) {
qubitz marked this conversation as resolved.
Show resolved Hide resolved
core.info('Setting up git user...')
await exec(`git config user.name "${GITHUB_ACTOR}"`)

const { GITHUB_ACTOR } = process.env

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

const message = core.getInput('description')
const ref = core.getInput('ref') || core.getInput('tag-name') || 'latest'
core.info(`Using '${ref}' as tag name.`)
async function run() {
try {
const git = new GitArgs(
core.getInput('git-directory'),
core.getInput('ref') || core.getInput('tag-name') || 'latest')

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)
EndBug marked this conversation as resolved.
Show resolved Hide resolved

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