如何将/etc/hosts条目添加到在Azure Pipelines构建代理上执行的容器中



我需要备份Azure API管理实例的Git存储库,该实例连接到虚拟网络并具有自定义域。因此,SCM端点例如myapiminstance.scm.azure-api.net没有有效的公共DNS条目,并且不能被解析为其本地IP地址例如10.1.2.3

由于我不想修改我的构建代理主机名解析,例如为我的虚拟网络实现专用DNS,我正在寻找一个非常简单的解决方案。

我如何使Azure Pipelines容器使用调整后的/etc/hosts运行,以便使用参数或变量组轻松控制IP地址和主机名?

在指定容器时,可以添加docker create参数--add-host作为选项:

name: apim-repo-backup-$(Date:yyyyMMdd)$(Rev:.r)
trigger:
paths:
include:
- apim-repo-backup.yml
- testHosts.ps1
stages:
- stage: test
displayName: test hostname modification
jobs:
- job:
pool:
name: internal
demands:
- Agent.OS -equals Linux
container:
image: mcr.microsoft.com/azure-powershell:latest
options: --add-host=myapiminstance.scm.azure-api.net:10.1.2.3
workspace:
clean: all
steps:
- task: AzurePowerShell@4
displayName: display hosts
inputs:
azureSubscription: 'MySubscription'
scriptType: filePath
azurePowerShellVersion: latestVersion 
scriptPath: 'testHosts.ps1'

testHosts.ps1:

cat /etc/hosts

输出:

127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.1.2.3    myapiminstance.scm.azure-api.net

最新更新