Skip to content

Commit

Permalink
improve release
Browse files Browse the repository at this point in the history
  • Loading branch information
labkode committed Oct 24, 2019
1 parent 0f8f184 commit 2faf402
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 30 deletions.
39 changes: 13 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ GIT_BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
GIT_DIRTY=`git diff-index --quiet HEAD -- || echo "dirty-"`
VERSION=`git describe --always`
GO_VERSION=`go version | awk '{print $$3}'`
BUILD_PLATFORM=`go version | awk '{print $$4}'`

LDFLAGS=-ldflags "-s -X main.buildDate=${BUILD_DATE} -X main.gitCommit=${GIT_DIRTY}${GIT_COMMIT} -X main.gitBranch=${GIT_BRANCH} -X main.version=${VERSION} -X main.goVersion=${GO_VERSION} -X main.buildPlatform=${BUILD_PLATFORM}"

off:
GORPOXY=off
Expand All @@ -25,17 +22,17 @@ imports: off
goimports -w tools pkg internal cmd

build: imports
go build -mod=vendor -o ./cmd/revad/revad ${LDFLAGS} ./cmd/revad
go build -mod=vendor -o ./cmd/reva/reva ${LDFLAGS} ./cmd/reva
go build -mod=vendor -o ./cmd/revad/revad ./cmd/revad
go build -mod=vendor -o ./cmd/reva/reva ./cmd/reva

tidy:
go mod tidy

build-revad: imports
go build -mod=vendor -o ./cmd/revad/revad ${LDFLAGS} ./cmd/revad
go build -mod=vendor -o ./cmd/revad/revad ./cmd/revad

build-reva: imports
go build -mod=vendor -o ./cmd/reva/reva ${LDFLAGS} ./cmd/reva
go build -mod=vendor -o ./cmd/reva/reva ./cmd/reva

test: off
go test -mod=vendor -race ./...
Expand All @@ -53,8 +50,9 @@ deps:
cd /tmp && go get golang.org/x/tools/cmd/goimports

build-ci: off
go build -mod=vendor -o ./cmd/revad/revad ${LDFLAGS} ./cmd/revad
go build -mod=vendor -o ./cmd/reva/reva ${LDFLAGS} ./cmd/reva
go build -mod=vendor -o ./cmd/revad/revad ./cmd/revad
go build -mod=vendor -o ./cmd/reva/reva ./cmd/reva

lint-ci:
go run tools/check-license/check-license.go

Expand All @@ -64,27 +62,16 @@ ci: build-ci test lint-ci

# to be run in Docker build
build-revad-docker: off
go build -mod=vendor -o ./cmd/revad/revad ${LDFLAGS} ./cmd/revad
go build -mod=vendor -o ./cmd/revad/revad ./cmd/revad
build-reva-docker: off
go build -mod=vendor -o ./cmd/revad/reva ${LDFLAGS} ./cmd/reva
go build -mod=vendor -o ./cmd/revad/reva ./cmd/reva
clean:
rm -rf dist

# for releasing you need to run: go run tools/release/main.go
# for releasing you need to run: go run tools/prepare-release/main.go
release-deps:
cd /tmp && go get github.com/restic/calens

# usually to be run from CI, it will push artefacts to github releases
dist:
rm -rf dist && mkdir dist
GOOS=linux GOARCH=amd64 go build -mod=vendor -o ./dist/revad_${VERSION}_linux_amd64 ${LDFLAGS} ./cmd/revad
GOOS=linux GOARCH=386 go build -mod=vendor -o ./dist/revad_${VERSION}_linux_386 ${LDFLAGS} ./cmd/revad
GOOS=darwin GOARCH=amd64 go build -mod=vendor -o ./dist/revad_${VERSION}_darwin_amd64 ${LDFLAGS} ./cmd/revad
GOOS=darwin GOARCH=386 go build -mod=vendor -o ./dist/revad_${VERSION}_darwin_386 ${LDFLAGS} ./cmd/revad

GOOS=linux GOARCH=amd64 go build -mod=vendor -o ./dist/reva_${VERSION}_linux_amd64 ${LDFLAGS} ./cmd/reva
GOOS=linux GOARCH=386 go build -mod=vendor -o ./dist/reva_${VERSION}_linux_386 ${LDFLAGS} ./cmd/reva
GOOS=darwin GOARCH=amd64 go build -mod=vendor -o ./dist/reva_${VERSION}_darwin_amd64 ${LDFLAGS} ./cmd/reva
GOOS=darwin GOARCH=386 go build -mod=vendor -o ./dist/reva_${VERSION}_darwin_386 ${LDFLAGS} ./cmd/reva

cd dist && sha256sum * > checksums-sha256.txt
# create local build versions
dist: off
go run tools/create-artifacts/main.go -version ${VERSION} -commit ${GIT_COMMIT} -branch ${GIT_BRANCH} -goversion ${GO_VERSION}
7 changes: 3 additions & 4 deletions cmd/revad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (
pidFlag = flag.String("p", "", "pid file. If empty defaults to a random file in the OS temporary directory")

// Compile time variables initialez with gcc flags.
gitCommit, gitBranch, buildDate, version, goVersion, buildPlatform string
gitCommit, gitBranch, buildDate, version, goVersion string
)

type coreConf struct {
Expand Down Expand Up @@ -192,10 +192,9 @@ func getVersionString() string {
msg += "commit=%s "
msg += "branch=%s "
msg += "go_version=%s "
msg += "build_date=%s "
msg += "build_platform=%s"
msg += "build_date=%s"

return fmt.Sprintf(msg, version, gitCommit, gitBranch, goVersion, buildDate, buildPlatform)
return fmt.Sprintf(msg, version, gitCommit, gitBranch, goVersion, buildDate)
}

func handleVersionFlag() {
Expand Down
113 changes: 113 additions & 0 deletions tools/create-artifacts/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2018-2019 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"time"
)

var (
commit = flag.String("commit", "", "sets git commit")
branch = flag.String("branch", "", "sets git branch")
version = flag.String("version", "", "sets git version")
goVersion = flag.String("goversion", "", "sets go version")
buildDate = time.Now().Format("2006-01-02")

binaries = []string{"reva", "revad"}
archs = []string{"386", "amd64"}
oses = []string{"linux", "darwin"}
)

func init() {
flag.Parse()

if *commit == "" || *branch == "" || *version == "" || *goVersion == "" {
fmt.Fprint(os.Stderr, "fill all the flags\n")
os.Exit(1)
}
}

func main() {

os.RemoveAll("dist")
os.MkdirAll("dist", 0755)

ldFlags := fmt.Sprintf("-s -X main.buildDate=%s -X main.gitCommit=%s -X main.gitBranch=%s -X main.version=%s -X main.goVersion=%s",
buildDate,
*commit,
*branch,
*version,
*goVersion,
)

for _, bin := range binaries {
for _, o := range oses {
for _, arch := range archs {
out := fmt.Sprintf("./dist/%s_%s_%s_%s", bin, *version, o, arch)
args := []string{"build", "-mod=vendor", "-o", out, "-ldflags", ldFlags, "./cmd/" + bin}
cmd := exec.Command("go", args...)
cmd.Env = append(os.Environ())
cmd.Env = append(cmd.Env, []string{"GOOS=" + o, "GOARCH=" + arch}...)
cmd.Dir = "." // root of the repo
run(cmd)
hashFile(out)
}
}
}
}

func run(cmd *exec.Cmd) {
var b bytes.Buffer
mw := io.MultiWriter(os.Stdout, &b)
cmd.Stdout = mw
cmd.Stderr = mw
err := cmd.Run()
fmt.Println(cmd.Dir, cmd.Args)
fmt.Println(b.String())
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
}

func hashFile(file string) {
hasher := sha256.New()
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if _, err := io.Copy(hasher, f); err != nil {
log.Fatal(err)
}
val := hex.EncodeToString(hasher.Sum(nil))
if err := ioutil.WriteFile(file+".sha256", []byte(val), 0644); err != nil {
log.Fatal(err)
}
}
File renamed without changes.
Binary file removed tools/release/release
Binary file not shown.

0 comments on commit 2faf402

Please sign in to comment.