使用Terraform工作流(而不是Terraform )来部署GCP计算引擎VM服务器的Github操作。我的工作流当前在Terraform格式处理中出错。如果我使用Terraform和/或直接在Google中部署,我知道的main.tf可以工作。
我的工作流错误:

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 }}最后我的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"
}
}发布于 2022-07-19 14:16:14
根据我的评论和Matt的解释,fmt选项可能有点违背直觉1
-check -检查输入是否已格式化。如果所有输入都被正确格式化,则退出状态为0,否则为非零。
为了避免这种情况,您应该在没有任何其他选项的情况下运行terraform fmt。您还可以在回购中引入预提交挂钩,这将执行Terraform代码的格式设置,这应该足以避免将来发生类似的错误。
https://stackoverflow.com/questions/73037051
复制相似问题