Matt - Block-Farms.io
Matt - Block-Farms.io

Reputation: 584

How to deploy GCP compute vm server using github actions terraform

Github actions with a Terraform workflow (not Terraform cloud) to deploy a GCP compute engine VM server. My workflow currently errors out on the Terraform Format process. The main.tf I know works if I deploy using Terraform Cloud and/or directly in the Google Cloud SDK.

My Workflow error:

enter image description here

The workflow.yaml:

name: 'Terraform CI'

on:
  push:
    branches:
    - main
  pull_request:

jobs:
  terraform:
    name: 'Terraform'
    runs-on: ubuntu-latest

    # Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
    defaults:
      run:
        shell: bash

    steps:
    # Checkout the repository to the GitHub Actions runner
    - name: Checkout
      uses: actions/checkout@v2

    # Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v1


    # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
    - name: Terraform Init
      run: terraform init
      env:
        GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}

    # Checks that all Terraform configuration files adhere to a canonical format
    - name: Terraform Format
      run: terraform fmt -check

    # Generates an execution plan for Terraform
    - name: Terraform Plan
      run: terraform plan
      env:
        GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}

      # On push to main, build or change infrastructure according to Terraform configuration files
      # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
    - name: Terraform Apply
      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
      run: terraform apply -auto-approve
      env:
        GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}

And finally my main.tf:

provider "google" {
  project = "test-project-1"
  region  = "us-west1"
  zone    = "us-west1-b"
}

resource "google_compute_instance" "default" {
  name         = "test-main-node"
  machine_type = "custom-4-8192"

  boot_disk {
    initialize_params {
      image = "ubuntu-os-cloud/ubuntu-minimal-1804-lts"
      size = "10"
      type = "pd-ssd"
    }
  }

  network_interface {
    network = "default"
  }
}

Upvotes: 0

Views: 647

Answers (1)

Marko E
Marko E

Reputation: 18148

As per my comment and Matt Schuchard's explanation, the fmt option might be a bit counterintuitive [1]:

-check - Check if the input is formatted. Exit status will be 0 if all input is properly formatted and non-zero otherwise.

In order to avoid that, you should run terraform fmt without any other option. You might as well introduce pre-commit hooks in your repo which would perform formatting of your Terraform code and that should be good enough to avoid any errors like this in the future.


[1] https://www.terraform.io/cli/commands/fmt#check

Upvotes: 1

Related Questions