Skip to content

Commit

Permalink
Populate action outputs for path and version
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Kyle Harding <kyle@balena.io>
  • Loading branch information
klutchell committed Nov 1, 2024
1 parent c815794 commit 1f64d40
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ runs:
- name: Print outputs
shell: bash
run: |
echo "Installed path: ${{ steps.setup-oras-cli.outputs.path }}"
echo "Installed version: ${{ steps.setup-oras-cli.outputs.version }}"
- name: Test CLI
shell: bash
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ To use this action in your workflow, add the following step:

### Outputs

- `path`: The path where the ORAS CLI was installed.
- `version`: The ORAS CLI release that was installed.

## Contributing
Expand Down
36 changes: 36 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,42 @@ describe('ORAS setup action', () => {
})
})

describe('execVersion', () => {
it('executes version command', async () => {
const exec = require('@actions/exec')
exec.getExecOutput = jest
.fn()
.mockResolvedValue({ exitCode: 0, stdout: '1.2.3', stderr: '' })

await main.execVersion()

expect(exec.getExecOutput).toHaveBeenCalledWith('oras', ['version'])
})

it('handles version command failure', async () => {
const exec = require('@actions/exec')
// exec.getExecOutput.mockRejectedValue(new Error('Version command failed'))

exec.getExecOutput = jest.fn().mockResolvedValue({
exitCode: 1,
stdout: '',
stderr: 'Version command failed'
})

await expect(main.execVersion()).rejects.toThrow('Version command failed')
})
})

describe('setup', () => {
beforeEach(() => {
// Mock successful download and extraction
tc.downloadTool.mockResolvedValue('/path/to/download')
tc.extractTar.mockResolvedValue('/path/to/cli')
core.getInput.mockReturnValue('1.2.3')
const exec = require('@actions/exec')
exec.getExecOutput = jest
.fn()
.mockResolvedValue({ exitCode: 0, stdout: '1.2.3', stderr: '' })
})

it('downloads and extracts CLI successfully', async () => {
Expand All @@ -111,6 +141,12 @@ describe('ORAS setup action', () => {

// Verify PATH addition
expect(core.addPath).toHaveBeenCalledWith('/path/to/cli')

// Verify path output is set
expect(core.setOutput).toHaveBeenCalledWith('path', '/path/to/cli')

// Verify version output is set
expect(core.setOutput).toHaveBeenCalledWith('version', '1.2.3')
})

it('handles download failure', async () => {
Expand Down
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ inputs:
version:
description: 'The ORAS CLI release to install'
required: false
default: ' 1.2.0'
default: '1.2.0'

outputs:
path:
description: 'The path where the ORAS CLI was installed'
version:
description: 'The ORAS CLI release that was installed'

Expand Down
20 changes: 19 additions & 1 deletion dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ function getPlatform() {
}
}

// Call the tool to print the version
async function execVersion() {
const exec = require('@actions/exec')
const { exitCode, stdout, stderr } = await exec.getExecOutput('oras', [
'version'
])
if (exitCode !== 0) {
throw new Error(stderr)
}
return stdout
}

async function setup() {
const version = getVersion()
const platform = getPlatform()
Expand All @@ -65,12 +77,18 @@ async function setup() {

// Expose the tool by adding it to the PATH
core.addPath(pathToCLI)

const versionOutput = await execVersion()

core.setOutput('path', pathToCLI)
core.setOutput('version', versionOutput.trim())
}

module.exports = {
setup,
getVersion,
getPlatform,
getArch,
getDownloadURL
getDownloadURL,
execVersion
}

0 comments on commit 1f64d40

Please sign in to comment.