Skip to content

Commit

Permalink
Add options for all "Trigger job to run" payload properties (#18)
Browse files Browse the repository at this point in the history
* document new run_body option and deprecate git_sha

* Add error debug

* add run_body input

* change object implementation to key-by-key

* rewrite inputs explanation

* add yaml package

* add steps_override handling

* move to node16
  • Loading branch information
chamini2 authored Sep 20, 2022
1 parent 09b68b1 commit 9f46e7d
Show file tree
Hide file tree
Showing 232 changed files with 18,093 additions and 29 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,34 @@ This action lets you trigger a job run on [dbt Cloud](https://cloud.getdbt.com),

## Inputs

### Required
### Credentials

- `dbt_cloud_token` - dbt Cloud [API token](https://docs.getdbt.com/docs/dbt-cloud/dbt-cloud-api/service-tokens)
- `dbt_cloud_account_id` - dbt Cloud Account ID
- `dbt_cloud_job_id` - dbt Cloud Job ID
- `failure_on_error` - Boolean to make the action report a failure when dbt-cloud runs. Mark this as `false` to run fal after the dbt-cloud job.

### Optional
- `cause` - Cause message to use (Default=`"Triggered by a GitHub Action"`)
- `git_sha` - The git SHA (e.g. commit) to checkout before running dbt Cloud Job. This refers to the state your repository is when running. ([dbt API docs](https://docs.getdbt.com/dbt-cloud/api-v2#tag/Jobs/operation/triggerRun))
- `interval` - The interval between polls in seconds (Default=`30`)

We recommend passing sensitive variables as GitHub secrets. [Example usage](https://github.com/fal-ai/fal_bike_example/blob/main/.github/workflows/fal_dbt.yml).

### Action configuration

- `failure_on_error` - Boolean to make the action report a failure when dbt-cloud runs. Mark this as `false` to run fal after the dbt-cloud job.
- `interval` - The interval between polls in seconds (Default: `30`)

### dbt Cloud Job configuration

Use any of the [documented options for the dbt API](https://docs.getdbt.com/dbt-cloud/api-v2#tag/Jobs/operation/triggerRun).

- `cause` (Default: `Triggered by a Github Action`)
- `git_sha`
- `git_branch`
- `schema_override`
- `dbt_version_override`
- `threads_override`
- `target_name_override`
- `generate_docs_override`
- `timeout_seconds_override`
- `steps_override`

## Create your workflow
```yaml
name: Run dbt cloud
Expand Down
62 changes: 50 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,69 @@ branding:
color: "orange"
inputs:
dbt_cloud_token:
description: "dbt Cloud API token"
description: dbt Cloud API token
required: true
dbt_cloud_account_id:
description: "dbt Cloud account ID"
description: dbt Cloud account ID
required: true
dbt_cloud_job_id:
description: "dbt Cloud Job ID"
description: dbt Cloud Job ID
required: true
cause:
description: "Job trigger cause"
required: true
default: "Triggered by a GitHub Action"
interval:
description: Interval between polls in seconds
required: false
default: "30"
failure_on_error:
description: Boolean to make the action report a failure when dbt-cloud runs. Mark this as `false` to run fal after the dbt-cloud job.
required: true

cause:
description: Job trigger cause
required: true
default: Triggered by a GitHub Action

git_sha:
description: "The git SHA to execute dbt Cloud Job"
description: The git sha to check out before running this job
required: false
interval:
description: "Interval between polls in seconds"

git_branch:
description: The git branch to check out before running this job
required: false
default: "30"

schema_override:
description: Override the destination schema in the configured target for this job.
required: false

dbt_version_override:
description: Override the version of dbt used to run this job
required: false

threads_override:
description: Override the number of threads used to run this job
required: false

target_name_override:
description: Override the target.name context variable used when running this job
required: false

generate_docs_override:
description: Override whether or not this job generates docs (true=yes, false=no)
required: false
# type: boolean

timeout_seconds_override:
description: Override the timeout in seconds for this job
required: false
# type: integer

steps_override:
description: Override the list of steps for this job. Pass a yaml list enclosed in a string and it will be parsed and sent as a list.
required: false
# type: array of strings

outputs:
git_sha:
description: "Repository SHA in which dbt Cloud Job ran"
runs:
using: "node12"
using: node16
main: "index.js"
57 changes: 49 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios');
const core = require('@actions/core');
const fs = require('fs');
const axiosRetry = require('axios-retry');
const YAML = require('yaml')

axiosRetry(axios, {
retryDelay: (retryCount) => retryCount * 1000,
Expand Down Expand Up @@ -36,13 +37,55 @@ function sleep(ms) {
});
}

async function runJob(account_id, job_id, cause, git_sha) {
let body = { cause: cause }
const OPTIONAL_KEYS = [
'git_sha',
'git_branch',
'schema_override',
'dbt_version_override',
'threads_override',
'target_name_override',
'generate_docs_override',
'timeout_seconds_override',
'steps_override',
];

const BOOL_OPTIONAL_KEYS = [ 'generate_docs_override' ];
const INTEGER_OPTIONAL_KEYS = [ 'threads_override', 'timeout_seconds_override' ];
const YAML_PARSE_OPTIONAL_KEYS = [ 'steps_override' ];

async function runJob(account_id, job_id) {
const cause = core.getInput('cause');

const body = { cause };

for (const key of OPTIONAL_KEYS) {
let input = core.getInput(key);

if (input != '' && BOOL_OPTIONAL_KEYS.includes(key)) {
input = core.getBooleanInput(key);
} else if (input != '' && INTEGER_OPTIONAL_KEYS.includes(key)) {
input = parseInt(input);
} else if (input != '' && YAML_PARSE_OPTIONAL_KEYS.includes(key)) {
core.debug(input);
try {
input = YAML.parse(input);
if (typeof input == 'string') {
input = [ input ];
}
} catch (e) {
core.setFailed(`Could not interpret ${key} correctly. Pass valid YAML in a string.\n Example:\n property: '["a string", "another string"]'`);
throw e;
}
}

if (git_sha) {
body['git_sha'] = git_sha
// Type-checking equality becuase of boolean inputs
if (input !== '') {
body[key] = input;
}
}

core.debug(`Run job body:\n${JSON.stringify(body, null, 2)}`)

let res = await dbt_cloud_api.post(`/accounts/${account_id}/jobs/${job_id}/run/`, body)
return res.data;
}
Expand Down Expand Up @@ -78,14 +121,11 @@ async function getArtifacts(account_id, run_id) {


async function executeAction() {

const account_id = core.getInput('dbt_cloud_account_id');
const job_id = core.getInput('dbt_cloud_job_id');
const cause = core.getInput('cause');
const git_sha = core.getInput('git_sha') || null;
const failure_on_error = core.getBooleanInput('failure_on_error');

const jobRun = await runJob(account_id, job_id, cause, git_sha);
const jobRun = await runJob(account_id, job_id);
const runId = jobRun.data.id;

core.info(`Triggered job. ${jobRun.data.href}`);
Expand Down Expand Up @@ -141,6 +181,7 @@ async function main() {
} catch (e) {
// Always fail in this case because it is not a dbt error
core.setFailed('There has been a problem with running your dbt cloud job:\n' + e.toString());
core.debug(e.stack)
}
}

Expand Down
8 changes: 8 additions & 0 deletions node_modules/.package-lock.json

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

13 changes: 13 additions & 0 deletions node_modules/yaml/LICENSE

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

143 changes: 143 additions & 0 deletions node_modules/yaml/README.md

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

Loading

0 comments on commit 9f46e7d

Please sign in to comment.