'Not able to clone repo

By default my gitlab repo has both http and ssh. I was trying to create standalone terraform module, and got an error, seems GitLab Runner can't clone the repo.

Here is my tf module

module     "vpc"   {
   source = "git::https://gitlab.com/space/project/vpc.git"
   ...
}

Error

Executing "step_script" stage of the job script
$ terraform init
Initializing modules...

Downloading git::https://gitlab.com/space/project/vpc.git for vpc...
Error: Failed to download module
Could not download module "vpc" (main.tf:10) source code from
"git::https://gitlab.com/space/project/vpc.git":

error downloading
'https://gitlab.com/space/project/vpc.git':
/usr/bin/git exited with 128: Cloning into '.terraform/modules/vpc'...
fatal: could not read Username for 'https://gitlab.com': No such device or
address

I've tried to clone repo in ci however no success

git clone https://gitlab.com/space/project/vpc.git

Error:

Cloning into 'vpc'...
fatal: could not read Username for 'https://gitlab.com': No such device or address
Cleaning up file based variables
ERROR: Job failed: command terminated with exit code 128


Solution 1:[1]

You need to publish your module to the terraform module registry and also provide authentication information.

You can then specify your source according the published location:

source = "gitlab.com/<namespace>/<module-name>/<module-system>"

In your ~/.terraformrc file, you also need to configure credentials to retrieve this module. For example:

credentials "gitlab.com" {
  token = "<TOKEN>"
}

In a GitLab CI job, you would want to (1) specify TF_CLI_CONFIG_FILE as a location in the workspace and (2) Create this file as a script step, adding the credentials. You can also use the builtin CI_JOB_TOKEN for the token credentials.

For example:

variables:
  TF_CLI_CONFIG_FILE: $CI_PROJECT_DIR/.terraformrc
before_script:
  - echo -e "credentials \"$CI_SERVER_HOST\" {\n  token = \"$CI_JOB_TOKEN\"\n}" > $TF_CLI_CONFIG_FILE
  # - tf ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sytech