Skip to content

Commit

Permalink
Migrated to travis-CI and simple build
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Jul 15, 2019
1 parent 6599631 commit 91d5408
Show file tree
Hide file tree
Showing 21 changed files with 458 additions and 409 deletions.
8 changes: 3 additions & 5 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.git
/.idea
*.iml
.idea
build/gopath
.gradle
vendor
/out
/var
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/.idea
*.iml
.idea
build
.gradle
vendor
/out
/dist
/var
42 changes: 42 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
language: go
go:
- 1.12.3
install: skip
os:
- linux
services:
- docker
env:
global:
- GO111MODULE=on
- CGO_ENABLED=0
- GOOS=linux
- GOARCH=amd64
cache:
directories:
- $HOME/.cache/go-build
- $HOME/gopath/pkg/mod
script: skip

jobs:
include:
- stage: test
name: Run Tests
script:
- go run ./build test
- stage: release
name: Release
if: tag =~ ^v\d+\.\d+\.\d+|snapshot-.+$
before_script:
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
script:
- go run ./build build-and-deploy
deploy:
provider: releases
api_key: "$GITHUB_DEPLOY_TOKEN"
file_glob: true
file: dist/*
skip_cleanup: true
name: $TRAVIS_TAG
on:
tags: true
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM scratch
MAINTAINER [email protected]

COPY build/out/site24x7_exporter-linux-amd64 /usr/bin/site24x7_exporter
ENV PATH=/usr/bin
COPY dist/site24x7_exporter-linux-amd64 /usr/bin/site24x7_exporter

ENTRYPOINT ["/usr/bin/site24x7_exporter"]
45 changes: 0 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,51 +123,6 @@ docker run -p9112:9112 -v/etc/certs:/etc/certs:ro echocat/site24x7_exporter \
| ``9`` | Discovery |
| ``10`` | Discovery Error |

## Build it

### Precondition

For building site24x7_exporter there is only:

1. a compatible operating system (Linux, Windows or Mac OS X)
2. and a working [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) installation required.

There is no need for a working and installed Go installation (or anything else). The build system will download every dependency and build it if necessary.

> **Hint:** The Go runtime build by the build system will be placed under ``~/.go/sdk``.
### Run build process

On Linux and Mac OS X:
```bash
# Build binaries (includes test)
./gradlew build

# Run tests (but do not build binaries)
./gradlew test

# Build binaries and release it on GitHub
# Environment variable GITHUB_TOKEN is required
./gradlew build githubRelease
```

On Windows:
```bash
# Build binaries (includes test)
gradlew build

# Run tests (but do not build binaries)
gradlew test

# Build binaries and release it on GitHub
# Environment variable GITHUB_TOKEN is required
gradlew build githubRelease
```

### Build artifacts

* Compiled and lined binaries can be found under ``./build/out/site24x7_exporter-*``

## Contributing

site24x7_exporter is an open source project of [echocat](https://echocat.org).
Expand Down
45 changes: 0 additions & 45 deletions build.gradle

This file was deleted.

78 changes: 78 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
)

func build(branch, commit string) {
buildBinaries(branch, commit)

if withDocker {
buildDocker(branch, dv, false)
tagDocker(branch, dv, false)
}
}

func buildBinaries(branch, commit string) {
for _, t := range targets {
buildBinary(branch, commit, t, false)
}
}

func buildBinary(branch, commit string, t target, forTesting bool) {
ldFlags := buildLdFlagsFor(branch, commit, forTesting)
outputName := t.outputName()
must(os.MkdirAll(filepath.Dir(outputName), 0755))
executeTo(func(cmd *exec.Cmd) {
cmd.Env = append(os.Environ(), "GOOS="+t.os, "GOARCH="+t.arch)
}, os.Stderr, os.Stdout, "go", "build", "-ldflags", ldFlags, "-o", outputName, ".")
}

func buildLdFlagsFor(branch, commit string, forTesting bool) string {
testPrefix := ""
testSuffix := ""
if forTesting {
testPrefix = "TEST"
testSuffix = "TEST"
}
return fmt.Sprintf("-X main.version=%s%s%s", testPrefix, branch, testSuffix) +
fmt.Sprintf(" -X main.revision=%s%s%s", testPrefix, commit, testSuffix) +
fmt.Sprintf(" -X main.compiled=%s", startTime.Format("2006-01-02T15:04:05Z"))
}

func buildDocker(branch string, v dockerVariant, forTesting bool) {
version := branch
if forTesting {
version = "TEST" + version + "TEST"
}
execute("docker", "build", "-t", v.imageName(version), "-f", v.dockerFile, "--build-arg", "image="+imagePrefix, "--build-arg", "version="+version, ".")
}

func tagDocker(branch string, v dockerVariant, forTesting bool) {
version := branch
if forTesting {
version = "TEST" + version + "TEST"
}
executeForVersionParts(version, func(tagSuffix string) {
tagDockerWith(version, v, v.imageName(tagSuffix))
})
if latestVersionPattern != nil && latestVersionPattern.MatchString(version) {
tagDockerWith(version, v, v.baseImageName())
}
if v.main {
tagDockerWith(version, v, imagePrefix+":"+version)
executeForVersionParts(version, func(tagSuffix string) {
tagDockerWith(version, v, imagePrefix+":"+tagSuffix)
})
if latestVersionPattern != nil && latestVersionPattern.MatchString(version) {
tagDockerWith(version, v, imagePrefix+":latest")
}
}
}

func tagDockerWith(branch string, v dockerVariant, tag string) {
execute("docker", "tag", v.imageName(branch), tag)
}
28 changes: 28 additions & 0 deletions build/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

func deploy(branch string) {
deployDocker(branch, dv)
}

func deployDocker(branch string, v dockerVariant) {
deployDockerTag(v.imageName(branch))
executeForVersionParts(branch, func(tagSuffix string) {
deployDockerTag(v.imageName(tagSuffix))
})
if latestVersionPattern != nil && latestVersionPattern.MatchString(branch) {
deployDockerTag(v.baseImageName())
}
if v.main {
deployDockerTag(imagePrefix + ":" + branch)
executeForVersionParts(branch, func(tagSuffix string) {
deployDockerTag(imagePrefix + ":" + tagSuffix)
})
if latestVersionPattern != nil && latestVersionPattern.MatchString(branch) {
deployDockerTag(imagePrefix + ":latest")
}
}
}

func deployDockerTag(tag string) {
execute("docker", "push", tag)
}
60 changes: 60 additions & 0 deletions build/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"flag"
"fmt"
"os"
"regexp"
)

var (
branch = "snapshot"
commit = "unknown"
withDocker = true
plainLatestVersionPattern string
latestVersionPattern *regexp.Regexp
)

func init() {
flag.StringVar(&branch, "branch", os.Getenv("TRAVIS_BRANCH"), "something like either main, v1.2.3 or snapshot-feature-foo")
flag.StringVar(&commit, "commit", os.Getenv("TRAVIS_COMMIT"), "something like 463e189796d5e96a7b605ab51985458faf8fd0d4")
flag.BoolVar(&withDocker, "withDocker", true, "enables docker tests and builds")
flag.StringVar(&plainLatestVersionPattern, "latestVersionPattern", os.Getenv("LATEST_VERSION_PATTERN"), "everything what matches here will be a latest tag")
}

func main() {
flag.Parse()
if branch == "" {
branch = "development"
}
if commit == "" {
commit = "development"
}
if compiled, err := regexp.Compile(plainLatestVersionPattern); err != nil {
panic(err)
} else {
latestVersionPattern = compiled
}
fArgs := flag.Args()
if len(fArgs) != 1 {
usage()
}
switch fArgs[0] {
case "build":
build(branch, commit)
case "test":
test(branch, commit)
case "deploy":
deploy(branch)
case "build-and-deploy":
build(branch, commit)
deploy(branch)
default:
usage()
}
}

func usage() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [flags] <command>", os.Args[0])
os.Exit(1)
}
Loading

0 comments on commit 91d5408

Please sign in to comment.