Skip to content

Commit 889b6e7

Browse files
authored
Merge pull request #14 from stackql/feature/stackql-exec-refactor
refactored action
2 parents f8096c7 + 700ca5d commit 889b6e7

File tree

10 files changed

+160
-201
lines changed

10 files changed

+160
-201
lines changed

.github/workflows/stackql-exec.yml

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,71 @@ jobs:
1717
- name: Checkout
1818
uses: actions/checkout@v3
1919

20-
- name: Prep Google Creds (Windows)
21-
if: ${{ matrix.os == 'windows-latest'}}
22-
run: | ## use the secret to create json file
23-
$GoogleCreds = [System.Environment]::GetEnvironmentVariable("GOOGLE_CREDS_ENV")
24-
$GoogleCredsDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($GoogleCreds))
25-
Write-Output $GoogleCredsDecoded | Set-Content sa-key.json
26-
shell: pwsh
27-
env:
28-
GOOGLE_CREDS_ENV: ${{ secrets.GOOGLE_CREDS }}
29-
30-
- name: Prep Google Creds (bash)
31-
if: ${{ matrix.os != 'windows-latest' }}
32-
shell: bash
33-
run: | ## use the base64 encoded secret to create json file
34-
sudo echo ${{ secrets.GOOGLE_CREDS }} | base64 -d > sa-key.json
35-
36-
- name: exec google example with query file
37-
id: stackql-exec-file
20+
#
21+
# query no auth
22+
#
23+
- name: pull providers
24+
id: stackql-exec-string-noauth
3825
uses: ./
3926
with:
40-
auth_obj_path: './stackql_scripts/auth.json'
41-
query_file_path: './stackql_scripts/google-example.iql'
27+
query: "REGISTRY PULL github;
28+
REGISTRY PULL google;"
4229

30+
#
31+
# authenticated query
32+
#
4333
- name: exec github example with query string
4434
id: stackql-exec-string
4535
uses: ./
4636
with:
47-
auth_str: '{ "github": { "type": "basic", "credentialsenvvar": "STACKQL_GITHUB_CREDS" } }'
48-
query: "REGISTRY PULL github v23.01.00104;
49-
SHOW PROVIDERS;
50-
select total_private_repos
37+
query: "select total_private_repos
5138
from github.orgs.orgs
5239
where org = 'stackql';"
5340
env:
54-
STACKQL_GITHUB_CREDS: ${{ secrets.STACKQL_GITHUB_CREDS }}
41+
STACKQL_GITHUB_USERNAME: ${{ secrets.STACKQL_GITHUB_USERNAME }}
42+
STACKQL_GITHUB_PASSWORD: ${{ secrets.STACKQL_GITHUB_PASSWORD }}
43+
44+
#
45+
# query_file_path
46+
#
47+
- name: exec google example with query file
48+
id: stackql-exec-file
49+
uses: ./
50+
with:
51+
query_file_path: './stackql_scripts/google-instances-by-status.iql'
52+
env:
53+
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
54+
55+
#
56+
# query_file_path with vars
57+
#
58+
- name: exec google example with query file using vars
59+
id: stackql-exec-file-with-vars
60+
uses: ./
61+
with:
62+
query_file_path: './stackql_scripts/google-instances-by-status-with-vars.iql'
63+
vars: GOOGLE_PROJECT=${{ env.GOOGLE_PROJECT }},GOOGLE_ZONE=${{ env.GOOGLE_ZONE }}
64+
env:
65+
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
66+
GOOGLE_PROJECT: ${{ vars.GOOGLE_PROJECT }}
67+
GOOGLE_ZONE: ${{ vars.GOOGLE_ZONE }}
5568

5669
- name: validate stackql-exec output
5770
shell: bash
5871
run: |
59-
if [ -z '${{ steps.stackql-exec-file.outputs.exec-result }}' ]; then
72+
if [ -z '${{ steps.stackql-exec-string-noauth.outputs.exec-result }}' ]; then
6073
echo "exec-stackql output does not contain expected result"
6174
exit 1
6275
fi
6376
if [ -z '${{ steps.stackql-exec-string.outputs.exec-result }}' ]; then
6477
echo "exec-stackql output does not contain expected result"
6578
exit 1
6679
fi
67-
68-
80+
if [ -z '${{ steps.stackql-exec-file.outputs.exec-result }}' ]; then
81+
echo "exec-stackql output does not contain expected result"
82+
exit 1
83+
fi
84+
if [ -z '${{ steps.stackql-exec-file-with-vars.outputs.exec-result }}' ]; then
85+
echo "exec-stackql output does not contain expected result"
86+
exit 1
87+
fi

README.md

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,61 @@ Github Action as a wrapper for executing a single command in stackql, maps all s
33

44
# Usage
55

6-
## AUTH
7-
8-
`Example auth string`
9-
```
10-
{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" },
11-
"github": { "type": "basic", "credentialsenvvar": "STACKQL_GITHUB_CREDS" }}
12-
```
13-
It can be passed with `auth_str` as a string, or stored in a file and pass filename to `auth-obj-path`
14-
- For "basic" auth, you need to set a environment variable with same name as the value of `credentialsenvvar` in the auth string for the Github Action step. You can use [Github Secrets](https://docs.github.com/en/actions/reference/encrypted-secrets) to store the value of the environment variable, and use env to pass it to the action. For example:
15-
```
16-
env:
17-
STACKQL_GITHUB_CREDS: ${{ secrets.STACKQL_GITHUB_CREDS }}
18-
```
19-
- For "service_account" auth, you need to store the credentials into a file; You can follow the example of `Prep Google Creds (bash)` step in the example
6+
## Provider Authentication
7+
Authentication to StackQL providers is done via environment variables source from GitHub Actions Secrets. To learn more about authentication, see the setup instructions for your provider or providers at the [StackQL Provider Registry Docs](https://stackql.io/registry).
208

219
# Examples
22-
## Basic Example
10+
## Query Example
2311
```
2412
- name: exec github example
2513
uses: ./
2614
with:
27-
auth_str: '{ "github": { "type": "basic", "credentialsenvvar": "STACKQL_GITHUB_CREDS" } }'
28-
query: "REGISTRY PULL github v23.01.00104;
15+
query: "REGISTRY PULL github;
2916
SHOW PROVIDERS;
3017
select total_private_repos
3118
from github.orgs.orgs
3219
where org = 'stackql';"
3320
env:
34-
STACKQL_GITHUB_CREDS: ${{ secrets.STACKQL_GITHUB_CREDS }}
35-
21+
STACKQL_GITHUB_USERNAME: ${{ secrets.STACKQL_GITHUB_USERNAME }}
22+
STACKQL_GITHUB_PASSWORD: ${{ secrets.STACKQL_GITHUB_PASSWORD }}
3623
```
3724

3825

39-
## Auth json file and query file example
40-
- `auth.json`
41-
```
42-
{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" },
43-
"github": { "type": "basic", "credentialsenvvar": "STACKQL_GITHUB_CREDS" }}
44-
```
26+
## Query File example
4527
- `google-example.iql`
4628
```
47-
REGISTRY PULL github v23.01.00104;
48-
SHOW PROVIDERS;
49-
select total_private_repos
50-
from github.orgs.orgs
51-
where org = 'stackql';
29+
<<<jsonnet
30+
local project = std.extVar("GOOGLE_PROJECT");
31+
{
32+
project: project,
33+
}
34+
>>>
35+
REGISTRY PULL google;
36+
SELECT status, count(*) as num_instances
37+
FROM google.compute.instances
38+
WHERE project = '{{ .project }}'
39+
GROUP BY status;
5240
```
5341
**Example**
5442
```
55-
- name: Prep Google Creds (Windows)
56-
if: ${{ matrix.os == 'windows-latest'}}
57-
run: | ## use the secret to create json file
58-
$GoogleCreds = [System.Environment]::GetEnvironmentVariable("GOOGLE_CREDS_ENV")
59-
$GoogleCredsDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($GoogleCreds))
60-
Write-Output $GoogleCredsDecoded | Set-Content sa-key.json
61-
shell: pwsh
62-
env:
63-
GOOGLE_CREDS_ENV: ${{ secrets.GOOGLE_CREDS }}
64-
65-
- name: Prep Google Creds (bash)
66-
if: ${{ matrix.os != 'windows-latest' }}
67-
shell: bash
68-
run: | ## use the base64 encoded secret to create json file
69-
sudo echo ${{ secrets.GOOGLE_CREDS }} | base64 -d > sa-key.json
70-
7143
- name: exec google example
7244
uses: ./
7345
with:
74-
auth_obj_path: './stackql_scripts/auth.json'
7546
query_file_path: './stackql_scripts/google-example.iql'
47+
vars: GOOGLE_PROJECT=$GOOGLE_PROJECT, GOOGLE_ZONE=$GOOGLE_ZONE
48+
env:
49+
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
50+
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
51+
GOOGLE_ZONE: ${{ secrets.GOOGLE_ZONE }}
7652
```
7753

78-
7954
## Inputs
80-
- `auth_obj_path` - (optional) the path of json file that stores stackql AUTH string
81-
- `auth_str` - (optional) stackql AUTH string, need either auth_str or auth_obj_path
82-
- `query` - (optional) stackql query to execute
83-
- `query_file_path` - (optional) stackql query file to execute, need either query or query_file_path
84-
- `query_output` - (optional) output format of the stackql exec result, accept "table", "csv", "json", default to "json"
55+
- `query` - stackql query to execute **(need to supply either `query` or `query_file_path`)**
56+
- `query_file_path` - stackql query file to execute **(need to supply either `query` or `query_file_path`)**
57+
- `vars` - (optional) comma delimited list of variables to pass to the stackql query preprocessor (jsonnet), accepts `var1=val1 var2=val2`, can be used to source environment variables into stackql queries
58+
- `query_output` - (optional) output format of the stackql exec result, accepts `table`, `csv`, `json`, defaults to `json`
59+
- `auth_obj_path` - (optional) the path of json file that stores stackql AUTH string **(only required when using non-standard environment variable names)**
60+
- `auth_str` - (optional) stackql AUTH string **(only required when using non-standard environment variable names)**
8561

8662

8763
## Outputs
@@ -90,3 +66,11 @@ to `true`, `stdout` and `stderr` are set to `exec-result` and `exec-error`
9066

9167
- `exec-result` - The STDOUT stream of the call to the `stackql` binary.
9268
- `exec-error` - The STDERR stream of the call to the `stackql` binary.
69+
70+
## Test action locally
71+
To run unit tests locally against this action, use the following:
72+
73+
```
74+
npm i
75+
npm run test lib/tests/utils.test.js
76+
```

action.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ name: 'StackQL Studios - StackQL Exec'
22
description: 'A wrapper for executing a single command, maps all stackql exec args to actions args (supplied using with.)'
33
author: 'Yuncheng Yang, StackQL Studios'
44
inputs:
5-
auth_obj_path:
6-
description: file path to json object of stackql auth
7-
required: false
8-
auth_str:
9-
description: json string of stackql auth
10-
required: false
115
query:
126
description: stackql query to be executed
137
required: false
148
query_file_path:
159
description: stackql query file to be executed
1610
required: false
11+
vars:
12+
description: comma delimited list of vars to be passed to query preprocessor (jsonnet)
13+
required: false
1714
query_output:
1815
description: output format
1916
default: 'json'
2017
required: false
18+
auth_obj_path:
19+
description: file path to json object of stackql auth, not required if using standard provider authentication environment variables
20+
required: false
21+
auth_str:
22+
description: json string of stackql auth, not required if using standard provider authentication environment variables
23+
required: false
2124
outputs:
2225
exec-result:
2326
description: "stdout of stackql command"
@@ -40,7 +43,7 @@ runs:
4043
fi
4144
4245
- name: Setup StackQL
43-
uses: stackql/setup-stackql@v1.1.0-beta
46+
uses: stackql/setup-stackql@v1.2.0
4447
if: ${{steps.check-stackql.outputs.stackql_installed == 'false'}}
4548
with:
4649
use_wrapper: true
@@ -75,6 +78,7 @@ runs:
7578
QUERY_FILE_PATH: ${{ inputs.query_file_path }}
7679
QUERY: ${{inputs.query}}
7780
OUTPUT: ${{inputs.query_output}}
81+
VARS: ${{inputs.vars}}
7882

7983
- name: execute stackql command
8084
id: exec-stackql

0 commit comments

Comments
 (0)