我有使用 ci/cd 管道部署简单存储帐户或虚拟机的 terraform 代码,在我的管道中,我试图获取要部署的资源的成本,我已经下载了 infracost 的扩展以获取资源成本在应用 terraform 之前,我需要资源列表和成本。到 terraform plan 为止,它已成功执行,tfplan 文件也创建了,但 terraform 显示我得到的是
│ Error: Failed to load plugin schemas
│ Error while loading schemas for plugin components: Failed to obtain
│ provider schema: Could not load the schema for provider
│ registry.terraform.io/hashicorp/azurerm: failed to instantiate provider
│ "registry.terraform.io/hashicorp/azurerm" to obtain schema: unavailable
│ provider "registry.terraform.io/hashicorp/azurerm".
即使 tfplan 文件存在,下面是我的代码
trigger: none
pool:
vmImage: 'ubuntu-latest'
variables:
- group: GenAISecrets
parameters:
- name: OrderID
displayName: Please Provide the id:-
type: object
stages:
- stage: InitializeAndValidate
displayName: "Terraform Validate and Plan"
jobs:
- job: terraform_plan
displayName: "Terraform Validate and Plan Job"
steps:
# Step 1: Install Terraform
- task: TerraformInstaller@0
inputs:
terraformVersion: 'latest'
# Step 2: Initialize Terraform with Backend Configuration
- task: TerraformTaskV3@3
displayName: "Terraform Init"
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: 'terraform'
backendServiceArm: 'TEST'
backendAzureRmResourceGroupName: $(ResourceGroup)
backendAzureRmStorageAccountName: $(StorageAccount)
backendAzureRmContainerName: $(Container)
backendAzureRmKey: '${{ parameters.OrderID }}.tfstate'
# Step 3: Terraform Validate to Check Configuration Files
- task: TerraformTaskV3@3
displayName: "Terraform Validate"
inputs:
provider: 'azurerm'
command: 'validate'
- task: TerraformTaskV3@3
displayName: "Terraform Plan"
inputs:
provider: 'azurerm'
command: 'plan'
workingDirectory: 'terraform'
environmentServiceNameAzureRM: 'TEST'
commandOptions: '-var-file=terraform.tfvars -out=$(System.DefaultWorkingDirectory)/tfplan'
- script: |
terraform show -json $(System.DefaultWorkingDirectory)/tfplan > planned_resources.json ##facing error here
displayName: "Export Planned Resources to JSON"
# Step 4: Infracost Cost Estimation
- task: InfracostSetup@2
displayName: "Estimate Costs with Infracost"
inputs:
planPath: "$(System.DefaultWorkingDirectory)/tfplan" # Path to the Terraform plan
usageFile: "infracost-usage.yml"
apiKey: $(infracostApiKey) # API Key from Azure DevOps secrets
outputFormat: "json" # Output format (e.g., json, table, etc.)
outputPath: "$(System.DefaultWorkingDirectory)/infracost.json"
- script: |
echo "Files after terraform plan:"
ls -al
displayName: "List Files After Plan"
# Step 6: Publish Cost Estimation Artifact
- task: PublishPipelineArtifact@1
inputs:
targetPath: "$(System.DefaultWorkingDirectory)/infracost.json"
artifactName: "CostEstimation"
publishLocation: "pipeline"
displayName: "Publish Cost Estimation"
这里有什么问题。请协助我解决这个问题。当我在本地尝试和测试时,它有效,但在管道中无法获得地形显示和成本。
首先,当前Terraform任务的最新版本是
TerraformTaskV4@4
,您可以考虑升级到新的任务版本。
在我的管道中,我尝试使用下面的示例,它可以正常工作。
variables:
ArmConnection: 'xxxx'
ResourceGroup: 'xxxx'
StorageAccount: 'xxxx'
BlobContainer: 'xxxx'
ResourceName: 'xxxx'
steps:
- task: TerraformInstaller@1
displayName: 'Install Terraform'
- task: TerraformTaskV4@4
displayName: 'Terraform init'
inputs:
provider: 'azurerm'
command: 'init'
backendAzureRmUseEntraIdForAuthentication: true
backendServiceArm: $(ArmConnection)
backendAzureRmResourceGroupName: $(ResourceGroup)
backendAzureRmStorageAccountName: $(StorageAccount)
backendAzureRmContainerName: $(BlobContainer)
backendAzureRmKey: '$(ResourceName).tfstate'
- task: TerraformTaskV4@4
displayName: 'Terraform validate'
inputs:
provider: 'azurerm'
command: 'validate'
- task: TerraformTaskV4@4
displayName: 'Terraform plan'
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: >
-out=tfplan
-var="resource_group_name=$(ResourceGroup)"
-var="resource_name=$(ResourceName)"
environmentServiceNameAzureRM: $(ArmConnection)
- bash: |
terraform show -json tfplan > planned_resources.json
displayName: 'Export Planned Resources to JSON'
- task: CopyFiles@2
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)'
Contents: 'planned_resources.json'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifact: 'drop'
publishLocation: 'pipeline'
. . .
确保“Terraform plan”步骤已成功生成并将
tfplan
文件保存到当前工作目录或指定路径中。
然后执行 '
terraform show
' 命令从 planned_resources.json
文件生成 JSON 文件(tfplan
)。
可以看到文件
planned_resources.json
已成功生成。