首页 > 解决方案 > AWS CodeBuild golang 构建失败

问题描述

当我运行以下命令时:- go build -o app我收到以下错误(对于多个依赖项):main.go:21:2: cannot find package "github.com/gorilla/mux" in any of: /usr/local/go/src/github.com/gorilla/mux (from $GOROOT) /go/src/github.com/gorilla/mux (from $GOPATH) /codebuild/output/src324986171/src/github.com/gorilla/mux

意味着代码构建失败。知道如何解决这个问题,或者一般来说问题出在哪里?谢谢你的帮助。

编辑:添加go get ./...到构建后,我的所有本地包都出现以下错误:# cd .; git clone https://github.com/aristotle/dbhelper /go/src/github.com/aristotle/dbhelper Cloning into '/go/src/github.com/aristotle/dbhelper'...

我的 buildspec.yml 看起来像这样:

version: 0.2

phases:
  install: 
    commands:
      - echo CODEBUILD_SRC_DIR - $CODEBUILD_SRC_DIR
      - echo GOPATH - $GOPATH
      - echo GOROOT - $GOROOT
  build:
    commands:
      - echo Build started on `date`
      - echo Getting packages
      - go get ./...
      - echo Compiling the Go code...
      - go build -o app main.go
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - app

标签: amazon-web-servicesgoaws-codepipelineaws-codecommit

解决方案


根据这篇文章,您需要将其添加到文件的install部分buildspec.yml

install: 
    commands:
      - go get github.com/gorilla/mux

也许包含它也go get ./...可以解决所有依赖关系......但如果你没有太多,那么明确列出它们是一个好习惯。

这是源文章:https ://www.contributing.md/2017/06/30/golang-with-aws-codebuild/


推荐阅读