6

Azure DevOps(三)Azure Pipeline 自动化将程序包上传到 Azure Blob Storage - Grant...

 1 year ago
source link: https://www.cnblogs.com/AllenMaster/p/17372029.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

  结合前几篇文章,我们了解到 Azure Pipeline 完美的解决了持续集成,自动编译。同时也兼顾了 Sonarqube 作为代码扫描工具。接下来另外一个问题出现了,Azure DevOps 由于有人员限制,项目上不能给非开发人员或者外包成员开权限,这个时候就需要将编译好的程序包上传到公共网盘或者私有的远端存储账户,方便下载。那么我们今天就结合 Azure storage account 来实践一次利用 Azure Release Pipeline 自动将编译好的程序包发布到 Blob Storage 中。接下来就开始了今天的实践内容。

--------------------Azure DevOps 系列--------------------

1,Azure DevOps(一)基于 Net6.0 的 WPF 程序如何进行持续集成、持续编译

2,Azure DevOps(二)Azure Pipeline 集成 SonarQube 维护代码质量和安全性

3,Azure DevOps(三)Azure Pipeline 自动化将程序包上传到 Azure Blob Storage

1,创建存储账户

我们登录 Azure Portal,点击 “create a resource” 创建新的资源

1996262-20230504165919198-1429431850.png

输入 “storage account” 查看资源,并进行创建

1996262-20230504170024612-243806761.png

输入以下参数:

Resource group:“Web_Test_SA_RG”(已有的资源组,也可以选择创建新的)

Storage account name:“cloudplatform”

Performance 选择:“Standard”

1996262-20230504170226190-943027773.png

Redundancy 选择:“Locally-redundant storage(LRS)”

点击 “Review” 进行创建前的预校验

1996262-20230504171152717-881183082.png

预校验完成后,点击 “Create” 进行创建

1996262-20230504171351030-2071301079.png

等待创建完成后,跳转到该资源进行查看

1996262-20230504171651045-375707967.png

2,为 service principal 授权

创建 sp 的过程大家可以参考:Azure AD(四)知识补充-服务主体

接下来就是为 Azure DevOps 的 Service connection (也就是服务主体设置权限)

选择 “cloudplatform =》Access Control”,点击 “+ Add”,添加角色分配

1996262-20230504173532601-16306834.png

选择 “Storage Blob Data Contributor” 点击 “Next”

1996262-20230505145839181-1683712142.png

点击 “+ Select members”,选择好 service principal,点击 “select” 进行选择确定

1996262-20230505145950371-14055796.png

点击 ”Review + assign“ 进行确定预览指派权限控制

1996262-20230505150804716-209797103.png

稍后,我们就可以看到当前 sp 的权限信息,Scope 为 ”This resource“

1996262-20230505151000872-1583058412.png

3,创建 Release Pipeline

登录 azure devops 找到上一篇课程中创建的项目 “NetCore_WPF_Sample” 项目

选择 “Pilelines =》Releases”,点击 “New pipeline” 创建新的管道作业

1996262-20230504163107059-1541228285.png

点击 ”Artifacts + Add“ 添加发布源

Source type 选择:”Build“

Project:”NetCore_WPF_Sample“

Source(build pipeline)选择 :”yunqian44.Standard.Tool.Platform“(build Pipeline)

Dafault version:”Latest“

Source alias 选择默认

点击 ”Add“ 进行添加操作

1996262-20230505152257398-548826366.png

接下来点击箭头所指的位置,添加任务项。

1996262-20230505152800927-566736346.png

1)添加新的 ”Task“,输入 ”Azure CLI“ 进行查询,并添加

1996262-20230505153944912-1587668923.png

输入以下参数

Manage 选择:”vs“(自己项目所用的 serviceconnection)

Script Type:”Shell“

Script Location:”Inline script“

Inline Script:

# create azure resource group
az group create --location eastasia --name $(terraform_rg)

# create azure storage account
az storage account create --name $(storage_account) --resource-group $(terraform_rg) --location eastasia --sku Standard_LRS

# create storage account container for package blob 
az storage container create --name $(storage_account_container) --account-name $(storage_account)
1996262-20230505154512473-1490921039.png

2)搜索 ”Archive files“,添加压缩任务

1996262-20230505155030550-564678901.png

修改 需要压缩的文件或者文件夹的根目录

1996262-20230505155302721-1416829381.png

如下图所示:

源:改为 Build Pipeline 输入目录下的 drop 文件

目标:改为Build Pipeline 输入目录下已 BuildId 命名的压缩文件

1996262-20230505160006929-1397058790.png

接下来,我们添加 ”Azure file copy“,添加文件拷贝任务

1996262-20230505160142095-613369789.png

输入一下参数,需要注意  存储账户,容器名称 Blob 前缀都已变量的形式体现

Source 选择 ”Archive“ 任务输出的压缩文件的目录

Azure Subscription 选择实际项目

Destination Type 选择:”Azure Blob“

RM Storage Account:$(storage_account) 

Container Name:$(storage_account_container)

Blob prefix:$(Build.BuildId).zip

1996262-20230505161318523-1204225545.png

最后,我们需要添加变量,选择 “Variable”,点击 “+” 进行操作,输入以下参数,并保存

Name Value
storage_account cloudplatform
storage_account_container wpf-client
terraform_rg Web_Test_SA_RG
1996262-20230505162402488-33933265.png

点击 “Create release” 尝试运行 release pipeline 

1996262-20230505162515674-159071238.png

点击 “OK” 确认此操作

1996262-20230505162608733-1521179801.png

Release Pipeline 运行成功

1996262-20230505163403213-28368730.png

存储账户的容器 “” 也被创建好了

1996262-20230505163511562-1394337763.png

编译好的程序包已被上传到 Storage Account 的 Container 中了

1996262-20230505163611160-763447056.png

Bingo!!!!!🎈🎉🎉🎉

结合前两篇内容讲解的 Azure DevOps 系列,基本上从项目前期的持续集成,持续编译,到中期的代码静态扫描,再到后期的实现项目编译好的程序包自动发布到存储账号中,基本已经满足项目需求。DevOps 已从完美的升级为  DevSecOps 了。下一篇,对我我们目前的项目,我是如何实施DevOps的!!!尽请期待。

Azure Blob 存储文档 ----- 访问控制:https://learn.microsoft.com/zh-cn/azure/storage/blobs/data-lake-storage-acl-cli

作者:Allen 

版权:转载请在文章明显位置注明作者及出处。如发现错误,欢迎批评指正。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK