You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Pipeline中通过Terraform将文件上传至Azure Storage

实现流程前置准备
  • 提前在Azure DevOps项目中配置好Azure Resource Manager服务连接,授权Pipeline访问你的Azure订阅
  • 提前配置Terraform远程状态存储(推荐使用Azure Blob存储tfstate文件,避免本地状态丢失导致资源管理混乱)
  • 确认Repo中Files文件夹的相对路径,后续Pipeline和Terraform配置都会调用该路径

步骤1:编写Terraform配置文件

将以下配置保存为main.tf,放到Repo中任意目录(建议单独存放在terraform文件夹下统一管理)

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
  }
  # 远程状态存储配置,和你提前创建的tfstate存储资源对应即可
  backend "azurerm" {
    resource_group_name  = "tfstate所在资源组名"
    storage_account_name = "tfstate存储账户名"
    container_name       = "tfstate"
    key                  = "config-files-upload.tfstate"
  }
}

provider "azurerm" {
  features {}
}

# 若已经有现成的存储账户和容器,可将下面两个resource改为data源调用现有资源即可
resource "azurerm_resource_group" "config_rg" {
  name     = "config-files-rg"
  location = "chinanorth3"
}

resource "azurerm_storage_account" "config_sa" {
  name                     = "configstorage001" # 存储账户名全局唯一,按需修改
  resource_group_name      = azurerm_resource_group.config_rg.name
  location                 = azurerm_resource_group.config_rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_container" "config_container" {
  name                  = "config-files"
  storage_account_name  = azurerm_storage_account.config_sa.name
  container_access_type = "private"
}

# 核心配置:递归遍历Files文件夹下所有文件,批量创建Blob
locals {
  # 路径对应Repo中Files文件夹的相对位置,按需调整
  config_file_list = fileset(path.root, "Files/**/*")
}

resource "azurerm_storage_blob" "config_files" {
  for_each               = local.config_file_list
  # 去掉Files前缀,直接把文件上传到容器根目录;如果要保留目录结构直接赋值each.value即可
  name                   = replace(each.value, "Files/", "")
  storage_account_name   = azurerm_storage_account.config_sa.name
  storage_container_name = azurerm_storage_container.config_container.name
  type                   = "Block"
  # 根据你的文件类型调整,比如JSON格式改为application/json
  source_content_type    = "text/plain"
  source                 = each.value
}

该配置会自动对比本地文件和Blob的内容差异,新增/修改文件时只会同步变更的内容,不会重复上传未修改的文件。


步骤2:编写Azure DevOps Pipeline配置

将以下配置保存为azure-pipelines.yaml放到Repo根目录,在Azure DevOps中导入该yaml创建Pipeline即可

trigger:
- main # 对应你的主干分支名,修改为实际分支即可

pool:
  vmImage: 'ubuntu-latest' # 建议用Linux代理执行,兼容性和速度都优于Windows代理

steps:
# 拉取全量Repo代码,包含Files文件夹和Terraform配置文件
- checkout: self
  fetchDepth: 1

# 安装指定版本的Terraform
- task: TerraformInstaller@0
  displayName: '安装Terraform'
  inputs:
    terraformVersion: '1.5.7' # 替换为你常用的稳定版本即可

# 执行Terraform初始化
- task: TerraformTaskV3@3
  displayName: 'Terraform Init'
  inputs:
    provider: 'azurerm'
    command: 'init'
    # 填写Terraform配置文件在Repo中的路径,对应你存放main.tf的目录
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    backendServiceArm: '你提前创建的Azure RM服务连接名称'
    backendAzureRmResourceGroupName: 'tfstate所在资源组名'
    backendAzureRmStorageAccountName: 'tfstate存储账户名'
    backendAzureRmContainerName: 'tfstate'
    backendAzureRmKey: 'config-files-upload.tfstate'

# 执行Terraform Plan预览变更
- task: TerraformTaskV3@3
  displayName: 'Terraform Plan'
  inputs:
    provider: 'azurerm'
    command: 'plan'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    environmentServiceNameAzureRM: '你提前创建的Azure RM服务连接名称'

# 执行Terraform Apply同步文件到Blob
- task: TerraformTaskV3@3
  displayName: 'Terraform Apply'
  inputs:
    provider: 'azurerm'
    command: 'apply'
    commandOptions: '-auto-approve'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    environmentServiceNameAzureRM: '你提前创建的Azure RM服务连接名称'

验证与后续使用

Pipeline执行成功后,登录Azure Portal打开对应的存储账户容器,即可看到所有Files文件夹下的配置文件已经同步上传。后续你修改Repo中Files下的文件,重新触发Pipeline就会自动同步变更到Blob,可直接对接ADF做后续的数据处理。


注意事项
  • 如果你的配置文件是二进制格式,需要修改Terraform中source_content_type参数为对应的MIME类型
  • 建议把存储账户密钥、服务连接密钥等敏感信息放到Azure DevOps的变量组中,设置为保密变量,不要明文写在yaml配置里

内容的提问来源于stack exchange,提问作者Kenny_I

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 01:45:02