你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

自动发布用于持续集成和持续交付 (CI/CD)

适用于: Azure 数据工厂 Azure Synapse Analytics

提示

Microsoft Fabric 中的 Data Factory 是下一代 Azure 数据工厂,具有更加简化的架构、内置人工智能和新功能。 如果不熟悉数据集成,请从Fabric数据工厂开始。 现有 ADF 工作负载可以升级到 Fabric,以跨数据科学、实时分析和报告访问新功能。

注意

Synapse Analytics 也支持 CI/CD。 有关详细信息,请参阅 Synapse Analytics CI/CD 文档。

概述

持续集成是这样一种做法:自动测试对代码库所做的每项更改。 持续交付尽早执行在持续集成期间发生的测试并将更改推送到暂存系统或生产系统。

在Azure 数据工厂中,CI/CD 意味着将数据工厂管道从开发、测试和生产等环境移到另一个环境。 数据工厂使用 Azure 资源管理器 模板(ARM 模板)存储各种数据工厂实体的配置,例如管道、数据集和数据流。

可通过两种建议的方式将数据工厂提升到另一个环境:

  • 通过将Data Factory与Azure Pipelines集成实现自动化部署。
  • 使用数据工厂用户体验与 Azure 资源管理器的集成手动上传 ARM 模板。

有关详细信息,请参阅 Azure 数据工厂 中的连续集成和交付。

本文重点介绍持续部署改进以及 CI/CD 的自动发布功能。

持续部署改进

自动发布功能采用数据工厂用户体验中的“全部验证”和“导出 ARM 模板”功能,并通过正式发布的 npm 包 @microsoft/azure-data-factory-utilities 使逻辑可供使用。 出于此原因,可以通过编程方式触发这些操作,而无需转到数据工厂 UI 并手动选择按钮。 这一能力为您的CI/CD流水线提供了更真实的持续集成体验。

注意

请务必使用 Node.js 版本20.x及其兼容版本,以避免因封包与旧版本不兼容而导致的错误。

当前 CI/CD 流

  1. 每个用户在其专用分支中进行更改。
  2. 不允许推向主主。 用户必须创建拉取请求才能进行更改。
  3. 用户必须加载数据工厂 UI,选择“发布”以将更改部署到数据工厂,然后在发布分支中生成 ARM 模板。
  4. 每次将新更改推送到发布分支时,DevOps 发布管道就会配置为创建新版本并部署 ARM 模板。

显示当前 CI/CD 流的关系图。

手动步骤

在当前 CI/CD 流中,用户体验是创建 ARM 模板的中介。 因此,用户必须转到数据工厂 UI,手动选择“发布”以启动 ARM 模板生成,然后将其放在发布分支中。

新的 CI/CD 流

  1. 每个用户在其专用分支中进行更改。
  2. 不允许推向主主。 用户必须创建拉取请求才能进行更改。
  3. Azure DevOps 流水线构建在每次主提交时都会被触发。 如果验证成功,则它将验证资源并将 ARM 模板生成为项目。
  4. 每次有新的生成可用时,DevOps 发布管道就会配置为创建新版本并部署 ARM 模板。

显示新的 CI/CD 流的关系图。

有何变化?

  • 你现在拥有了一个使用 DevOps 构建流水线的构建流程。
  • 构建流水线使用 ADFUtilities (@microsoft/azure-data-factory-utilities) npm 包,该包验证所有资源并生成 ARM 模板。 这些模板可以是单一模板,也可以是链接模板。
  • 构建流水线验证数据工厂资源,并生成ARM模板,而不是数据工厂界面(发布 按钮)。
  • DevOps 发布定义现在会消耗这个新的构建流水线,而不是 Git 的产物。

注意

可以继续使用现有机制(即 adf_publish 分支),也可以使用新流。 两者都受到支持。

软件包概述

包目前支持以下两个命令:

  • 导出 ARM 模板
  • 验证

导出 ARM 模板

运行 npm run build export <rootFolder> <factoryId> [outputFolder] 以使用给定文件夹的资源导出 ARM 模板。 该命令还会在生成 ARM 模板前进行验证检查。 这里有一个使用名为 testResourceGroup 的资源组的示例:

npm run build export C:\DataFactories\DevDataFactory /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testResourceGroup/providers/Microsoft.DataFactory/factories/DevDataFactory ArmTemplateOutput
  • RootFolder 是表示数据工厂资源所在位置的必填字段。
  • FactoryId是一个必填字段,它以 /subscriptions/<subId>/resourceGroups/<rgName>/providers/Microsoft.DataFactory/factories/<dfName> 格式表示数据工厂资源 ID。
  • OutputFolder 是一个可选参数,它指定用于保存所生成 ARM 模板的相对路径。

现在,仅停止/启动更新后的触发器功能已普遍可用,并已合并到前一个命令中。

注意

生成的 ARM 模板未发布到工厂的实时版本。 应使用 CI/CD 管道进行部署。

验证

运行 npm run build validate <rootFolder> <factoryId> 以验证给定文件夹的所有资源。 下面是一个示例:

npm run build validate C:\DataFactories\DevDataFactory /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testResourceGroup/providers/Microsoft.DataFactory/factories/DevDataFactory
  • RootFolder 是表示数据工厂资源所在位置的必填字段。
  • FactoryId是一个必填字段,它以 /subscriptions/<subId>/resourceGroups/<rgName>/providers/Microsoft.DataFactory/factories/<dfName> 格式表示数据工厂资源 ID。

创建 Azure 管道

虽然 npm 包可以通过多种方式使用,但其中一个主要好处是通过 Azure Pipelines 来使用。 每次合并到协作分支时,都可以触发一个流水线,先验证所有代码,然后将ARM模板导出为构建 工件 ,供发布流水线调用。 它与当前CI/CD流程的不同之处在于,你将 发布流程指向这个产物,而不是现有 adf_publish 的分支。

执行以下步骤以便开始:

  1. 打开Azure DevOps项目,转到 Pipelines。 选择“新建管道”。

    显示“新管道”按钮的屏幕截图。

  2. 选择要在其中保存管道 YAML 脚本的存储库。 把它保存在和你的Data Factory资源同一个仓库里的构建文件夹里。 确保存储库中有包含包名称的 package.json 文件,如以下示例中所示:

    {
        "scripts":{
            "build":"node node_modules/@microsoft/azure-data-factory-utilities/lib/index"
        },
        "dependencies":{
            "@microsoft/azure-data-factory-utilities":"^1.0.0"
        }
    } 
    
  3. 选择启动管道. 如果已上传或合并 YAML 文件(如以下示例中所示),也可以直接指向该文件并对其进行编辑。

    显示“初学者管道”的屏幕截图。

    # Sample YAML file to validate and export an ARM template into a build artifact
    # Requires a package.json file located in the target repository
    
    trigger:
    - main #collaboration branch
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    
    # Installs Node and the npm packages saved in your package.json file in the build
    
    - task: UseNode@1
      inputs:
        version: '20.x'
      displayName: 'Install Node.js'
    
    - task: Npm@1
      inputs:
        command: 'install'
        workingDir: '$(Build.Repository.LocalPath)/<folder-of-the-package.json-file>' #replace with the package.json folder
        verbose: true
      displayName: 'Install npm package'
    
    # Validates all of the Data Factory resources in the repository. You'll get the same validation errors as when "Validate All" is selected.
    # Enter the appropriate subscription and name for the source factory. Either of the "Validate" or "Validate and Generate ARM template" options are required to perform validation. Running both is unnecessary.
    
    - task: Npm@1
      inputs:
        command: 'custom'
        workingDir: '$(Build.Repository.LocalPath)/<folder-of-the-package.json-file>' #replace with the package.json folder
        customCommand: 'run build validate $(Build.Repository.LocalPath)/<Root-folder-from-Git-configuration-settings-in-ADF> /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/<Your-ResourceGroup-Name>/providers/Microsoft.DataFactory/factories/<Your-Factory-Name>'
      displayName: 'Validate'
    
    # Validate and then generate the ARM template into the destination folder, which is the same as selecting "Publish" from the UX.
    # The ARM template generated isn't published to the live version of the factory. Deployment should be done by using a CI/CD pipeline. 
    
    - task: Npm@1
      inputs:
        command: 'custom'
        workingDir: '$(Build.Repository.LocalPath)/<folder-of-the-package.json-file>' #replace with the package.json folder
        customCommand: 'run build export $(Build.Repository.LocalPath)/<Root-folder-from-Git-configuration-settings-in-ADF> /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/<Your-ResourceGroup-Name>/providers/Microsoft.DataFactory/factories/<Your-Factory-Name> "ArmTemplate"'
    #For using preview that allows you to only stop/ start triggers that are modified, please comment out the above line and uncomment the below line. Make sure the package.json contains the build-preview command. 
     #customCommand: 'run build-preview export $(Build.Repository.LocalPath) /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/GartnerMQ2021/providers/Microsoft.DataFactory/factories/Dev-GartnerMQ2021-DataFactory "ArmTemplate"'
      displayName: 'Validate and Generate ARM template'
    
    # Publish the artifact to be used as a source for a release pipeline.
    
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Build.Repository.LocalPath)/<folder-of-the-package.json-file>/ArmTemplate' #replace with the package.json folder
        artifact: 'ArmTemplates'
        publishLocation: 'pipeline'
    
  4. 输入你的 YAML 代码。 用YAML文件作为起点。

  5. 保存并运行。 如果使用了 YAML,则每次更新主分支时都会触发该过程。 确认运行成功并在管道发布的人工物中产生 ArmTemplates 该人工物。

注意

生成的工件已经包含触发器的部署前后脚本,所以你不需要手动添加它们。 不过,部署时仍需参考停止 和启动触发器的文档 以运行提供的脚本。

了解更多关于Data Factory中的持续集成与交付:Azure 数据工厂中的持续集成与交付。