【发布时间】:2022-10-25 20:37:08
【问题描述】:
我已经准备好用于在 Azure Cloud 上构建一些 IaC 基础架构的 terraform 模块。目前我正在准备发布 Azure Pipelines。我已经在我的管道中创建了所有任务。我想在多个环境中使用 terraform 部署,例如:dev、test、prod,所以我决定使用 terraform 工作空间。我在我的发布管道中为开发环境准备了以下任务:terraform init、terraform validate、terraform plan、terraform apply。问题是我不确定如何创建 terraform 工作区然后使用 terraform 工作区。当我从经典编辑器创建 azure devops 管道时,有一些任务用于创建 terraform 工作区和选择 terraform 工作区操作,但它们没有按预期工作。我正在尝试使用管道变量来存储 terraform 工作区名称,我正在使用 terraform env 变量 TF_WORKSPACE。由 Azure Devops YAML 格式的 MS Tasks 提供,如下所示。
terraform 工作区新任务
steps:
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: 'terraform workspace'
inputs:
command: workspace
workingDirectory: '$(System.DefaultWorkingDirectory)/Project-Name/drop/Terraform'
workspaceSubCommand: new
skipExistingWorkspace: true
terraform 工作区选择任务
variables:
TF_WORKSPACE: 'Development'
steps:
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: 'terraform workspace'
inputs:
command: workspace
workingDirectory: '$(System.DefaultWorkingDirectory)/Project-Name/drop/Terraform'
workspaceName: '-TF_WORKSPACE $(TF_WORKSPACE)'
【问题讨论】:
-
有两个与工作空间相关的 terraform 命令。 1. 要创建工作区,请使用
terraform workspace new <name>2. 要选择工作区,请使用terraform workspace select <name>。 -
嗨@MarkoE,是的,我知道有两个命令用于创建 terraform 工作区,但在 azure devops 管道中没有为
terraform workspace new WorkspaceName准备任务的选项 -
有一个选项:它被称为
script任务。你可以运行任何你想要的 CLI 命令。确实没有必要使用市场上的这些包装器扩展。他们什么也得不到。
标签: azure-devops terraform azure-pipelines terraform-workspace