From 061ff3ba7b5226e28fe106ecd226c148d0d85e9d Mon Sep 17 00:00:00 2001 From: Matteo Ferrando Date: Thu, 21 Jul 2022 10:32:08 -0400 Subject: [PATCH 1/2] Remove git sha checkout and return as output --- action.yml | 11 +++++++---- index.js | 37 +++++++++++-------------------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/action.yml b/action.yml index 6eef3a0..10f50ad 100644 --- a/action.yml +++ b/action.yml @@ -1,17 +1,17 @@ name: "dbt Cloud action" -description: "Runs a dbt Cloud job specified by job id" +description: "Runs a dbt Cloud Job specified by Job ID" branding: icon: "cloud" 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" @@ -21,6 +21,9 @@ inputs: description: "Interval between polls in seconds" required: false default: "30" +outputs: + git_sha: + description: "Repository SHA in which dbt Cloud Job ran" runs: using: "node12" main: "index.js" diff --git a/index.js b/index.js index d2daad9..819ca8e 100644 --- a/index.js +++ b/index.js @@ -91,31 +91,16 @@ async function executeAction() { } } -function checkoutTargetBranch(git_sha) { - core.info(`Checking out ${git_sha}`); - const command = `git -c advice.detachedHead=false checkout ${git_sha}`; - return new Promise((resolve, reject) => { - exec(command, (error, stdout, stderr) => { - if (stderr) { - core.info(`STDERR: ${stderr}`); - } - if (stdout) { - core.info(`STDOUT: ${stdout}`); - } - - if (error) { - // Return to avoid reject and resolve in same Promise - return reject(error); - } - - core.info('Done'); - resolve(); - }); - }) +async function main() { + try { + const git_sha = await executeAction(); + + // GitHub Action output + core.info(`dbt Cloud Job commit SHA is ${git_sha}`) + core.setOutput('git_sha', git_sha); + } catch (e) { + core.setFailed('There has been a problem with running your dbt cloud job:\n' + e.toString()); + } } -executeAction() - .then(git_sha => checkoutTargetBranch(git_sha)) - .catch(e => { - core.setFailed('There has been a problem with running your dbt cloud job: ' + e.toString()); - }); +main(); From 234dc94e52973ead1bcbc1193b0e3aaab18738cf Mon Sep 17 00:00:00 2001 From: Matteo Ferrando Date: Thu, 21 Jul 2022 12:08:38 -0400 Subject: [PATCH 2/2] Update readme --- README.md | 109 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 14cbc90..0040a33 100644 --- a/README.md +++ b/README.md @@ -2,24 +2,101 @@ This action lets you trigger a job run on [dbt Cloud](https://cloud.getdbt.com), fetches the `run_results.json` artifact, and `git checkout`s the branch that was ran by dbt Cloud. -### Inputs - **Required**: - - `dbt_cloud_token` - dbt Cloud [api token](https://docs.getdbt.com/docs/dbt-cloud/dbt-cloud-api/user-tokens) - - `dbt_cloud_account_id` - dbt Cloud account id - - `dbt_cloud_job_id` - dbt Cloud job id +## Inputs -We recommend passing sensitive variables as Github secrets. Example [here](https://github.com/fal-ai/fal_bike_example/blob/main/.github/workflows/fal_dbt.yml). +### Required +- `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 - **Optional**: - - `cause` - Cause message to use [Default=`"Triggered by a GitHub Action"`] - - `interval` - The interval between polls in seconds [Default=`30`] -### Example usage +### Optional +- `cause` - Cause message to use [Default=`"Triggered by a GitHub Action"`] +- `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). + +## Create your workflow +```yaml +name: Run dbt cloud +on: + workflow_dispatch: + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - uses: fal-ai/dbt-cloud-action@main + id: dbt_cloud_run + with: + dbt_cloud_token: ${{ secrets.DBT_CLOUD_API_TOKEN }} + dbt_cloud_account_id: ${{ secrets.DBT_CLOUD_ACCOUNT_ID }} + dbt_cloud_job_id: ${{ secrets.DBT_CLOUD_JOB_ID }} +``` + +### Use with [fal](https://github.com/fal-ai/fal) + +You can trigger a dbt Cloud run and it will download the artifacts to be able to run your `fal run` command easily in GitHub Actions. + +You have to do certain extra steps described here: + ```yaml -- uses: fal-ai/dbt-cloud-action@main - id: dbt_cloud_run - with: - dbt_cloud_token: ${{ secrets.DBT_CLOUD_API_TOKEN }} - dbt_cloud_account_id: ${{ secrets.DBT_ACCOUNT_ID }} - dbt_cloud_job_id: ${{ secrets.DBT_JOB_ID }} +name: Run dbt cloud and fal scripts +on: + workflow_dispatch: + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + # Checkout before downloading artifacts or setting profiles.yml + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - uses: fal-ai/dbt-cloud-action@main + id: dbt_cloud_run + with: + dbt_cloud_token: ${{ secrets.DBT_CLOUD_API_TOKEN }} + dbt_cloud_account_id: ${{ secrets.DBT_ACCOUNT_ID }} + dbt_cloud_job_id: ${{ secrets.DBT_CLOUD_JOB_ID }} + + - name: Setup profiles.yml + shell: python + env: + contents: ${{ secrets.PROFILES_YML }} + run: | + import yaml + import os + import io + + profiles_string = os.getenv('contents') + profiles_data = yaml.safe_load(profiles_string) + + with io.open('profiles.yml', 'w', encoding='utf8') as outfile: + yaml.dump(profiles_data, outfile, default_flow_style=False, allow_unicode=True) + + - uses: actions/setup-python@v2 + with: + python-version: "3.9.x" + + - name: Install dependencies + # Normally would install a `requirements.txt`. + # This is to make it visible. + run: | + pip install dbt-bigquery + pip install fal + + - name: Run fal scripts + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} + SLACK_BOT_CHANNEL: ${{ secrets.SLACK_BOT_CHANNEL }} + run: | + # Move to the same code state of the dbt Cloud Job + git checkout ${{ steps.dbt_cloud_run.outputs.git_sha }} + # TODO: review target in passed profiles.yaml contents + fal run --profiles-dir . + ```