In some build systems like gradle or blaze I can generate code (ent or proto) on a build stage and don't add in to a repository.
Is it possible to do the same for the go build command?
Yes, if you add "go generate" as your pre-build step in CI script.
$ go generate
$ go build
$ go test
But I would recommend more practical approach: to store your generated code in your repo and check it on CI - run go generate and assert that there is no changes.
Links
https://go.dev/blog/generate
Related
I inherited a build script that builds a docker image and uses hashicorp/levant library to deploy. For a year or so we have been running go get github.com/jrasell/levant to grab the levant library. In past few days, the repo URL was merged under Hashicorp's organization and we've changed our script to pull with go get github.com/hashicorp/levant. But either way, we get this multi assign error. What does this mean, doesn't 'go' just basically pull the git repo?
../go/src/github.com/hashicorp/levant/template/render.go:28:11: cannot assign
*"github.com/hashicorp/nomad/vendor/github.com/hashicorp/nomad/api".Job to job
(type *"github.com/hashicorp/nomad/api".Job) in multiple assignment
Firstly, go get works with packages, not repositories.
In addition to pulling them go get also compiles and installs them, and here's when your error pops up.
More info here:
https://nanxiao.gitbooks.io/golang-101-hacks/content/posts/go-get-command.html
I recommend you to use Go modules.
hashicorp/levant does have go.{mod,sum} files and hence you should forget using the go get way.
It's better to do a clone and follow the go module way i.e.,
git clone git#github.com:hashicorp/levant.git
go test ./...
go build ./...
The steps with not only just clone your repo but would also bring your dependent packages required for building/ testing the package.
Note: You should have Go v1.11+
I am making a Go project containing a few packages. Those are data structures (and there will be algorithms as well). My project root looks like this:
C:.
├───array
├───binary_tree
├───heap
└───list
The thing is, I want to add like a CI. So I would have a job checking if all packages build and if all tests pass.
Unfortunately, I can't run go build on project root. I have to pass whole path to it. I mean I could write a script which calls go build X/Y/foo and then go build X/Y/bar, but the CI on GitLab (docker image) won't have those paths, it will just git clone my repo and that's it (cuz I cannot run it on a relative path from project root, but a relative path from GOPATH, so like github.com/dabljues/project_name/array). And what about the test?
So the question there would be: Can I somehow run go build and go test for all the packages in the Go project? (located in separate folders)
Use a Makefile. Define the targets and instruction to compile them.
For example define the following in a Makefile
build:
go build -o bin/main main.go
run:
go run main.go
To run just invoke : make build
I'm trying to run tests on my software as it's about to be deployed via git to blueMix. Currently the pipeline is 'build stage' -> 'deploy stage', and now I'm trying to implement the 'test stage' in the middle of that process.
My test stage has a test job, and the Tester Type is simple. The command I have currently is:
#!/bin/bash
# invoke tests here
go test
, however the test stage fails and returns:
_build.sh: line 3: go: command not found
Build step 'Execute shell' marked build as failure
Evidently go isn't installed at the testing stage. Do I get the test server to install Go on each new git push to test it, or am I sorely mistaken?
Thanks!
That's correct. There are only certain tools pre-installed in the enviroment that runs the jobs. You can find them here: https://hub.jazz.net/docs/deploy_var/#resources - Everything else would have to be installed each time the job is run.
So I have a go project with go-gettable dependencies, tests, etc.
I want to integrate it into Jenkins. Is there an automated build system for go that anyone recommends for this use case, other than writing makefiles?
I need:
automatic installation of go-get deps (they can be in a spec file of course)
recursive build.
running tests.
GOPATH/GOROOT management (to isolate SDKs and paths)
I've used godag in the past for this sort of job but it seems a bit unmaintained.
EDIT: For the time being I'm living with the following script entered directly into Jenkins as a build step:
#this gets the dependencies but doesn't install them, avoiding permission problems
go get -d
#build the packages, -x outputs the compiler command line
go build -x
#this was tricky - ./... means "for each sub-package recursively"
go test ./...
You can do it with teamcity as well.
Here is an example for building terraform.
Teamcity agent setup:
Install Go
Add Go to path
Don't forget to restart agent as path was changed
Teamcity build setup:
Use agent side checkout (if we want to include the .git folder, which a lot of build scripts make use of)
Use checkout rule (we want to use the Go convention):
+:. => src/github.com/mitchellh/terraform
Build steps:
echo cd %system.teamcity.build.checkoutDir%
cd "%system.teamcity.build.checkoutDir%"
path=C:\Program Files (x86)\Git\bin;%env.Path%;%system.teamcity.build.checkoutDir%\bin;
echo path=C:\Program Files (x86)\Git\bin;%env.Path%;%system.teamcity.build.checkoutDir%\bin;
set GOPATH=%system.teamcity.build.checkoutDir%
echo set GOPATH=%system.teamcity.build.checkoutDir%
echo "Getting dependancies"
go get golang.org/x/tools/cmd/vet
go get golang.org/x/tools/cmd/cover
go get golang.org/x/tools/cmd/stringer
go get github.com/mitchellh/gox
echo cd %system.teamcity.build.checkoutDir%\src\github.com\mitchellh\terraform
cd "%system.teamcity.build.checkoutDir%\src\github.com\mitchellh\terraform"
echo "Update resources"
go get ./...
echo "Run static code analysis"
go tool vet -asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr .
echo "Build"
cd scripts
sh build.sh
echo "Run unit tests"
go test -timeout=30s -parallel=4
echo "Run code coverage"
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
rm coverage.out
I am using a Team City build server on a Mac that runs a rake file, in the rake file I do all the commands to get dependancies, (go gets), tests and builds to the correct environment.
let me know if you want any pointers in writing the Rake files,
As a side note, i have been creating functional tests for REST Api's using this frame work. This has saved my code many times over. http://github.com/DigitalInnovation/cucumber_rest_api
Since August 2019, TeamCity 2019.1 now supports Go directly.
See "Building Go programs in TeamCity"
To enable Go support in TeamCity,
go to Build Configuration Settings | Build Features,
click Add build feature, and
select Golang from the list.
Support for Go is provided by TeamCity out of the box, there are no external plugins required.
TeamCity parses results of go test command execution. The results are persisted and it is possible to review the figures in a historical perspective.
Consequently, all the TeamCity features that are related to test reporting are now available for Go developers.
I've used Google and looked at the Docs but can't find an answer.
I want to trigger another build or a rake script to do some cleaning up of files after a successful build. I currently use Rake to do my build and pass in %teamcity.build.branch% to it.
I would like to know if I can pass the same branch name of the successful build to the triggered build or script. I can then use this to do some tidying up.
Thanks
In addition to finished build trigger, you need to add snapshot dependency, with "Run build on the same agent" option enabled. This way, cleanup will run after each build, on the same agent.
You then will be able to refer to original build's branch name using dependencies parameters:
%dep.<original_bt_id>.teamcity.build.branch%