Skip to content

Commit

Permalink
shell option (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix authored Feb 19, 2024
1 parent d48a906 commit fcbc939
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 7 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

permissions:
contents: read

Expand Down Expand Up @@ -50,6 +54,9 @@ jobs:
- name: Checkout
id: checkout
uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Test Local Action
id: test-action
Expand All @@ -61,6 +68,75 @@ jobs:
"${{ runner.temp }}/post.sh"
fi
- name: Test Local Action (login shell)
id: test-longin-shell
uses: ./
with:
shell: bash -l -ex {0}
post-run: |
echo "test" | tee "${{ runner.temp }}/test.txt"
if [ -f "${{ runner.temp }}/post.sh" ]; then
"${{ runner.temp }}/post.sh"
fi
- name: Test Local Action (Python)
id: test-python
uses: ./
with:
shell: python
post-run: |
print("Hello, world!")
- name: Test file
run: |
test ! -f "${{ runner.temp }}/test.txt"
test-action-windows:
name: GitHub Actions Test (Windows)
runs-on: windows-latest

steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Test Local Action
id: test-action
uses: ./
with:
post-run: |
echo "test" | tee "${{ runner.temp }}/test.txt"
if [ -f "${{ runner.temp }}/post.sh" ]; then
"${{ runner.temp }}/post.sh"
fi
- name: Test Local Action (pwsh)
id: test-pwsh
uses: ./
with:
shell: pwsh
post-run: |
Write-Host "Hello World!!"
- name: Test Local Action (cmd)
id: test-cmd
uses: ./
with:
shell: cmd
post-run: |
echo "Hello World!!"
- name: Test Local Action (Python)
id: test-python
uses: ./
with:
shell: python
post-run: |
print("Hello, world!")
- name: Test file
run: |
test ! -f "${{ runner.temp }}/test.txt"
4 changes: 4 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
- main
workflow_dispatch:

permissions:
contents: write
pull-requests: read

jobs:
draft_release:
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ See [action.yml](./action.yml)
- name: Post Action
uses: srz-zumix/post-run-action@main
with:
# custom shell
# Default : bash -e {0}
# bash : bash --noprofile --norc -eo pipefail {0}
# custom : e.g. `bash -l -ex {0}`
# see https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell
shell: base -ex {0}
# post run script text
post-run: |
echo "test" | tee "${{ runner.temp }}/test.txt"
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ inputs:
post-run:
description: 'post run script text'
required: true
shell:
# Default : bash -e {0}
# bash : bash --noprofile --norc -eo pipefail {0}
# custom : e.g. `bash -l -ex {0}`
# see https://docs.github.com/ja/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell
description: 'custom shell command'
default: ""

runs:
using: node16
Expand Down
51 changes: 48 additions & 3 deletions dist/post/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 55 additions & 4 deletions src/post/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,68 @@

import * as core from '@actions/core'
import { promises as fs } from 'fs'
import * as path from 'path'
import * as io from '@actions/io'
import * as exec from '@actions/exec'

async function resolveShell(): Promise<string[]> {
const defaultCommands: { [key: string]: string[] } = {
default: ['bash', '-e', '{0}'],
sh: ['sh', '-e', '{0}'],
bash: ['bash', '--noprofile', '--norc', '-eo', 'pipefail', '{0}'],
cmd: ['cmd', '/D', '/E:ON', '/V:OFF', '/S', '/C', '"CALL "{0}""'],
pwsh: ['pwsh', '-command', ". '{0}'"],
powershell: ['powershell', '-command', ". '{0}'"]
}
const shellCommand = core.getInput('shell', { required: false })
if (!shellCommand) {
return defaultCommands['default']
}

const shellCommands = shellCommand.split(' ')
if (shellCommands.length === 1) {
if (shellCommands[0] in defaultCommands) {
return defaultCommands[shellCommands[0]]
} else {
return [shellCommands[0], '{0}']
}
}
return shellCommands
}

function resolveExtension(command: string): string {
const commandExtensions: { [key: string]: string } = {
python: 'py',
cmd: 'cmd',
pwsh: 'ps1',
powershell: 'ps1'
}
if (command in commandExtensions) {
return commandExtensions[command]
}
return 'sh'
}

async function run(): Promise<void> {
try {
const content = core.getInput('post-run', { required: true })
const content: string = core.getInput('post-run', { required: true })
const shellCommands: string[] = await resolveShell()
const command = shellCommands[0]
const commandPath: string = await io.which(command, true)

const runnerTempPath: string = process.env.RUNNER_TEMP as string
const scriptPath = `${runnerTempPath}/post-run.sh`
const extension: string = resolveExtension(command)
const scriptPath = path.join(runnerTempPath, `post-run.${extension}`)
await fs.writeFile(scriptPath, content)
const bashPath: string = await io.which('bash', true)
await exec.exec(`"${bashPath}"`, [scriptPath])

const commandArgs = shellCommands
.slice(1)
.map(item => item.replace('{0}', scriptPath))

const options: exec.ExecOptions = {}
options.windowsVerbatimArguments = command === 'cmd'

await exec.exec(`"${commandPath}"`, commandArgs, options)
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
Expand Down

0 comments on commit fcbc939

Please sign in to comment.