登录
首页 >  Golang >  Go问答

使用 Terraform 在 CircleCI 的 Go 图像上执行部署

来源:stackoverflow

时间:2024-03-01 20:54:28 109浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《使用 Terraform 在 CircleCI 的 Go 图像上执行部署》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

我已经为我的 terraform 代码进行了基于 go 的自动化测试,我想让它们在 circleci 中运行。为此,我为 circleci 设置了以下 config.yml:

version: 2

jobs:
  build:
    docker:
      - image: circleci/golang:1.12
      - image: hashicorp/terraform:light

    working_directory: /go/src/bitbucket.org/teapigsteam/findmytea-terraform
    steps:
      - checkout

      - run: go get -v -t -d ./...
      - run: go test -v ./...

但由于某种原因,go 找不到 terraform 可执行文件:

#!/bin/bash -eo pipefail
go test -v ./...
=== RUN   TestFindMyTeaApp
TestFindMyTeaApp 2020-03-21T12:20:26Z retry.go:72: terraform [init -upgrade=false]
TestFindMyTeaApp 2020-03-21T12:20:26Z command.go:87: Running command terraform with args [init -upgrade=false]
TestFindMyTeaApp 2020-03-21T12:20:26Z retry.go:80: Returning due to fatal error: FatalError{Underlying: exec: "terraform": executable file not found in $PATH}
TestFindMyTeaApp 2020-03-21T12:20:26Z retry.go:72: terraform [destroy -auto-approve -input=false -var app_name=findmytea-terraform-tdd -lock=false]
TestFindMyTeaApp 2020-03-21T12:20:26Z command.go:87: Running command terraform with args [destroy -auto-approve -input=false -var app_name=findmytea-terraform-tdd -lock=false]
TestFindMyTeaApp 2020-03-21T12:20:26Z retry.go:80: Returning due to fatal error: FatalError{Underlying: exec: "terraform": executable file not found in $PATH}
--- FAIL: TestFindMyTeaApp (0.00s)
    apply.go:13: 
            Error Trace:    apply.go:13
                                        findmyteaui_test.go:19
            Error:          Received unexpected error:
                            FatalError{Underlying: exec: "terraform": executable file not found in $PATH}
            Test:           TestFindMyTeaApp
    destroy.go:11: 
            Error Trace:    destroy.go:11
                                        panic.go:406
                                        testing.go:609
                                        apply.go:13
                                        findmyteaui_test.go:19
            Error:          Received unexpected error:
                            FatalError{Underlying: exec: "terraform": executable file not found in $PATH}
            Test:           TestFindMyTeaApp
FAIL
FAIL    bitbucket.org/teapigsteam/findmytea-terraform/test  0.005s

Exited with code exit status 1
CircleCI received exit code 1

谁能告诉我我做错了什么?或者也许这根本不可能?


解决方案


不要尝试使用两个 Docker 镜像,而是尝试仅使用 hashicorp/terraform:full

我相信您看到此错误是因为您的代码是在 Golang 容器中执行的,而该容器无法访问 Terraform 轻型容器中的可执行文件。

您可以创建一个自定义 Docker 映像来运行它,并手动安装 Terraform。然而,hashicorp/terraform:full 镜像无论如何都是建立在 Golang 镜像之上的,所以理论上应该会让你更接近你的目标。

本篇关于《使用 Terraform 在 CircleCI 的 Go 图像上执行部署》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>