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

Populate action outputs for path and version #2

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ runs:
id: setup-oras-cli
- name: Print outputs
shell: bash
run: |
echo "Installed version: ${{ steps.setup-oras-cli.outputs.version }}"
run: echo "${{ toJSON(steps.setup-oras-cli.outputs) }}"
- name: Test CLI
shell: bash
run: oras version
8 changes: 8 additions & 0 deletions .github/workflows/flowzone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ jobs:
secrets: inherit
with:
restrict_custom_actions: false
custom_test_matrix: >
{
"os": [
["ubuntu-latest"],
["macos-latest"],
["windows-latest"]
],
}
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist/
node_modules/
coverage/
coverage/
CHANGELOG.md
.versionbot/
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
46 changes: 46 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ describe('ORAS setup action', () => {
'https://github.com/oras-project/oras/releases/download/v1.2.3/oras_1.2.3_linux_amd64.tar.gz'
)
})
it('constructs correct download URL for windows', () => {
const url = main.getDownloadURL('1.2.3', 'windows', 'amd64')
expect(url).toBe(
'https://github.com/oras-project/oras/releases/download/v1.2.3/oras_1.2.3_windows_amd64.zip'
)
})
})

describe('execVersion', () => {
it('executes version command', async () => {
const exec = require('@actions/exec')
exec.getExecOutput = jest.fn().mockResolvedValue({
exitCode: 0,
stdout: 'Version: 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', () => {
Expand All @@ -101,6 +135,12 @@ describe('ORAS setup action', () => {
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: 'Version: 1.2.3',
stderr: ''
})
})

it('downloads and extracts CLI successfully', async () => {
Expand All @@ -111,6 +151,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
25 changes: 23 additions & 2 deletions 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.

25 changes: 23 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ const tc = require('@actions/tool-cache')

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

function getVersion() {
Expand Down Expand Up @@ -50,6 +53,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.match(/Version:\s*([\d.]+)/)[1]
}

async function setup() {
const version = getVersion()
const platform = getPlatform()
Expand All @@ -65,12 +80,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
}
Loading