Referencing submodule from git repository results in error when initializing module during GitLab CI/CD execution

huangapple go评论63阅读模式
英文:

Referencing submodule from git repository results in error when initializing module during GitLab CI/CD execution

问题

I reference my module as:

source = "git::https://gitlab.com/dash-infrastructure.git//modules/lambda-apigw-api"

Terraform plan in my CI/CD passes fine. However, in the terraform apply stage, I get the following error:

Error: Failed to download module

on main-lambda-apigw-from-module.tf line 1:
1: module "lambda_apigw_module" {

Could not download module "lambda_apigw_module"
(main-lambda-apigw-from-module.tf:1) source code from
"https://gitlab.com/dash-infrastructure.git":
error downloading
'https://gitlab.com/dash-infrastructure.git':
/usr/bin/git exited with 1: error: pathspec 'master' did not match any
file(s) known to git

Branch 'master' doesn't even exist in the repository of my module. Any idea what could be the issue?

I am expecting the apply to download my module from the git repo. I tried specifying the ref to my only and default branch - main, even specifying the commit hash with:

source = "git::https://gitlab.com/dash-infrastructure.git//modules/lambda-apigw-api/?ref=main"

But did not help, still same error.

EDIT - RESOLVED

Matt was right - the pipeline executing workflow was indeed the issue. I unintentionally included all 'untracked' files as artifacts of the GitLab CI/CD stage 'terraform plan', which caused this discrepancy. When adding only the artifacts I needed (plans directory), this problem was fixed:

plan lambda develop:
  stage: deploy_lambda
  tags:
    - dash
  needs: 
    - job: build lambda develop
      artifacts: true
  script:
    - cd ./backend/lambda/infrastructure/nonprod
    - mkdir plans
    - *terraform-init-nonprod
    - terraform plan -out plans/plan
  artifacts:
    paths:
    - backend/lambda/infrastructure/nonprod/plans/*
英文:

I reference my module as:

source =  "git::https://gitlab.com/dash-infrastructure.git//modules/lambda-apigw-api"

Terraform plan in my CI/CD passess fine. However, in the terraform apply stage, I get the following error:


│ Error: Failed to download module
│ 
│   on main-lambda-apigw-from-module.tf line 1:
│    1: module "lambda_apigw_module" {
│ 
│ Could not download module "lambda_apigw_module"
│ (main-lambda-apigw-from-module.tf:1) source code from
│ "git::https://gitlab.com/dash-infrastructure.git":
│ error downloading
│ 'https://gitlab.com/dash-infrastructure.git':
│ /usr/bin/git exited with 1: error: pathspec 'master' did not match any
│ file(s) known to git

Branch 'master' doesn't even exist in the repository of my module. Any idea what could be the issue?

I am expecting the apply to download my module from the git repo. I tried specifying the ref to my only and default branch - main, even specifying the commit hash with:

source = "git::https://gitlab.com/dash-infrastructure.git//modules/lambda-apigw-api/?ref=main"

But did not help, still same error.

EDIT - RESOLVED

Matt was right - the pipeline executing workflow was indeed the issue. I unintentionally included all 'untracked' files as artifacts of the GitLab CI/CD stage 'terraform plan', which caused this discrepancy. When adding only the artifacts I needed (plans directory), this problem was fixed:

plan lambda develop:
  stage: deploy_lambda
  tags:
    - dash
  needs: 
    - job: build lambda develop
      artifacts: true
  script:
    - cd ./backend/lambda/infrastructure/nonprod
    - mkdir plans
    - *terraform-init-nonprod
    - terraform plan -out plans/plan
  artifacts:
    paths:
    - backend/lambda/infrastructure/nonprod/plans/*

答案1

得分: 1

在这种情况下,您需要指定git引用,它可以是分支、标签、提交SHA等。根据问题的后半部分,假设您打算引用main分支:

source = "git::https://gitlab.com/dash-infrastructure.git?ref=main"

并且可以通过terraform init检索git远程存储库的main分支。更多信息可以在文档中找到,该文档还链接到git引用文档。

此外:
> 我的CI/CD中Terraform计划运行正常。然而,在Terraform应用阶段,我遇到了以下错误:

这明显暗示了在执行工作流部分的管道代码中也存在问题,因此可能需要在这方面提出后续问题。

英文:

In this situation you would need to specify the git reference which can be a branch, tag, commit sha, etc. Assuming from the latter portion of the question that you intend to reference a branch main:

source = "git::https://gitlab.com/dash-infrastructure.git?ref=main"

and the main branch of the git remote repository would be retrieved through terraform init. More information can be found in the documentation which also links to git reference documentation.

Additionally:
> Terraform plan in my CI/CD passess fine. However, in the terraform apply stage, I get the following error:

This heavily implies there are also issues in the pipeline code executing the workflow portion, and so there may need to be a followup question around that.

答案2

得分: 0

Matt说得对 - 执行工作流程的管道确实有问题。我不小心将所有“未跟踪”的文件都包括在GitLab CI/CD阶段“terraform plan”的工件中,导致了这个不一致。当只添加我需要的工件(plans目录)时,这个问题就解决了:

plan lambda develop:
  stage: deploy_lambda
  tags:
    - dash
  needs: 
    - job: build lambda develop
      artifacts: true
  script:
    - cd ./backend/lambda/infrastructure/nonprod
    - mkdir plans
    - *terraform-init-nonprod
    - terraform plan -out plans/plan
  artifacts:
    paths:
    - backend/lambda/infrastructure/nonprod/plans/*
英文:

Matt was right - the pipeline executing workflow was indeed the issue. I unintentionally included all 'untracked' files as artifacts of the GitLab CI/CD stage 'terraform plan', which caused this discrepancy. When adding only the artifacts I needed (plans directory), this problem was fixed:

plan lambda develop:
  stage: deploy_lambda
  tags:
    - dash
  needs: 
    - job: build lambda develop
      artifacts: true
  script:
    - cd ./backend/lambda/infrastructure/nonprod
    - mkdir plans
    - *terraform-init-nonprod
    - terraform plan -out plans/plan
  artifacts:
    paths:
    - backend/lambda/infrastructure/nonprod/plans/*

huangapple
  • 本文由 发表于 2023年7月18日 16:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76711049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定