Skip to content

Commit

Permalink
Add CI/Release automation with GitHub Actions (#1)
Browse files Browse the repository at this point in the history
This change makes the CI to be run by GitHub Actions.

This also automates the release process and builds the artifacts so that
I don't have to run a weird script on my machine every time.
  • Loading branch information
lhchavez authored May 24, 2020
1 parent 4acc444 commit 628d951
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 33 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI

on:
pull_request: {}
push:
branches:
- master

jobs:

test:
strategy:
fail-fast: false
matrix:
go:
- 1.13
- 1.14
name: Go ${{ matrix.go }}

runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
id: go

- name: Install golint
run: go install golang.org/x/lint/golint

- name: Lint
run: golint -set_exit_status ./...

- name: Get dependencies
run: go get -tags=static -t -v ./...

- name: Vet
run: go vet -tags=static -v ./...

- name: Test
run: go test -tags=static -v -race -coverprofile=coverage.txt -covermode=atomic ./...

- name: Ensure formatting
run: |
if [[ $(git ls-tree -r HEAD^{tree} . --full-name --name-only | \
grep '\.go$' | \
xargs -n 1 gofmt -d | \
wc -c) -ne 0 \
]]; then
echo "please run gofmt on all the files"
exit 1
fi
- name: Upload code coverage
run: bash <(curl -s https://codecov.io/bash)
66 changes: 66 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Release

on:
push:
branches:
- master
tags:
- v*

jobs:

release:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.13
id: go

- name: Get dependencies
run: go get -tags=static -t -v ./...

- name: Bump version and push tag
id: bump-version
uses: anothrNick/github-tag-action@c170e78287f338a4af0dc49e033e50e5a072d82b
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WITH_V: true
DEFAULT_BUMP: patch

- name: Build
run: |
mkdir -p artifacts/usr/bin/
go build -o artifacts/usr/bin/omegaup-logslurp \
-ldflags "-X main.ProgramVersion=${{ steps.bump-version.outputs.tag }}" \
-tags=static \
github.com/omegaup/logslurp/cmd/omegaup-logslurp
- name: Package
run: |
tar -cJf omegaup-logslurp.tar.xz --owner=root:0 --group=root:0 -C artifacts/ .
- name: Create Release
id: create-release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.bump-version.outputs.tag }}
release_name: ${{ steps.bump-version.outputs.tag }}
draft: false
prerelease: false

- name: Upload omegaup-logslurp.tar.xz Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create-release.outputs.upload_url }}
asset_path: ./omegaup-logslurp.tar.xz
asset_name: omegaup-logslurp.tar.xz
asset_content_type: application/octet-stream
8 changes: 4 additions & 4 deletions cmd/omegaup-logslurp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func readLogslurpConfig(configPath string) (*logslurpConfig, error) {
},
OffsetFilePath: "/var/lib/omegaup/logslurp_offsets.json",
}
if err := readJson(configPath, &config); err != nil {
if err := readJSON(configPath, &config); err != nil {
return nil, err
}
if config.StreamsDirectory != "" {
Expand All @@ -43,7 +43,7 @@ func readLogslurpConfig(configPath string) (*logslurpConfig, error) {
continue
}
var streamConfig logslurp.StreamConfig
if err := readJson(path.Join(config.StreamsDirectory, directoryEntry.Name()), &streamConfig); err != nil {
if err := readJSON(path.Join(config.StreamsDirectory, directoryEntry.Name()), &streamConfig); err != nil {
return nil, err
}
config.Streams = append(config.Streams, &streamConfig)
Expand Down Expand Up @@ -77,7 +77,7 @@ func readOffsetMapping(offsetMappingPath string) (offsetMapping, error) {
offsets := offsetMapping{
Offsets: make(map[string]fileOffset),
}
err := readJson(offsetMappingPath, &offsets)
err := readJSON(offsetMappingPath, &offsets)
return offsets, err
}

Expand All @@ -96,7 +96,7 @@ func (o *offsetMapping) write(offsetMappingPath string) error {
return nil
}

func readJson(jsonPath string, v interface{}) error {
func readJSON(jsonPath string, v interface{}) error {
f, err := os.Open(jsonPath)
if err != nil {
if os.IsNotExist(err) {
Expand Down
1 change: 1 addition & 0 deletions cmd/omegaup-logslurp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
logEntryFlushInterval = 5 * time.Second
)

// Stream represents the streamed version of a log file.
type Stream struct {
config *logslurp.StreamConfig
tail *logslurp.Tail
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/omegaup/slurp
module github.com/omegaup/logslurp

go 1.13

require (
github.com/coreos/go-systemd/v22 v22.0.0
github.com/fsnotify/fsnotify v1.4.7
github.com/inconshreveable/log15 v0.0.0-20200109203555-b30bc20e4fd1
github.com/omegaup/go-base v1.0.1
github.com/pkg/errors v0.8.1
golang.org/x/sys v0.0.0-20190520201301-c432e742b0af // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
github.com/coreos/go-systemd/v22 v22.0.0 h1:XJIw/+VlJ+87J+doOxznsAWIdmWuViOVhkQamW5YV28=
github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/inconshreveable/log15 v0.0.0-20200109203555-b30bc20e4fd1 h1:KUDFlmBg2buRWNzIcwLlKvfcnujcHQRQ1As1LoaCLAM=
github.com/inconshreveable/log15 v0.0.0-20200109203555-b30bc20e4fd1/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/omegaup/go-base v1.0.1 h1:7771a0jnJm4CPuK2lJxb56w03ASM1BkYLLfnYwhPagA=
github.com/omegaup/go-base v1.0.1/go.mod h1:KRWOUExqxHkr/h1GiHEtAKGp4rx6mOGWsBSlN636INY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190520201301-c432e742b0af h1:NXfmMfXz6JqGfG3ikSxcz2N93j6DgScr19Oo2uwFu88=
golang.org/x/sys v0.0.0-20190520201301-c432e742b0af/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191206220618-eeba5f6aabab h1:FvshnhkKW+LO3HWHodML8kuVX8rnJTxKm9dFPuI68UM=
golang.org/x/sys v0.0.0-20191206220618-eeba5f6aabab/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
13 changes: 7 additions & 6 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (e *PushRequestStreamEntry) String() string {
)
}

// MarshalJSON returns the JSON encoding of the PushRequestStreamEntry.
func (e *PushRequestStreamEntry) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(
"[\"%d\", %q]",
Expand Down Expand Up @@ -83,11 +84,11 @@ func NewLogStream(rd io.RuneReader, config *StreamConfig) (*LogStream, error) {
rd: rd,
config: config,
}
if r, err := regexp.Compile(s.config.RegexpString); err != nil {
r, err := regexp.Compile(s.config.RegexpString)
if err != nil {
return nil, err
} else {
s.regexp = r
}
s.regexp = r
return s, nil
}

Expand Down Expand Up @@ -158,11 +159,11 @@ func (s *LogStream) Read() (*PushRequestStream, error) {
}
group := t.w.String()[groupPairs[2*i]:groupPairs[2*i+1]]
if label == "ts" {
if ts, err := time.Parse(s.config.TimestampLayout, group); err != nil {
ts, err := time.Parse(s.config.TimestampLayout, group)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse timestamp %q", group)
} else {
entry.Timestamp = ts
}
entry.Timestamp = ts
} else if label != "" {
labels[label] = group
} else {
Expand Down
42 changes: 21 additions & 21 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ var _ io.ReadCloser = (*Tail)(nil)
// This means that the calls to Read() will never return EOF, unless Stop() is
// called.
func NewTail(name string, off int64, log log15.Logger) (*Tail, error) {
if resolved, err := filepath.Abs(name); err != nil {
resolved, err := filepath.Abs(name)
if err != nil {
return nil, err
} else {
name = resolved
}
name = resolved
parent := filepath.Dir(name)

t := &Tail{
Expand All @@ -87,11 +87,11 @@ func NewTail(name string, off int64, log log15.Logger) (*Tail, error) {
chunks: make(chan *chunk),
log: log,
}
if watcher, err := fsnotify.NewWatcher(); err != nil {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
} else {
t.watcher = watcher
}
t.watcher = watcher

go t.run()

Expand All @@ -100,20 +100,20 @@ func NewTail(name string, off int64, log log15.Logger) (*Tail, error) {
t.Close()
return nil, errors.Wrapf(err, "could not watch %q", parent)
}
if f, filesize, inode, err := openNonBlocking(name); err != nil {
f, filesize, inode, err := openNonBlocking(name)
if err != nil {
t.Stop()
t.Close()
return nil, errors.Wrapf(err, "could not open %q", name)
} else {
atomic.StoreUint64(&t.inode, inode)
t.file = f
if t.off > filesize {
// If the provided offset is larger than the file size, that means that the
// file was truncated. We will read from the beginning.
atomic.StoreInt64(&t.off, 0)
}
}

atomic.StoreUint64(&t.inode, inode)
t.file = f
if t.off > filesize {
// If the provided offset is larger than the file size, that means that the
// file was truncated. We will read from the beginning.
atomic.StoreInt64(&t.off, 0)
}
return t, nil
}

Expand Down Expand Up @@ -231,15 +231,15 @@ func (t *Tail) run() {
if event.Op&fsnotify.Create == fsnotify.Create {
// But if the file was re-created, close it and open it again.
t.file.Close()
if f, _, inode, err := openNonBlocking(t.name); err != nil {
f, _, inode, err := openNonBlocking(t.name)
if err != nil {
reportedError = err
return
} else {
t.log.Info("file was re-created", "path", t.name)
t.file = f
atomic.StoreInt64(&t.off, 0)
atomic.StoreUint64(&t.inode, inode)
}
t.log.Info("file was re-created", "path", t.name)
t.file = f
atomic.StoreInt64(&t.off, 0)
atomic.StoreUint64(&t.inode, inode)
}

case err, ok := <-t.watcher.Errors:
Expand Down

0 comments on commit 628d951

Please sign in to comment.