Skip to content

Commit 102cf80

Browse files
authored
Merge pull request #2 from balena-io-experimental/kyle/outputs
Populate action outputs for path and version
2 parents c815794 + a927d80 commit 102cf80

File tree

9 files changed

+109
-9
lines changed

9 files changed

+109
-9
lines changed

.github/actions/test/action.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ runs:
99
id: setup-oras-cli
1010
- name: Print outputs
1111
shell: bash
12-
run: |
13-
echo "Installed version: ${{ steps.setup-oras-cli.outputs.version }}"
12+
run: echo "${{ toJSON(steps.setup-oras-cli.outputs) }}"
1413
- name: Test CLI
1514
shell: bash
1615
run: oras version

.github/workflows/flowzone.yml

+8
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ jobs:
2525
secrets: inherit
2626
with:
2727
restrict_custom_actions: false
28+
custom_test_matrix: >
29+
{
30+
"os": [
31+
["ubuntu-latest"],
32+
["macos-latest"],
33+
["windows-latest"]
34+
],
35+
}

.prettierignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
dist/
22
node_modules/
3-
coverage/
3+
coverage/
4+
CHANGELOG.md
5+
.versionbot/

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ To use this action in your workflow, add the following step:
2121

2222
### Outputs
2323

24+
- `path`: The path where the ORAS CLI was installed.
2425
- `version`: The ORAS CLI release that was installed.
2526

2627
## Contributing

__tests__/main.test.js

+46
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,40 @@ describe('ORAS setup action', () => {
9393
'https://github.com/oras-project/oras/releases/download/v1.2.3/oras_1.2.3_linux_amd64.tar.gz'
9494
)
9595
})
96+
it('constructs correct download URL for windows', () => {
97+
const url = main.getDownloadURL('1.2.3', 'windows', 'amd64')
98+
expect(url).toBe(
99+
'https://github.com/oras-project/oras/releases/download/v1.2.3/oras_1.2.3_windows_amd64.zip'
100+
)
101+
})
102+
})
103+
104+
describe('execVersion', () => {
105+
it('executes version command', async () => {
106+
const exec = require('@actions/exec')
107+
exec.getExecOutput = jest.fn().mockResolvedValue({
108+
exitCode: 0,
109+
stdout: 'Version: 1.2.3',
110+
stderr: ''
111+
})
112+
113+
await main.execVersion()
114+
115+
expect(exec.getExecOutput).toHaveBeenCalledWith('oras', ['version'])
116+
})
117+
118+
it('handles version command failure', async () => {
119+
const exec = require('@actions/exec')
120+
// exec.getExecOutput.mockRejectedValue(new Error('Version command failed'))
121+
122+
exec.getExecOutput = jest.fn().mockResolvedValue({
123+
exitCode: 1,
124+
stdout: '',
125+
stderr: 'Version command failed'
126+
})
127+
128+
await expect(main.execVersion()).rejects.toThrow('Version command failed')
129+
})
96130
})
97131

98132
describe('setup', () => {
@@ -101,6 +135,12 @@ describe('ORAS setup action', () => {
101135
tc.downloadTool.mockResolvedValue('/path/to/download')
102136
tc.extractTar.mockResolvedValue('/path/to/cli')
103137
core.getInput.mockReturnValue('1.2.3')
138+
const exec = require('@actions/exec')
139+
exec.getExecOutput = jest.fn().mockResolvedValue({
140+
exitCode: 0,
141+
stdout: 'Version: 1.2.3',
142+
stderr: ''
143+
})
104144
})
105145

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

112152
// Verify PATH addition
113153
expect(core.addPath).toHaveBeenCalledWith('/path/to/cli')
154+
155+
// Verify path output is set
156+
expect(core.setOutput).toHaveBeenCalledWith('path', '/path/to/cli')
157+
158+
// Verify version output is set
159+
expect(core.setOutput).toHaveBeenCalledWith('version', '1.2.3')
114160
})
115161

116162
it('handles download failure', async () => {

action.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ inputs:
66
version:
77
description: 'The ORAS CLI release to install'
88
required: false
9-
default: ' 1.2.0'
9+
default: '1.2.0'
1010

1111
outputs:
12+
path:
13+
description: 'The path where the ORAS CLI was installed'
1214
version:
1315
description: 'The ORAS CLI release that was installed'
1416

dist/index.js

+23-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const tc = require('@actions/tool-cache')
33

44
function getDownloadURL(version, platform, arch) {
55
// Construct the download URL
6+
// See: https://github.com/oras-project/oras/releases/tag/v1.2.0
67
// e.g. https://github.com/oras-project/oras/releases/download/v1.2.0/oras_1.2.0_linux_arm64.tar.gz
7-
return `https://github.com/oras-project/oras/releases/download/v${version}/oras_${version}_${platform}_${arch}.tar.gz`
8+
// e.g. https://github.com/oras-project/oras/releases/download/v1.2.0/oras_1.2.0_windows_amd64.zip
9+
const extension = platform === 'windows' ? 'zip' : 'tar.gz'
10+
return `https://github.com/oras-project/oras/releases/download/v${version}/oras_${version}_${platform}_${arch}.${extension}`
811
}
912

1013
function getVersion() {
@@ -50,6 +53,18 @@ function getPlatform() {
5053
}
5154
}
5255

56+
// Call the tool to print the version
57+
async function execVersion() {
58+
const exec = require('@actions/exec')
59+
const { exitCode, stdout, stderr } = await exec.getExecOutput('oras', [
60+
'version'
61+
])
62+
if (exitCode !== 0) {
63+
throw new Error(stderr)
64+
}
65+
return stdout.match(/Version:\s*([\d.]+)/)[1]
66+
}
67+
5368
async function setup() {
5469
const version = getVersion()
5570
const platform = getPlatform()
@@ -65,12 +80,18 @@ async function setup() {
6580

6681
// Expose the tool by adding it to the PATH
6782
core.addPath(pathToCLI)
83+
84+
const versionOutput = await execVersion()
85+
86+
core.setOutput('path', pathToCLI)
87+
core.setOutput('version', versionOutput.trim())
6888
}
6989

7090
module.exports = {
7191
setup,
7292
getVersion,
7393
getPlatform,
7494
getArch,
75-
getDownloadURL
95+
getDownloadURL,
96+
execVersion
7697
}

0 commit comments

Comments
 (0)