0
点赞
收藏
分享

微信扫一扫

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道

这里首先先介绍下我这个环境下CICD两个阶段的定义以及要完成的工作,然后再对CI阶段是如何做的展开详细介绍

持续集成(CI):

  • PreCheck - 使用Bicep Linter完成语法检查等工作
  • Validate - 执行validate命令检查部署前置条件,验证依赖关系等
  • Publish - 将部署用的脚本publish出去,供CD Pipeline使用

持续交付(CD):

  • Pre Deploy - 执行whatif尝试进行部署
  • Deploy - 最终执行部署

所以每个阶段基本都会有两个stage,分别完成不同的工作,接下来就是针对每个stage的展开介绍了

Azure DevOps中CI和CD的pipeline现在是分开的,CI的pipeline名字现在就叫pipelines,CD的pipeline则是叫release

Azure DevOps的注册等就不赘述了,测试的话注册一个免费的就OK了,我们直接就进入执行过程,需要首先创建一个pipeline

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_BICEP

然后代码仓库选择Azure Repo即可

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_CICD_02

选择一个starter pipeline或者空白模板都可以

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_Azure_03


接下来就是pipeline的编写工作,微软提供了一个不错的在线编辑工具,还支持图形化界面,不过还是可以用VSCode + Azure Pipelines extension来编写pipeline,这个看个人习惯了


# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

pool:
vmImage: ubuntu-latest

stages:
- stage: PreCheck
jobs:
- job: Lint
displayName: 'Lint Code'
steps:
- script: >
az bicep build --file Deploy/main.bicep
name: LintCode
displayName: 'Lint Code Step'
- stage: Validate
jobs:
- job: ValidateCode
displayName: 'Validate deployment code'
steps:
- task: AzureCLI@2
name: ValidateCodeWithCLI
displayName: 'Validate BICEP Code with Azure CLI'
inputs:
azureSubscription: 'Azure Global'
scriptType: bash
scriptLocation: inlineScript
inlineScript: >
az deployment group validate --resource-group $(ResourceGroupName) --template-file Deploy/main.bicep
- stage: Publish
jobs:
- job: Publish
displayName: 'Publish Artiface'
steps:
- task: CopyFiles@2
displayName: 'Copy deployment file to artifactstagingdirectory'
inputs:
SourceFolder: 'Deploy'
Contents: '**'
TargetFolder: '$(build.artifactstagingdirectory)'
OverWrite: true
- task: PublishBuildArtifacts@1
displayName: 'Publish to artifact'
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: Build
publishLocation: Container

因为在pipeline中有使用一些变量来让部署增加灵活性,所以需要创建个变量,然后可以再给pipeline改个名

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_IAC_04


准备完成后,先手动跑下试试看

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_CICD_05


注意把变量也要选上

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_CICD_06


可以看到检查完成了

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_Azure_07


可以看到有一些warning提示有参数定义了但是没用

Azure DevOps结合BICEP实现Azure资源自动创建 - 完成CI管道_BICEP_08


这样CI阶段就搞完了

举报

相关推荐

0 条评论