Deploy dags from Google Cloud Storage to Astro
Prerequisites
- A Google cloud storage (GCS) bucket.
- An Astro Deployment with dag-only deploys enabled.
- A Deployment API token, Workspace API token, or Organization API token.
- An Astro project containing your project configurations.
Dag deploy template
This CI/CD template can be used to deploy dags from a single GCS bucket to a single Astro Deployment. When you create or modify a dag in the GCS bucket, a Cloud function triggers and initializes an astro
project to deploy your dags using Astro CLI.
dags
folder isn’t up to date with your GCS dags bucket when you trigger this deploy, you will revert your dags back to the version hosted in your Astro project.-
Download the latest Astro CLI binary from GitHub releases, then rename the file to,
astro_cli.tar.gz
. For example, to use Astro CLI version 1.13.0 in your template, downloadastro_1.13.0_linux_amd64.tar.gz
and rename it toastro_cli.tar.gz
. -
In your GCS bucket, create the following new folders:
dags
cli_binary
-
Add
astro_cli.tar.gz
tocli_binary
. -
Create a 1st gen Cloud Function with the Python 3.9 Runtime in the same region as your storage bucket.
-
Create a Cloud Storage trigger with the following configuration:
- Event provider: Select Cloud Storage.
- Event: Select On finalizing/creating file in the selected bucket.
- Bucket: Select your storage bucket.
-
Choose the Runtime Service Account. Ensure that the service account has
storage.objects.list
access to the Google Cloud Storage bucket. -
Set the following environment variables for your Cloud Function:
ASTRO_HOME
=\tmp
ASTRO_API_TOKEN
: The value for your Workspace or Organization API token.ASTRO_DEPLOYMENT_ID
: Your Deployment ID.
For production Deployments, ensure that you store the value for your API token in a secrets backend. See Secret Manager overview.
-
Add the dependency
google-cloud-storage
to therequirements.txt
file for your Cloud Function. See Specifying Dependencies in Python. -
Add the following code to
main.py
:
- If you haven’t already, deploy your complete Astro project to your Deployment. See Deploy code.
- Add your dags to the
dags
folder in your storage bucket.
If you stage multiple commits to dag files and push them all at once to your remote branch, the template only deploys dag code changes from the most recent commit. It will miss any code changes made in previous commits.
To avoid this, either push commits individually or configure your repository to Squash commits for pull requests that merge multiple commits simultaneously.
- In the Astro UI, select a Workspace, click Deployments, and then select your Deployment. Confirm that your deploy worked by checking the Deployment Dag bundle version. The version’s name should include the time that you added the dags to your GCS bucket.