Add support for Docker image build

This commit is contained in:
Torin Sandall
2016-10-17 20:32:09 -07:00
parent 17af66f0ca
commit 4dfa7acf21
7 changed files with 53 additions and 83 deletions
+2
View File
@@ -0,0 +1,2 @@
.git
vendor
+2 -3
View File
@@ -4,9 +4,8 @@
# build artifacts
coverage
opa
opa_linux_amd64
opa_darwin_amd64
opa_*
.Dockerfile_*
# runtime artifacts
policies
-2
View File
@@ -1,6 +1,4 @@
language: go
env:
- CROSSCOMPILE="linux/amd64 darwin/amd64"
go:
- 1.6
- 1.7
+13
View File
@@ -0,0 +1,13 @@
# Copyright 2016 The OPA Authors. All rights reserved.
# Use of this source code is governed by an Apache2
# license that can be found in the LICENSE file.
FROM scratch
MAINTAINER Torin Sandall <torinsandall@gmail.com>
ADD /opa_linux_GOARCH /opa
ENTRYPOINT ["/opa"]
CMD ["run"]
+19 -13
View File
@@ -2,7 +2,7 @@
# Use of this source code is governed by an Apache2
# license that can be found in the LICENSE file.
VERSION := "0.1.1-dev"
VERSION := 0.1.1-dev
PACKAGES := \
github.com/open-policy-agent/opa/ast/.../ \
@@ -16,6 +16,13 @@ PACKAGES := \
github.com/open-policy-agent/opa/test/.../
GO := go
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
BIN := opa_$(GOOS)_$(GOARCH)
REPOSITORY := openpolicyagent
IMAGE := $(REPOSITORY)/opa
BUILD_COMMIT := $(shell ./build/get-build-commit.sh)
BUILD_TIMESTAMP := $(shell ./build/get-build-timestamp.sh)
@@ -26,10 +33,6 @@ LDFLAGS := "-X github.com/open-policy-agent/opa/version.Version=$(VERSION) \
-X github.com/open-policy-agent/opa/version.Timestamp=$(BUILD_TIMESTAMP) \
-X github.com/open-policy-agent/opa/version.Hostname=$(BUILD_HOSTNAME)"
# Set CROSSCOMPILE to space separated list of <platform>/<arch> pairs to
# have the build produce binaries for each platform/arch.
CROSSCOMPILE ?=
GO15VENDOREXPERIMENT := 1
export GO15VENDOREXPERIMENT
@@ -46,11 +49,15 @@ generate:
$(GO) generate
build: generate
ifeq ($(CROSSCOMPILE),)
$(GO) build -o opa -ldflags $(LDFLAGS)
else
@./build/cross-compile.sh --prefix opa --ldflags $(LDFLAGS) --platforms "$(CROSSCOMPILE)"
endif
$(GO) build -o $(BIN) -ldflags $(LDFLAGS)
image:
@$(MAKE) build GOOS=linux
sed -e 's/GOARCH/$(GOARCH)/g' Dockerfile.in > .Dockerfile_$(GOARCH)
docker build -t $(IMAGE):$(VERSION) -f .Dockerfile_$(GOARCH) .
push:
docker push $(IMAGE):$(VERSION)
install: generate
$(GO) install -ldflags $(LDFLAGS)
@@ -87,6 +94,5 @@ fmt:
$(GO) fmt $(PACKAGES)
clean:
rm -f ./opa
rm -f ./opa_linux_amd64
rm -f ./opa_darwin_amd64
rm -f opa_*_*
rm -f .Dockerfile_*
-61
View File
@@ -1,61 +0,0 @@
#!/usr/bin/env bash
set -ex
PROGRAM=$(basename $0)
function usage() {
echo ""
echo "$0 <arguments> [options]"
echo ""
echo "Runs 'go build ...' with flags needed to produce binaries for"
echo "other platforms, e.g., linux, darwin, windows, etc."
echo ""
echo "arguments:"
echo ""
echo " --prefix <prefix> (e.g., 'opa')"
echo " --platforms <GOOS_1>/<GOARCH_1> <GOOS_2>/<GOARCH_2> ... (e.g., 'linux/amd64 darwin/amd64')"
echo ""
echo "options:"
echo ""
echo " --ldflags <ldflags> (e.g., '-X ...')"
echo ""
}
while [[ $# -gt 0 ]]; do
key=$1
case $key in
--prefix)
PREFIX="$2"
shift
;;
--ldflags)
LDFLAGS="$2"
shift
;;
--platforms)
PLATFORMS="$2"
shift
;;
-h|--help)
usage
exit 0
;;
*)
;;
esac
shift
done
if [[ -z "$PREFIX" || -z "$PLATFORMS" ]]; then
usage
exit 1
fi
for x in $PLATFORMS; do
IFS='/' read -a platform <<< "$x"
GOOS=${platform[0]}
GOARCH=${platform[1]}
env GOOS=$GOOS GOARCH=$GOARCH go build -o ${PREFIX}_${GOOS}_${GOARCH} -ldflags "${LDFLAGS}"
done
+17 -4
View File
@@ -65,18 +65,31 @@ CHANGELOG.md snippet and uploading the packages from the build phase.
git checkout v<semver>
```
1. Run command to build packages. This will produce a bunch of binaries (e.g., amd64/linux, i386/linux, amd64/darwin, etc.) that can be published (“distributions”).
1. Build binaries for target platforms.
```
make build CROSSCOMPILE="linux/amd64 darwin/amd64"
make build GOOS=linux GOARCH=amd64
make build GOOS=darwin GOARCH=amd64
```
1. Build Docker image.
```
make image
```
## Publishing
1. Open browser and go to https://github.com/open-policy-agent/opa/releases
1. Create a new release for the version.
- Copy the changelog content into the message.
- Upload the distributions packages.
- Upload the binaries.
1. In addition to publishing the packages, there may be documentation updates that should be released. See [site/README.md](../site/README.md) for steps to update the website.
1. Push the Docker image.
```
make push
```
1. There may be documentation updates that should be released. See [site/README.md](../site/README.md) for steps to update the website.