- Add Lightdash preview projects to pull requests
 - Deploy dbt changes to your production Lightdash project
 
Add credentials to Github secrets
We are going to add some secrets and config to GitHub actions, but you don’t want those to be public, so the best way to do this is to add them as secrets on Github.If you already have a GitHub action for Lightdash, then you can use the same Lightdash secrets you created for your other action.
Settings, on the left sidebar, click on Secrets under Security. Now click on the New repository secret

LIGHTDASH_API_KEY as a secret.
Back in Lightdash, create a new personal access token, by going to Settings > Personal Access Tokens. This is the token you’ll put in for LIGHTDASH_API_KEY.

LIGHTDASH_PROJECT is the UUID for your project. For example, if your URL looks like https://app.lightdash.cloud/projects/3538ab33-dc90-aabb-bc00-e50bba3a5f69/tables, then 3538ab33-dc90-45f0-aabb-e50bba3a5f69 is your LIGHTDASH_PROJECT.
LIGHTDASH_URL is https://eu1.lightdash.cloud or https://app.lightdash.cloud for Starter customers, or something like https://your_company.lightdash.cloud for dedicated instances. If you self-host, this should be your own custom domain.
DBT_PROFILES tips:
- You might be able to copy a bunch of the information from your local 
profiles.ymlfile. You can see what’s in there by typingcat ~/.dbt/profiles.ymlin your terminal. - If you have a separate 
prodanddevprofile, you probably want to use the information from yourprodprofile for your GitHub action. - If you want to have different connection settings depending on the user that opened the pull request (dev profiles), then check out this guide.
 
DBT_PROFILES secret.
BigQuery
BigQuery
BigQuery OAuth:Step 1: create a secret called Step 2: create another secret called More info in dbt’s profiles docs: https://docs.getdbt.com/reference/warehouse-profiles/bigquery-profile#service-account-file
GOOGLE_APPLICATION_CREDENTIALSCreate a service account with the role BigQuery User. Add the service account credentials (the JSON file) that you want to use for your GitHub action. It should look something like this:DBT_PROFILESCopy-paste this template into the secret and fill out the details.This will always use this project connection in your GitHub actions. If you want your preview projects to have different connection settings depending on the user that opened the pull request (dev profiles), then see what you need to add to your secret in this guide.Postgres
Postgres
Postgres profile configuration:More info in dbt’s profiles docs: https://docs.getdbt.com/reference/warehouse-profiles/postgres-profile#profile-configurationThis will always use this project connection in your GitHub actions. If you want your preview projects to have different connection settings depending on the user that opened the pull request (dev profiles), then see what you need to add to your secret in this guide.
Redshift
Redshift
Redshift password-based authentication:More info in dbt’s profiles docs: https://docs.getdbt.com/reference/warehouse-profiles/redshift-profile#password-based-authenticationThis will always use this project connection in your GitHub actions. If you want your preview projects to have different connection settings depending on the user that opened the pull request (dev profiles), then see what you need to add to your secret in this guide.
Snowflake
Snowflake
User / Password authentication:More info in dbt’s profiles docs: https://docs.getdbt.com/reference/warehouse-profiles/snowflake-profile#user—password-authenticationThis will always use this project connection in your GitHub actions. If you want your preview projects to have different connection settings depending on the user that opened the pull request (dev profiles), then see what you need to add to your secret in this guide.
DataBricks
DataBricks
Set up a DataBricks target:More info in dbt’s profiles docs: https://docs.getdbt.com/reference/warehouse-profiles/bigquery-profile#service-account-jsonThis will always use this project connection in your GitHub actions. If you want your preview projects to have different connection settings depending on the user that opened the pull request (dev profiles), then see what you need to add to your secret in this guide.
Add previews to pull requests
If you’ve connected Lightdash to GitHub, you can setup agithub action and get Lightdash to create new dynamic preview projects automatically when a new pull request is created, and it will automatically delete the preview project when the pull request is closed or merged.
Create preview workflow
Go to your repo, click onActions menu, and click on Configure

New workflow, then select setup a workflow yourself.

Start commit
Do the same with this close-preview.yml file.
Use developer credentials
When developing in dbt, you typically have a different set of credentials and dataset/schema than when you are running in production. Here are two options on how to set them up based on the developer that opened the Pull Request.If you use dbt cloud IDE to create commits and pull requests you need a few extra steps. We need to add a step in the GitHub action to fetch the user that created the pull request.When copying the following templates, you should replace 
${{ github.actor }} with ${{steps.get_pr_creator.outputs.result}}.Use profile targets
Update yourDBT_PROFILES to have 1 target per developer. The target name should be their GitHub username.
--target flag for the lightdash start-preview command.
Use Github environments
Setup a GitHub environment for each developer where the secrets are specifically for them. The environment name should be their GitHub username. Then, update your GitHub action to use the username as the environment.Use dbt cloud schema
If you are using a continuous integration job in dbt cloud, you can use the schema that is created by dbt cloud (dbt_cloud_pr_<job_id>_<pr_id>) for your preview project.
First we need to add an environment variable to your profile.yml file that will be used by dbt to connect to the correct schema.
dataset instead of schema.
Then we need to add a step in the GitHub action to fetch the pull request id.
1234. You will need to replace this with the actual job id.
You’re done!
Everytime you create a new pull request, a Lightdash preview project with your branch name will be created on your organization. You will see this link as a github-actions bot comment in the pull request conversation. Everytime you make a change to that branch, the preview environment will get updated. Once you close or merge your pull request, the preview project will get deleted. You can see the log on yourGithub actions page:

Deploy changes to Lightdash
If you’ve connected Lightdash to GitHub, you can use a github action to deploy your project automatically whenever new changes get merged to your main branch. This is the easiest way to keep Lightdash in sync with your changes to dbt.Create deploy workflow
Go to your repo, click onActions menu.
If you don’t have any GitHub actions, you’ll just need to click on Configure

New workflow, then select setup a workflow yourself.

deploy-lightdash.yml
And commit this to your repo by clicking on Start commit.
You’re done!
Everytime you merge a change to your repo, on themain branch, it will automatically deploy your new config into your Lightdash projects
You can see the log on the Github actions page
