Developers want to build better software faster, I don’t think any developer would disagree on that and with the release of Go 1.11 a long-awaited (and debated) feature became available, Go modules! With that amazing addition to the Go language, you can now really have repeatable builds because all the information is put in a single
go.mod file and during the build the Go compiler will get all the right versions for you.
Now that we can have easier repeatable builds, should every developer at every build have to pull down the dependencies and store them locally or is there a different way to do this? I think the engineers at JFrog have a good answer with their latest updates of Artifactory, which now supports Go modules. So when I saw their Go challenge, I couldn’t pass on a good opportunity to see if a hummingbird can join the gopher and frog on their boat.
Are you ready for the Go Challenge? Learn how to use #Artifactory as the 1st module registry for Go and score some limited JFrog swag. Learn More >> https://t.co/exng8rqunV << pic.twitter.com/VQjAXZIolq— JFrog (@jfrog) September 21, 2018
Setting up Artifactory and JFrog CLI
To start with Artifactory, just visit their downloads page and choose one of the options to get started. For my setup I’ve opted to run the Docker container on my Intel NUC (yes… I manage that one machine 😅). To run the container, simply run:
# Pull the container $ docker pull docker.bintray.io/jfrog/artifactory-pro:latest # Run it and expose port 8081 $ docker run -d -p 8081:8081 --name=artifactory docker.bintray.io/jfrog/artifactory-pro:latest
After a few seconds you can open up the homepage of Artifactory (like http://localhost:8081) and follow the on-screen instructions to set up the Artifactory server. If you need a license for the Pro features, you can very easily sign up for a trial. Be sure to select Go as one of the options (or, as an admin, add a new local repository of type Go).
Next, you’ll need the, Go based, JFrog CLI. For macOS users, downloading the CLI is as easy as running
brew install jfrog-cli-go. Users of other Operating Systems can download a binary or an installer from that page as well. Now that is installed, you’ll need to configure it to talk to your server:
# Run this command and answer the prompts $ jfrog rt config Artifactory server ID: myServer Artifactory URL: http://localhost:8181/artifactory User: admin Password/API key: <your password>
Converting a Flogo app to use modules
Converting a Go app to use modules is relatively easy. According to most of the Go engineers you should be able to simply clone a Git repo to a path outside of your $GOPATH and running a few commands. Personally, I’ve learned a great deal from a blog post by Roberto Selback. To get started, I decided to use one of the Flogo apps I built using the Go API; my InvoiceService
The first step is to initialize the Go modules:
cd /path/to/source go mod init github.com/TIBCOSoftware/apiscout/samples/invoiceservice-go/main
The next step is to populate the
go.mod file with all the module dependencies that the app has. You could do that manually, but let’s face it… you probably want a command for that. So, run
go mod tidy which inserts all dependencies into the file and generates a
go.sum file with the commit hashes of those dependencies.
Building the app
Now the app works with Go modules (yay! 😁), but every developer will have to get the code from GitHub. With a two commands you can make sure that all those dependencies are checked into Artifactory.
# Upload the modules to Artifactory and build an app $ jfrog rt go build go --url=http://localhost:8081/artifactory --user=admin --password=<your password>
To build an app and make sure the modules are downloaded from Artifactory, you’ll need to set the new GOPROXY environment variable.
export GOPROXY="http://admin:<your password>@localhost:8081/artifactory/api/go/go" go build
So now we have a repeatable build, no matter if the sources change, or worse, disappear (😱) from the version control system while you’re working on the code! 🎉
So if you haven’t done so yet, take the JFrog Go challenge and see how awesome hummingbirds, gophers and frogs can work together! I’d love to know your thoughts! Let me know by dropping me a note here or on Twitter.
Disclaimer: I’m not an Artifactory expert, so if I can improve my workflow please let me know!
Cover image by Pixabay