How To: Using private git repositories as Terraform modules


Inside the pipeline, you want to load Terraform modules from a private git repository. You want to reuse your credentials from a git integration to allow Terraform to initialize its modules.


Your build is stuck on terraform init command.

In the logs you’ll see:

Executing command: terraform init   
Initializing modules...   
Downloading git::<username>/<repository>.git for <module-name>...   
Username for '':

After some period of being hung build is terminated with the message Build was terminated because of prolonged inactivity.

You can reuse the token from your git integration in an automated manner. To do so, you need to add an extra freestyle step:

version: '1.0'   
    title: "Reading GitHub token "  
    image: "codefresh/cli"  
      - cf_export GITHUB_TOKEN=$(codefresh get context <git-integration-name> --decrypt -o yaml | yq -r

❕ Please note that you have to replace <git-integration-name> with the name of your git integration, whose token you want to use.

Once it’s done, $GITHUB_TOKEN variable will contain your git token across the steps.

Then you need to add an extra git config command to your Terraform step, to use this token while Terraform authenticated:

    title: "Init terraform with modules from private repo"  
    image: "hashicorp/terraform:latest"  
    working_directory: "./$"  
      - git config --global url."https://<username>:$".insteadOf  
      - terraform init

❕ Please note that you have to replace<username> with the name of your git user.

Now you’re able to use Terraform modules from private git repositories.