1

Go and devops are both new things for me. I'd like to setup a CI that tests and builds my module, then stores artifacts for download. In addition it'd be good to cache dependencies for next builds.

I don't get how to do it using subdirectory for the go product. I want my git repository to look like that:

+ repository_root/
  + go/
    - go.mod
    - go.sum
    + hello/
      - main.go
  + cpp/
  + python/
  + .circleci/

go.mod

module github.com/{USER}/{REPOSITORY_ROOT}/go

go 1.14

Of course {USER} is replace with my github user name and {REPOSITORY_ROOT} is the repo name.

For now I have a following config.yml that doesn't work.

version: 2
jobs:
  build:
    working_directory: ~/project/go
    docker:
      - image: circleci/golang:1.9

    steps:
      - checkout:
          path: ~/project/

      - run: mkdir bin
      - run: go get -v -t -d ./...
      - run: go test -v ./...
      - run: go build -o bin/hello.exe github.com/{USER}/{REPOSITORY_ROOT}/go/hello

      - store_artifacts:
          path: bin/
          destination: hello-app

Error on build command:

#!/bin/bash -eo pipefail
go build -o ./bin/hello.exe github.com/{USER}/{REPO_ROOT}/go/hello
can't load package: package github.com/{USER}/{REPO_ROOT}/go/hello: cannot find package "github.com/{USER}/{REPO_ROOT}/go/hello" in any of:
    /usr/local/go/src/github.com/{USER}/{REPO_ROOT}/go/hello (from $GOROOT)
    /go/src/github.com/{USER}/{REPO_ROOT}/go/hello (from $GOPATH)

Exited with code exit status 1
CircleCI received exit code 1

I know that the module is outside of GOPATH but I have the same setup on my local machine and the same build command works just fine.

2

My approach nowadays, always package your build process into Dockerfile. I.e. here is Dockerfile for one of my open-sourced Go projects:

https://github.com/relizaio/relizaGoClient/blob/master/Dockerfile

Note: if you're using go modules (which seems to be the case), replace RUN go get -d -v ./... with RUN go mod download in the build-stage. BTW, missing go mod instruction might be the reason for your build failure in the current setup.

Then in your CI, simply do docker build instruction, and then copy your artifact from the container if you need it outside.

Key benefit: you can verify build process against same Dockerfile locally and you can port it to any CI tool whatsoever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.