Main Push Workflow

Note: Premium video content requires a subscription.

We’ll review the when Main Push Workflow. This is what happens when a push is made to the main branch. Here’s the relevant section.

.circleci/config.yml

parameters:
  manual:
    type: boolean
    default: false

jobs:
  dev_up:
    docker:
      - image: cimg/ruby:3.1.0
    environment: *common_env_vars
    steps:
      - checkout
      - run: .circleci/bin/install
      - run: terraspace up demo -y

workflows:
  dev_up:
    when:
      not: << pipeline.parameters.manual >>
    jobs:
      - dev_up:
          filters:
            branches:
              only: main

Pipeline Parameter

The config defines a manual pipeline parameter. We’re using where.not in the workflows section to tell CircleCI to run the dev_plan when manual=false.

Build Script

The pipeline uses the cimg/ruby:3.1.0 Docker image. This is a Docker image managed by CircleCI.

The first part installs terraform and terraspace with a supporting script that looks something like this:

.circleci/bin/install

#!/bin/bash
# install terraform
git clone https://github.com/tfutils/tfenv.git ~/.tfenv
export PATH="$HOME/.tfenv/bin:$PATH"
tfenv install 1.5.5 # do not use later than 1.5.5
tfenv use 1.5.5

# install terraspace
bundle install
terraspace --version

After the install script, it calls the

terraspace up demo -y

Essentially, terraspace up automatically runs for dev infrastructure for every push to main.

CircleCI Notes

Environment Variables

We should set these environment variables. Note that the AWS variables are required only if you’re using terraspace_plugin_aws.

  1. Go to Project Settings.
  2. Go to Environment Variables
  3. Add the variables, , IE: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, and TS_TOKEN, GH_TOKEN, etc.

Terraspace Command

At the very end, the terraspace up demo -y command will run to deploy the demo stack. You can customize this command or add additional commands. IE: You probably want to use terraspace all up if you prefer.

Commit and Push

Let’s commit and push the files.

git add .
git commit -m 'add ci'
git push -u origin main

This starts the build process immediately.

Results

After the job starts, you’ll see something like the following. You may have to refresh to see the job running.

You can see that a resource was created.

Next, we’ll look at the Branch Workflow.

More tools: