我们目前正在使用 Azure DevOps 管道针对现有基础设施触发
terraform plan
。
在此管道中的
terraform init
阶段,terraform 将分析脚本并从外部存储库下载模块。在运行该管道的初始阶段,我们遇到了以下错误。
fatal: could not read Username for 'https://github.com': terminal prompts disabled
我们通过使用 GitHub PAT 内联解决了这个问题
git config --global url."https://$(GITHUB-USER-ACCT):$(GITHUB-PERSONAL-ACCESS-TOKEN)@github.com".insteadOf https://github.com
然而,由于这增加了维护 PAT 的额外负担,我们想知道是否可以从这种方法转向使用 GitHub 应用程序作为身份验证机制的 GitHub 服务连接的方法。此服务连接是使用 OAuth 应用程序创建的。我们使用的服务连接对包含 Terraform 模块的集中存储库具有读取权限。
是否可以重用使用 GitHub Apps 创建的 GitHub 服务连接,以从脚本任务克隆 GitHub 存储库?
我们尝试向拥有所有 Terraform 模块的集中存储库的 GitHub 服务连接提供读取权限。然而,这似乎不起作用,因为当尝试从 Azure Pipeline 克隆特定的 GitHub 存储库时,Terraform 开始失败。
是的,您可以通过登录您的 Github 帐户,使用您的 github 授权直接在 Azure Devops 项目中创建 OAuth 服务连接,以访问 Azure 管道,然后创建一个直接从 github 存储库获取 Terraform 模块的管道,如下所示:-
我的 Azure Devops Github 服务连接:-
管道:-
Azure Devops yaml 管道代码:-
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
persistCredentials: true # Ensure credentials are persisted for fetching code from GitHub
- script: |
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt install terraform
displayName: 'Install Terraform'
- script: |
terraform init
terraform plan -var "resource_group_name=my-resrsilicongroup" -var "location=East US" -out=tfplan
displayName: 'Terraform Init and Plan'
- script: |
terraform apply tfplan
displayName: 'Terraform Apply'
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
输出:-