Skip to main content

Automate code deploys with CI/CD

Continuous Integration and Continuous Delivery (CI/CD) is an industry term that refers to programmatic workflows that automate key parts of the software development lifecycle, including code changes, builds, and testing. CI/CD enables teams to develop faster, more securely, and more reliably.

On Astro, you can use Deployment API keys to automate deploying code changes to a Deployment. Astronomer recommends hosting your Astro project source code in a version control tool and setting up a CI/CD workflow for all production environments.

There are many benefits to configuring a CI/CD workflow on Astro. Specifically, you can:

  • Avoid manually running astro deploy every time you make a change to your Astro project.
  • Ensure that all changes to your Astro project are reviewed and approved by your team before they get pushed to Astro.
  • Automate promoting code across development and production environments on Astro when pull requests to certain branches are merged.
  • Enforce automated testing, which increases code quality and allows your team to respond quickly in case of an error or failure.
  • Configure more granular user permissions by managing access and changes to your Astro project source code in your version control tool.

Use the Astronomer CI/CD templates to automate deploying code to Astro with popular CI/CD management tools, including GitHub Actions and Circle CI.

Prerequisites

CI/CD templates

Templates allow you to easily configure automated workflows using popular CI/CD tools. Each template can be implemented as-is to produce a simple CI/CD pipeline. Astronomer recommends reconfiguring the templates to work with your own directory structures, tools, and best practices.

Astro supports the following CI/CD workflows:

  • Image-only workflows: All files in your Astro project are built into a Docker image and pushed to Astro in a single step.
  • DAG-based workflows: The DAG-only deploy feature is used to deploy DAGs in your Astro project separate from the Docker image that is built for all other project files.

The following templates are available to implement your CI/CD workflows:

  • Single branch: Deploys a single branch from your version control tool to Astro. This is the default template for all CI/CD tools.
  • Multiple branch: Deploys multiple branches to separate Deployments on Astro.
  • Custom image: Deploys an Astro project with a customized Runtime image and additional build arguments.

Image-only workflows

The image-only workflow builds a Docker image and pushes it to Astro whenever you update any file in your Astro project. This type of template is simple to set up and works well for development workflows that include complex Docker customization or logic.

CI/CD templates for image-only workflows:

  • Access Deployment API key credentials. These credentials must be set as OS-level environment variables named ASTRONOMER_KEY_ID and ASTRONOMER_KEY_SECRET.
  • Install the latest version of the Astro CLI.
  • Run astro deploy. This creates a Docker image for your Astro project, authenticates to Astro using your Deployment API key, and pushes the image to your Deployment.

This workflow is equivalent to the following bash script:

# Set Deployment API key credentials as environment variables
$ export ASTRONOMER_KEY_ID="<your-api-key-id>"
$ export ASTRONOMER_KEY_SECRET="<your-api-key-secret>"

# Install the latest version of Astro CLI
$ curl -sSL install.astronomer.io | sudo bash -s

# Build your Astro project into a Docker image and push the image to your Deployment
$ astro deploy
info

All image-only templates use Astro CLI v1.0+ to deploy CI/CD pipelines. These templates will not work if you use the astrocloud executable. To upgrade, see Install the Astro CLI.

DAG-based workflows

caution

The features used in this workflow are in Public Preview.

The DAG-based workflow uses the --dags flag in the Astro CLI to enable a faster way to push DAG changes to Astro. These CI/CD pipelines deploy your DAGs only when files in your dags folder are modified, and they deploy the rest of your Astro project as a Docker image when other files or directories are modified. For more information about the benefits of this workflow, see Deploy DAGs only.

CI/CD templates that use the DAG-based workflow do the following:

  • Access Deployment API key credentials. These credentials must be set as OS-level environment variables named ASTRONOMER_KEY_ID and ASTRONOMER_KEY_SECRET.
  • Install the latest version of the Astro CLI.
  • Determine which files were updated by the commit:
    • If only DAG files in the dags folder have changed, run astro deploy --dags. This pushes your dags folder to your Deployment.
    • If any file not in the dags folder has changed, run astro deploy. This triggers two subprocesses. One that creates a Docker image for your Astro project, authenticates to Astro using your Deployment API key, and pushes the image to your Deployment. A second that pushes your dags folder to your Deployment.

This workflow is equivalent to the following bash script:

# Set Deployment API key credentials as environment variables
export ASTRONOMER_KEY_ID="<your-api-key-id>"
export ASTRONOMER_KEY_SECRET="<your-api-key-secret>"
export DAG_FOLDER="<path to dag folder ie. dags/>"

# Install the latest version of Astro CLI
curl -sSL install.astronomer.io | sudo bash -s

# Determine if only DAG files have changes
files=$(git diff --name-only HEAD^..HEAD)
dags_only=1

for file in $files; do
if [[ $file != "$DAG_FOLDER"* ]]; then
echo "$file is not a dag, triggering a full image build"
dags_only=0
break
fi
done

# If only DAGs changed deploy only the DAGs in your 'dags' folder to your Deployment
if [ $dags_only == 1 ]
then
astro deploy --dags
fi

# If any other files changed build your Astro project into a Docker image, push the image to your Deployment, and then push and DAG changes
if [ $dags_only == 0 ]
then
astro deploy
fi
info

All CI/CD pipelines that use the DAG-based deployment method require Astro CLI v1.7 or later to deploy. All CI/CD templates in this document automatically install the latest version of the Astro CLI. If your CI/CD pipeline does not automatically install the latest version of the Astro CLI, make sure to specify version 1.7 or later.

GitHub Actions (Image-only deploys)

To automate code deploys to a Deployment using GitHub Actions, complete the following setup in a Git-based repository that hosts an Astro project:

  1. Set the following as GitHub secrets:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. In your project repository, create a new YAML file in .github/workflows that includes the following configuration:

    name: Astronomer CI - Deploy code

    on:
    push:
    branches:
    - main

    env:
    ## Sets Deployment API key credentials as environment variables
    ASTRONOMER_KEY_ID: ${{ secrets.ASTRONOMER_KEY_ID }}
    ASTRONOMER_KEY_SECRET: ${{ secrets.ASTRONOMER_KEY_SECRET }}

    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: checkout repo
    uses: actions/checkout@v3
    - name: Deploy to Astro
    run: |
    curl -sSL install.astronomer.io | sudo bash -s
    astro deploy

GitHub Actions (DAG-based deploys)

The following templates are examples of how to implement DAG-only deploys in GitHub Actions. These templates can be modified to run on other CI/CD tools.

To automate code deploys to a Deployment using GitHub Actions, complete the following setup in a Git-based repository that hosts an Astro project:

  1. Set the following as GitHub secrets:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. In your project repository, create a new YAML file in .github/workflows that includes the following configuration:

    name: Astronomer CI - Deploy code
    on:
    push:
    branches:
    - main

    env:
    ## Sets Deployment API key credentials as environment variables
    ASTRONOMER_KEY_ID: ${{ secrets.ASTRONOMER_KEY_ID }}
    ASTRONOMER_KEY_SECRET: ${{ secrets.ASTRONOMER_KEY_SECRET }}
    DAG_FOLDER: <path to dag folder ie. dags/>

    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - name: checkout repo
    uses: actions/checkout@v3
    with:
    # Checkout as many commits as needed for the diff
    fetch-depth: 2
    # Determine if only DAGs have changes
    - name: Get Deployment Type
    run: |
    files=$(git diff --name-only HEAD^..HEAD)
    dags_only=1

    for file in $files; do
    if [[ $file != "$DAG_FOLDER"* ]]; then
    echo "$file is not a dag, triggering a full image build"
    dags_only=0
    break
    fi
    done

    echo "DAGS_ONLY=$dags_only" >> $GITHUB_OUTPUT
    id: deployment-type
    # If only DAGs changed, do a DAG-only deploy
    - name: DAG Deploy to Astro
    if: steps.deployment-type.outputs.DAGS_ONLY == 1
    run: |
    curl -sSL https://install.astronomer.io | sudo bash -s
    astro deploy --dags
    # If any other files changed, deploy the entire Astro project
    - name: Image and DAG Deploy to Astro
    if: steps.deployment-type.outputs.DAGS_ONLY == 0
    run: |
    curl -sSL https://install.astronomer.io | sudo bash -s
    astro deploy

This Github Actions script checks the diff between your current commit and your main branch when a commit is pushed to main. Make sure to customize the script for your specific use case.

Jenkins (Image-only deploys)

To automate code deploys to a single Deployment using Jenkins, complete the following setup in a Git-based repository hosting an Astro project:

  1. In your Jenkins pipeline configuration, add the following parameters:

    • ASTRONOMER_KEY_ID: Your Deployment API key ID
    • ASTRONOMER_KEY_SECRET: Your Deployment API key secret
    • ASTRONOMER_DEPLOYMENT_ID: The Deployment ID of your production deployment

    Be sure to set the values for your API credentials as secret.

  2. At the root of your Git repository, add a Jenkinsfile that includes the following script:

    pipeline {
    agent any
    stages {
    stage('Deploy to Astronomer') {
    when {
    expression {
    return env.GIT_BRANCH == "origin/main"
    }
    }
    steps {
    checkout scm
    sh '''
    curl -LJO https://github.com/astronomer/astro-cli/releases/download/v1.9.0/astro_1.9.0_linux_amd64.tar.gz
    tar -zxvf astro_1.9.0_linux_amd64.tar.gz astro && rm astro_1.9.0_linux_amd64.tar.gz
    ./astro deploy
    '''
    }
    }
    }
    }
    post {
    always {
    cleanWs()
    }
    }
    }

    This Jenkinsfile triggers a code push to Astro every time a commit or pull request is merged to the main branch of your repository.

Jenkins (DAG-based deploys)

Use the following template to implement DAG-only deploys with Jenkins.

  1. In your Jenkins pipeline configuration, add the following parameters:

    • ASTRONOMER_KEY_ID: Your Deployment API key ID
    • ASTRONOMER_KEY_SECRET: Your Deployment API key secret
    • ASTRONOMER_DEPLOYMENT_ID: The Deployment ID of your production deployment

    Be sure to set the values for your API credentials as secret.

  2. At the root of your Git repository, add a Jenkinsfile that includes the following script:

    pipeline {
    agent any
    stages {
    stage('Dag Only Deploy to Astronomer') {
    when {
    expression {
    return env.GIT_BRANCH == "origin/main"
    }
    }
    steps {
    checkout scm
    sh '''
    curl -LJO https://github.com/astronomer/astro-cli/releases/download/v1.9.0/astro_1.9.0_linux_amd64.tar.gz
    tar -zxvf astro_1.9.0_linux_amd64.tar.gz astro && rm astro_1.9.0_linux_amd64.tar.gz
    files=($(git diff-tree HEAD --name-only --no-commit-id))
    find="dags"
    if [[ ${files[*]} =~ (^|[[:space:]])"$find"($|[[:space:]]) && ${#files[@]} -eq 1 ]]; then
    ./astro deploy --dags;
    else
    ./astro deploy;
    fi
    '''
    }
    }
    }
    post {
    always {
    cleanWs()
    }
    }
    }

AWS CodeBuild

To automate code deploys to a single Deployment using AWS CodeBuild, complete the following setup in a Git-based repository hosting an Astro project:

  1. In your AWS CodeBuild pipeline configuration, add the following environment variables:

    • ASTRONOMER_KEY_ID: Your Deployment API key ID
    • ASTRONOMER_KEY_SECRET: Your Deployment API key secret
    • ASTRONOMER_DEPLOYMENT_ID: The Deployment ID of your production deployment

    Be sure to set the values for your API credentials as secret.

  2. At the root of your Git repository, add a buildspec.yml file that includes the following script:


    version: 0.2

    phases:
    install:
    runtime-versions:
    python: latest

    build:
    commands:
    - echo "${CODEBUILD_WEBHOOK_HEAD_REF}"
    - export ASTRONOMER_KEY_ID="${ASTRONOMER_KEY_ID}"
    - export ASTRONOMER_KEY_SECRET="${ASTRONOMER_KEY_SECRET}"
    - curl -sSL install.astronomer.io | sudo bash -s
    - astro deploy "${ASTRONOMER_DEPLOYMENT_ID}" -f

  3. In your AWS CodeBuild project, create a webhook event for the source provider where your Astro project is hosted, such as BitBucket or GitHub. When configuring the webhook, select an event type of PUSH.

Your buildspec.yml file now triggers a code push to an Astro Deployment every time a commit or pull request is merged to the main branch of your repository.

CircleCI

To automate code deploys to a Deployment using CircleCI, complete the following setup in a Git-based repository that hosts an Astro project:

  1. Set the following environment variables in a CircleCI context:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. Create a new YAML file in .circleci/config.yml that includes the following configuration:

    # Use the latest CircleCI pipeline process engine version.
    # See: https://circleci.com/docs/2.0/configuration-reference
    version: 2.1

    orbs:
    docker: circleci/docker@2.0.1
    github-cli: circleci/github-cli@2.0.0

    # Define a job to be invoked later in a workflow.
    # See: https://circleci.com/docs/2.0/configuration-reference/#jobs
    jobs:

    build_image_and_deploy:
    docker:
    - image: cimg/base:stable
    # Add steps to the job
    # See: https://circleci.com/docs/2.0/configuration-reference/#steps
    steps:
    - setup_remote_docker:
    version: 20.10.11
    - checkout
    - run:
    name: "Setup custom environment variables"
    command: |
    echo export ASTRONOMER_KEY_ID=${ASTRONOMER_KEY_ID} >> $BASH_ENV
    echo export ASTRONOMER_KEY_SECRET=${ASTRONOMER_KEY_SECRET} >> $BASH_ENV
    - run:
    name: "Deploy to Astro"
    command: |
    curl -sSL install.astronomer.io | sudo bash -s
    astro deploy -f

    # Invoke jobs with workflows
    # See: https://circleci.com/docs/2.0/configuration-reference/#workflows
    workflows:
    version: 2.1
    build-and-deploy-prod:
    jobs:
    - build_image_and_deploy:
    context:
    - <YOUR-CIRCLE-CI-CONTEXT>
    filters:
    branches:
    only:
    - main

Drone

To automate code deploys to a Deployment using a Docker-based Drone CI pipeline, complete the following setup in a Git-based repository that hosts an Astro project.

Prerequisites

This pipeline configuration requires:

  1. Set the following environment variables as repository-level secrets on Drone:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. In your Drone server, open your Astro project repository and go to Settings > General. Under Project Settings, turn on the Trusted setting.

  3. In the top level of your Git repository, create a file called .drone.yml that includes the following configuration:

    ---
    kind: pipeline
    type: docker
    name: deploy

    steps:
    - name: install
    image: debian
    commands:
    - apt-get update
    - apt-get -y install curl
    - curl -sSL install.astronomer.io | sudo bash -s
    - name: wait
    image: docker:dind
    volumes:
    - name: dockersock
    path: /var/run
    commands:
    - sleep 5
    - name: deploy
    image: docker:dind
    volumes:
    - name: dockersock
    path: /var/run
    commands:
    - astro deploy -f
    depends on:
    - wait

    environment:
    ASTRONOMER_KEY_ID:
    from_secret: ASTRONOMER_KEY_ID
    ASTRONOMER_KEY_SECRET:
    from_secret: ASTRONOMER_KEY_SECRET

    services:
    - name: docker
    image: docker:dind
    privileged: true
    volumes:
    - name: dockersock
    path: /var/run

    volumes:
    - name: dockersock
    temp: {}

    trigger:
    branch:
    - main
    event:
    - push

GitLab

info

If you use hosted runners, you might need certain tags or modifications based on your Gitlab runner. Speak with your Gitlab administrator and review the Gitlab Documentation to customize this template for your use case.

To automate code deploys to a Deployment using GitLab, complete the following setup in your GitLab repository that hosts an Astro project:

  1. In GitLab, go to Project Settings > CI/CD > Variables and set the following environment variables:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. Go to the Editor option in your project's CI/CD section and commit the following:

    ---
    astro_deploy:
    stage: deploy
    image: docker:latest
    services:
    - docker:dind
    variables:
    ASTRONOMER_KEY_ID: ${ASTRONOMER_KEY_ID}
    ASTRONOMER_KEY_SECRET: ${ASTRONOMER_KEY_SECRET}
    before_script:
    - apk add --update curl && rm -rf /var/cache/apk/*
    - apk add bash
    script:
    - (curl -sSL install.astronomer.io | bash -s)
    - astro deploy -f
    only:
    - main

Bitbucket

To automate code deploys to a Deployment using Bitbucket, complete the following setup in a Git-based repository that hosts an Astro project:

  1. Set the following environment variables as Bitbucket pipeline variables:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. Create a new YAML file in bitbucket-pipelines.yml at the root of the repository that includes the following configuration:

    pipelines:
    pull-requests: # The branch pattern under pull requests defines the *source* branch.
    dev:
    - step:
    name: Deploy to Production
    deployment: Production
    script:
    - curl -sSL install.astronomer.io | sudo bash -s
    - astro deploy
    services:
    - docker

Azure DevOps

To automate code deploys to a Deployment using Azure DevOps, complete the following setup in a Git-based repository that hosts an Astro project:

  1. Set the following environment variables as DevOps pipeline variables:

    • ASTRONOMER_KEY_ID = <your-key-id>
    • ASTRONOMER_KEY_SECRET = <your-key-secret>
  2. Create a new YAML file in astro-devops-cicd.yaml at the root of the repository that includes the following configuration:

    trigger:
    - main

    pr: none

    stages:
    - stage: deploy
    jobs:
    - job: deploy_image
    pool:
    vmImage: 'Ubuntu-latest'
    steps:
    - script: |
    curl -sSL install.astronomer.io | sudo bash -s
    astro deploy
    env:
    ASTRONOMER_KEY_ID: $(ASTRONOMER_KEY_ID)
    ASTRONOMER_KEY_SECRET: $(ASTRONOMER_KEY_SECRET)