mikrotik_
mikrotik_

Reputation: 430

Azure DevOps use module from another repository in different project

I have my coding project in Azure DevOps and I would like to use Terraform modules I create. However I would like to keep terraform and my application each in different project.

I am having issues when I want to use Terraform inside AZ Pipelines, I receive the following error when I run my pipeline:

  - task: Bash@3
    displayName: 'DEBUG'
    inputs:
      targetType: 'inline'
      script: |
        git config --get-all --global http.https://dev.azure.com/my-organization/Infra/_git/terraform-modules.extraheader "Authorization: bearer $SYSTEM_ACCESSTOKEN"
        terraform init
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)

Output

Error: Failed to download module

Could not download module "iam" (aws_ecs.tf:11) source code from
"git::https://dev.azure.com/my-organization/Infra/_git/terraform-modules": error
downloading 'https://dev.azure.com/my-organization/Infra/_git/terraform-modules':
/usr/bin/git exited with 128: Cloning into '.terraform/modules/iam'...
fatal: could not read Username for 'https://dev.azure.com': terminal prompts
disabled

Module source is configured like this:

  source = "git::https://dev.azure.com/my-organization/Infra/_git/terraform-modules//modules/ecs-iam"

AZ DevOps settings (images):

On the local machine, everything works fine, the module is cloned OK.

I have tried to follow the official guide https://www.terraform.io/docs/modules/sources.html

Is this even possible?

Upvotes: 2

Views: 3601

Answers (3)

AditYa
AditYa

Reputation: 907

In my case, I had the same error but it was in my local test by using the terraform init and plan commands. I resolved the issue with the below solution.

I am using a PowerShell in VS code tool. so set the ENV variable using the below.

$env:AZDO_ORG_SERVICE_URL="https://dev.azure.com/ADO_ORG_NAME/"
$env:AZDO_PERSONAL_ACCESS_TOKEN="<PAT token>"

The module source path is in the .tf file. Copied HTTP URL from the Azure repository's clone option and amended to clone modules from a specific branch.

source = "git::https://<ORG_NAME>@dev.azure.com/<ORG_Name>/SW/_git/<REP_NAME>//Policies/Terraform/modules/?ref=<Branch_Name>"

Then the authentication is working.

Upvotes: 0

Daniel Mann
Daniel Mann

Reputation: 59035

There's no need to directly use Git. Add a repository resource to your YAML, then use checkout to clone it.

Ref: https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#resources

Upvotes: 0

FN_
FN_

Reputation: 853

I think you have the wrong url in git config, You should remove everything after repository name.

It should look like:

git config --global http.https://dev.azure.com/my-organization/Infra.extraheader "Authorization: bearer $SYSTEM_ACCESSTOKEN"

Upvotes: 2

Related Questions