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

如何在Azure Pipeline中将Azure文件共享挂载为Web应用容器卷

配置Azure文件共享挂载到容器的具体方法

你的现有Pipeline仅包含镜像构建推送阶段,挂载Azure文件共享属于部署阶段的配置操作,需要添加部署任务并通过应用设置指定挂载规则,具体修改如下:

1. 修正原YAML语法错误

原YAML中variables块后缩进写- stage属于语法错误,需将stage调整为与trigger、resources、variables同级的顶层元素。

2. 添加部署阶段与挂载配置

在构建阶段后新增部署阶段,使用AzureWebAppContainer@1任务完成镜像部署,并通过appSettings参数配置Azure文件共享的挂载规则:

trigger:
- master

resources:
- repo: self

variables:
  # 原有构建变量
  dockerRegistryServiceConnection: 'xxxx'
  imageRepository: 'xxx'
  containerRegistry: 'xxx.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  vmImageName: 'ubuntu-latest'
  # 新增部署所需变量
  webAppName: '你的Azure App Service名称'
  azureSubscription: '你的Azure服务连接名称'
  storageAccountName: '你的存储账户名称'
  fileShareName: '你的文件共享名称'
  # 存储账户密钥建议存为Azure DevOps安全变量,此处引用变量
  storageAccountKey: '$(StorageAccountKey)'

stages:
# 构建阶段(修正缩进)
- stage: Build_Master
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
  displayName: Build master
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          master

# 新增部署阶段
- stage: Deploy_Master
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
  displayName: Deploy master to App Service
  dependsOn: Build_Master
  jobs:
  - job: Deploy
    displayName: Deploy
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: AzureWebAppContainer@1
      displayName: Deploy to Azure App Service Container
      inputs:
        azureSubscription: $(azureSubscription)
        appName: $(webAppName)
        containers: '$(containerRegistry)/$(imageRepository):master'
        # 配置应用设置实现Azure文件共享挂载
        appSettings: |
          - WEBSITES_ENABLE_APP_SERVICE_STORAGE=true
          - Mounts=[{\"source\":\"$(fileShareName)\",\"target\":\"/var/www/html/shared_storage\",\"type\":\"azureFile\",\"accountName\":\"$(storageAccountName)\",\"accountKey\":\"$(storageAccountKey)\"}]

关键配置说明

  • 存储账户密钥安全处理:禁止在YAML中明文写入storageAccountKey,需在Azure DevOps的库-变量组或流水线变量中创建加密安全变量,再通过$(StorageAccountKey)引用。
  • Mounts参数格式:必须为JSON数组格式,注意转义双引号(\"),各字段含义:
    • source:你的Azure文件共享名称
    • target:容器内挂载路径(即/var/www/html/shared_storage)
    • type:固定为azureFile
    • accountName:存储账户名称
    • accountKey:存储账户访问密钥
  • WEBSITES_ENABLE_APP_SERVICE_STORAGE:必须设置为true,启用App Service的存储挂载功能。

验证挂载生效

部署完成后可通过两种方式验证:

  1. 登录Azure门户,进入对应App Service,在容器设置的挂载列表中确认配置的文件共享。
  2. 进入容器SSH终端,执行df -h命令,查看/var/www/html/shared_storage路径是否挂载成功。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 00:30:23