From 9b2e96af7d2c92555bc1457ccf97f520f5452abf Mon Sep 17 00:00:00 2001 From: lesovsky Date: Sat, 19 Dec 2020 11:16:54 +0500 Subject: [PATCH] Implement releases: - add GH Actions default/releases workflows: run lint/test on Default, build docker images and binary on Release. - add goreleaser handler for releases events. - add git tag into app version string. - add Dockerfile. - add docker related commands into README. --- .github/workflows/default.yml | 19 ++++++++++++++ .github/workflows/release.yml | 47 +++++++++++++++++++++++++++++++++++ .goreleaser.yml | 15 +++++++++++ Dockerfile | 11 ++++++++ Makefile | 14 +++++++++-- README.md | 26 ++++++++++++------- cmd/main.go | 4 +-- 7 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/default.yml create mode 100644 .github/workflows/release.yml create mode 100644 .goreleaser.yml create mode 100644 Dockerfile diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml new file mode 100644 index 0000000..3c0bc4f --- /dev/null +++ b/.github/workflows/default.yml @@ -0,0 +1,19 @@ +--- +name: Default + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Run lint + run: make lint + - name: Run test + run: make test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..372e0d4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,47 @@ +--- +name: Release + +on: + push: + tags: [ v* ] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Run lint + run: make lint + - name: Run test + run: make test + + build: + runs-on: ubuntu-latest + needs: test + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Build image + run: make docker-build + - name: Log in to Docker Hub + run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + - name: Push image to Docker Hub + run: make docker-push + + goreleaser: + runs-on: ubuntu-latest + needs: [ test, build ] + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - uses: actions/setup-go@v2 + with: + go-version: 1.15 + - uses: goreleaser/goreleaser-action@v2 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..7826d67 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,15 @@ +builds: + - binary: noisia + goarch: [amd64] + goos: + - linux +archives: + - builds: [noisia] +changelog: + sort: asc +nfpms: + - vendor: noisia + homepage: https://github.com/lesovsky/noisia + maintainer: Alexey Lesovsky + description: Harmful workload generator for PostgreSQL + formats: [deb] \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d163ddc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +# stage 1: build +FROM golang:1.15 as build +LABEL stage=intermediate +WORKDIR /app +COPY . . +RUN make build + +# stage 2: scratch +FROM scratch as scratch +COPY --from=build /app/bin/noisia /bin/noisia +CMD ["noisia"] \ No newline at end of file diff --git a/Makefile b/Makefile index 6e947f8..46e01f9 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ APPNAME = noisia +TAG=$(shell git describe --tags |cut -d- -f1) COMMIT=$(shell git rev-parse --short HEAD) BRANCH=$(shell git rev-parse --abbrev-ref HEAD) -LDFLAGS = -a -installsuffix cgo -ldflags "-X main.appName=${APPNAME} -X main.gitCommit=${COMMIT} -X main.gitBranch=${BRANCH}" +LDFLAGS = -a -installsuffix cgo -ldflags "-X main.appName=${APPNAME} -X main.gitTag=${TAG} -X main.gitCommit=${COMMIT} -X main.gitBranch=${BRANCH}" -.PHONY: help clean lint test race build +.PHONY: help clean dep lint test build build-docker .DEFAULT_GOAL := help @@ -31,3 +32,12 @@ test: dep ## Run tests build: dep ## Build mkdir -p ./bin CGO_ENABLED=0 GOOS=linux GOARCH=${GOARCH} go build ${LDFLAGS} -o bin/${APPNAME} ./cmd + +docker-build: ## Build docker image + docker build -t lesovsky/${APPNAME}:${TAG} . + docker image prune --force --filter label=stage=intermediate + docker tag lesovsky/${APPNAME}:${TAG} lesovsky/${APPNAME}:latest + +docker-push: ## Push docker image to the registry + docker push lesovsky/${APPNAME}:${COMMIT} + docker push lesovsky/${APPNAME}:latest diff --git a/README.md b/README.md index d8d08c4..adebcf7 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,21 @@ - `failed connections` - exhaust connections pool (other clients can't connect to Postgres) - ...see built-in help for more runtime options. -#### Usage -Install `golang`, clone the repo and run `make build`. Check `bin/` directory for `noisia` executable. +#### Disclaimer + +ATTENTION: USE ONLY FOR TESTING PURPOSES, DO NOT EXECUTE NOISIA AGAINST PRODUCTION, RECKLESS USAGE WILL CAUSE PROBLEMS. + +DISCLAIMER: THIS SOFTWARE PROVIDED AS-IS WITH NO CARES AND GUARANTEES RELATED TO YOUR DATABASES. USE AT YOUR OWN RISK. + + +#### Installation and usage +Check out [releases](https://github.com/lesovsky/noisia/releases) page. + +#### Using Docker +```shell script +docker pull lesovsky/noisia:latest +docker run --rm -ti lesovsky/noisia:latest noisia --help +``` #### Using in your own code You can import `noisia` and use necessary workloads in your code. Always use contexts to avoid infinite run. See tiny example below: @@ -44,14 +57,9 @@ func main() { ``` #### TODO/Ideas: -- sequential scans - -#### Disclaimer - -ATTENTION: USE ONLY FOR TESTING PURPOSES, DO NOT EXECUTE NOISIA AGAINST PRODUCTION, RECKLESS USAGE WILL CAUSE PROBLEMS. - -DISCLAIMER: THIS SOFTWARE PROVIDED AS-IS WITH NO CARES AND GUARANTEES RELATED TO YOUR DATABASES. USE AT YOUR OWN RISK. +If you have any ideas, feel free to open a [discussion](https://github.com/lesovsky/noisia/discussions). +[ ] sequential scans #### License BSD-3. See [LICENSE](LICENSE) for more details. diff --git a/cmd/main.go b/cmd/main.go index 3466fd3..a9daa17 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,7 +12,7 @@ import ( ) var ( - appName, gitCommit, gitBranch string + appName, gitTag, gitCommit, gitBranch string ) func main() { @@ -47,7 +47,7 @@ func main() { logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}).Level(zerolog.InfoLevel).With().Timestamp().Logger() if *showVersion { - fmt.Printf("%s %s-%s\n", appName, gitCommit, gitBranch) + fmt.Printf("%s %s %s-%s\n", appName, gitTag, gitCommit, gitBranch) os.Exit(0) }