Skip to content
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
10 changes: 8 additions & 2 deletions .github/workflows/sanity-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ permissions:
contents: read

jobs:

test-action:
name: OCI CLI GitHub Action Test
name: Sanity test run-oci-cli-command action
runs-on: ubuntu-latest
env:
OCI_CLI_USER: ${{ secrets.OCI_CLI_USER }}
Expand All @@ -37,3 +36,10 @@ jobs:
- name: Output object storage namespace
id: output-os-ns
run: echo "${{ steps.test-action-get-os-ns.outputs.output }}"

- name: Test non-JSON output
id: test-non-json-output
uses: ./
with:
command: --output=table iam region list
silent: false
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ purposes only:

## Outputs

- `output`: will contain the results of the command in JSON format.
- `output`: will contain the results of the command.
- `raw_output`: if the output of a given query is a single string value, `raw_output` will return the string without
surrounding quotes.

> **Note:** filtering the `output` or `raw_output` by piping it through another tool like `jq` may result in the
> filtered output being visible in the job logs. We recommend using the `query` parameter instead.
> filtered output being visible in the job logs. We recommend using the `query` parameter whenever possible.

If the result of the command is not valid JSON, it will not be visible unless `silent` is set to _false_.

## Sample workflow

Expand All @@ -63,14 +65,14 @@ jobs:
OCI_CLI_REGION: ${{ secrets.OCI_CLI_REGION }}
steps:
- name: Retrieve the OCID of a named compartment in tenancy
uses: oracle-actions/run-oci-cli-command@v1.2.0
uses: oracle-actions/run-oci-cli-command@v1.3.0
id: find-compartment-id
with:
command: 'iam compartment list --compartment-id-in-subtree=true'
query: "data[?name=='testing'].id"

- name: Retrieve the display name and shape of the instances in my compartment
uses: oracle-actions/run-oci-cli-command@v1.2.0
uses: oracle-actions/run-oci-cli-command@v1.3.0
id: find-instances
with:
command: 'compute instance list --compartment-id ${{ steps.find-compartment-id.outputs.raw_output }}'
Expand Down
49 changes: 33 additions & 16 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.

4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oracle-actions/run-oci-cli-command",
"version": "1.2.0",
"version": "1.3.0",
"author": {
"name": "Oracle Cloud Infrastructure",
"email": "[email protected]"
Expand Down
49 changes: 33 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'

/**
* Test if the content of a variable has a valid JSON structure
*/
function isJson(item: string): boolean {
let value = typeof item !== 'string' ? JSON.stringify(item) : item
try {
value = JSON.parse(value)
} catch (e) {
return false
}

return typeof value === 'object' && value !== null
}

/**
* Install the OCI CLI (if ncessary) and then run the command specified by
* the user workflow. By default, the action suppresses/masks the command
Expand Down Expand Up @@ -42,27 +56,30 @@ async function runOciCliCommand(): Promise<void> {
if (silent) core.setSecret(cliCommand)

const cliResult = await exec.getExecOutput(cliCommand, [], { silent: silent })
let stdout = {}
let output = ''
let raw_output = ''

if (cliResult) {
const stdout = cliResult.stdout ? JSON.parse(cliResult.stdout) : {}
const stderr = cliResult.stderr ? JSON.stringify(cliResult.stderr) : ''

if (cliResult.exitCode == 0) {
const output = JSON.stringify(JSON.stringify(stdout))

if (silent && output) core.setSecret(output)
core.setOutput('output', output)

if (cliResult && cliResult.exitCode == 0) {
if (cliResult.stdout && !isJson(cliResult.stdout)) {
output = cliResult.stdout
raw_output = cliResult.stdout
} else {
stdout = JSON.parse(cliResult.stdout)
output = JSON.stringify(JSON.stringify(stdout))
if (Object.keys(stdout).length == 1) {
const raw_output = stdout[0]
if (silent && raw_output) core.setSecret(raw_output)
core.setOutput('raw_output', raw_output)
raw_output = Object.keys(stdout)[0]
}
} else {
core.setFailed(`Failed: ${JSON.stringify(stderr)}`)
}

if (silent && output) core.setSecret(output)
core.setOutput('output', output)

if (silent && raw_output) core.setSecret(raw_output)
core.setOutput('raw_output', raw_output)
} else {
core.setFailed('Failed to execute OCI CLI command.')
const stderr = cliResult.stderr ? JSON.stringify(cliResult.stderr) : ''
core.setFailed(`Failed: ${JSON.stringify(stderr)}`)
}
}

Expand Down