Main Push Pipeline

Note: Premium video content requires a subscription.

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


  - main # once merged into main `terraspace up` runs on dev env

  vmImage: ubuntu-latest

# Note: common-variables group must already exist.
- group: common-variables

- task: UseRubyVersion@0
    versionSpec: '>= 3.1'

- script: |
  displayName: install terraspace

- script: |
    export TS_TOKEN=$(TS_TOKEN)
    # Uncomment export INFRACOST_API_KEY if using cost estimates
    terraspace up demo
  displayName: terraspace up

Pipeline Scripts

The trigger: ["main"] tells Azure to run this pipeline whenever there’s a commit pushed to main. The pool specifies the VM image to use. In this case: Ubuntu.

The steps section contains the meat of the logic. The Azure Pipelines YAML Schema docs are useful.

In the first step, we use a task to install Ruby. Some pre-built tasks are provided by Azure or even 3rd parties.

We then use two script steps to run bash scripts. The first script calls the supporting install script. The script looks something like this. We’re sumarizing it for clarity.



# install terraform
git clone ~/.tfenv
export PATH="$HOME/.tfenv/bin:$PATH"
tfenv install latest
tfenv use latest
terraform --version

# install terraspace
bundle install
terraspace --version

The script installs terraspace and dependencies like terraform.

The second script step exports some required env variables and then calls

terraspace up demo -y

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

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

Once the YAML files are push, we’ll be able to create the initial pipelines.

Creating the Pipeline with the Azure Portal

To create the pipeline:

  1. Click on the New Pipeline Button. Note, if you don’t have any pipelines yet, there’ll be another button that says something like “Create First Pipeline” instead.
  2. For “Where is your code”, select Azure Repos Git
  3. Choose the Repo with your code. In the example shown below, it’s called infra-ci, yours will be different.
  4. For “Configure Your Pipeline”, select Existing Azure Pipelines YAML file
  5. Select an existing YAML file, choose the /.azure/pipelines/dev-up.yml file
  6. Then click Run
  7. It is useful to rename the pipeline right away to something better. Rename it to dev-up
  8. Click on the pipeline to see the logs

Here’s the New Pipeline button.

Where’s your code.

Choose the Repo with your code.

Select Existing Azure Pipelines YAML file.

Select existing YAML file /.azure/pipelines/dev-up.yml.

Click Run

Here’s the rename button.

Here’s the rename form.

Here are the logs.

The logs show that a random_pet resource was created. 🐶🎉

Creating the Pipeline with the az cli

Tip: You can also create the pipeline with the az CLI

az devops configure --defaults organization= project=PROJECT
az devops configure --list
az pipelines create --name dev-up --yml-path .azure/pipelines/dev-up.yml # starts pipeline immediately

Next, we’ll look at the Branch Pipeline.

More tools: