We are currently using an Azure DevOps pipeline to trigger a terraform plan
against an existing infrastructure.
Within this pipeline during the terraform init
stage, terraform will analyze the scripts and download modules from an external repository. During the initial stage of running this pipeline we encountered the following error.
fatal: could not read Username for 'https://github.com': terminal prompts disabled
which we resolved by using GitHub PAT inline as following
git config --global url."https://$(GITHUB-USER-ACCT):$(GITHUB-PERSONAL-ACCESS-TOKEN)@github.com".insteadOf https://github.com
However as this adds the additional load of maintaining a PAT, wondering whether we can move from this method to a method of using the GitHub Service connection using GitHub Apps as an authentication mechanism. This Service connection was created using the OAuth application. The Service connection that we used has read permissions on the centralized repository which contains the Terraform modules.
Is there are way to reuse the GitHub Service Connection that has been created using GitHub Apps to clone GitHub repositories from script tasks?
We have tried providing Read Permissions to the GitHub Service Connection to the centralized repository where we have all the Terraform modules. However, that doesn’t seem to work as Terraform started to fail when trying to clone the specific GitHub repository from Azure Pipeline.
2
Answers
Yes, You can directly create OAuth service connection in Azure Devops project with your github authorization to access Azure pipelines by logging into your Github account, then create a pipeline which fetches the Terraform module directly from your github repository like below:-
My Azure Devops Github Service connection:-
My Github repository
main.tf root
Pipeline:-
Azure Devops yaml pipeline code:-
Output:-
If you create the Pipeline from
DevOps repo
, but the module is fromexternal github repo
, I’m afraid it’s not supported to use GitHub Service connection using GitHub Apps as an authentication mechanism to download the module.As per the terraform module doc:
Terraform installs modules from Git repositories by running git clone, and so it will respect any local Git configuration set on your system, including credentials. To access a non-public Git repository, configure Git with suitable credentials for that repository.
It requires credentials from
local git configuration
, On devops pipeline, we can usepersistCredentials: true
to keep the credential on the agent(endpioint below is the github service connection):It does store the info:
however, the latter
git clone
still failed:Aslo tried for terraform init with same logic, it’s failed for same.
Hence, it could be not supported yet.