错误:不允许应用具有 VCS 连接的工作区

Gra*_*tic 6 terraform github-actions

Error: Apply not allowed for workspaces with a VCS connection
Run Code Online (Sandbox Code Playgroud)

我在尝试通过 Github Actions 应用 terraform 计划时收到此错误。

Github Action(地形应用)

- name: Terraform Apply Dev
  id: apply_dev
  if: github.ref == 'refs/heads/master' && github.event_name == 'push'
  run: TF_WORKSPACE=dev terraform apply -auto-approve deployment/
Run Code Online (Sandbox Code Playgroud)

Terraform 工作区 该工作区是在 Terraform Cloud 上创建的,Version control workflow称为app-infra-dev

地形后端

# The configuration for the `remote` backend.
terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "my-org-name"
    workspaces {
      prefix = "app-infra-"
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

因此,因为我调用了我的工作区app-infra-dev,所以我在后端文件中的工作区前缀是在我的 GH 操作中app-infra-设置TF_WORKSPACE=dev的。我本来希望这足以让它发挥作用。

谢谢你的帮助!

Von*_*onC 0

if: github.ref == 'refs/heads/master' && github.event_name == 'push'您可以考虑在 GitHub 事件本身上触发 apply,而不是,如本例所示

name: terraform apply

# Controls when the action will run.
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ master ]
Run Code Online (Sandbox Code Playgroud)

在该示例中,您可以看到 terraform apply 在 terraform 命令序列末尾使用:


# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  apply:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      - uses: hashicorp/setup-terraform@v1
        with:
          terraform_wrapper: true
          terraform_version: 0.14.0

      # Runs a single command using the runners shell
      - name: create credentials
        run: echo "$GOOGLE_APPLICATION_CREDENTIALS" > credentials.json
        env:
          GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}

      - name: export GOOGLE_APPLICATION_CREDENTIALS
        run: |
          echo "GOOGLE_APPLICATION_CREDENTIALS=`pwd`/credentials.json" >> $GITHUB_ENV
      - name: terraform init
        run: terraform init

      - name: terraform workspace new
        run: terraform workspace new dev-tominaga
        continue-on-error: true

      - name: terraform workspace select
        run: terraform workspace select dev-tominaga
        continue-on-error: true

      - name: terraform init
        run: terraform init

      - name: terraform workspace show
        run: terraform workspace show

      - name: terraform apply
        id: apply
        run: terraform apply -auto-approve
Run Code Online (Sandbox Code Playgroud)

检查您是否可以将其适应您的工作流程。