From 30811bf861f96e22fd5d75c10250eab4b57283eb Mon Sep 17 00:00:00 2001 From: zikaeroh <48577114+zikaeroh@users.noreply.github.com> Date: Sat, 23 May 2020 15:14:58 -0700 Subject: [PATCH] Build workflow, version package --- .github/CODEOWNERS | 1 + .github/workflows/ci.yml | 154 ++++++++++++++++++++++++++++++++++++ Dockerfile | 4 +- internal/version/version.go | 12 +++ main.go | 10 ++- 5 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 .github/CODEOWNERS create mode 100644 .github/workflows/ci.yml create mode 100644 internal/version/version.go diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..a4e785e --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @zikaeroh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..4ca1e99 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,154 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +env: + GO_DEV_VERSION: "1.14" # Recommended Go version for development. + GOLANGCI_LINT_VERSION: "v1.26.0" + NODE_VERSION: "14" + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + go: ["1.14"] + name: Go ${{ matrix.go }} + + steps: + - uses: actions/checkout@v2 + + - name: Cache Go modules + uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('**/go.sum') }} + + - name: Install Go + uses: actions/setup-go@v2-beta + with: + go-version: ${{ matrix.go }} + + - name: Download Go modules + run: go mod download + + - name: Run tests + run: go test -race -covermode=atomic -coverprofile=coverage.txt ./... + + - name: Run 1x benchmarks + run: go test -run=- -bench . -benchtime=1x ./... + + style: + name: Style + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Cache Go modules + uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ env.GO_DEV_VERSION }}-${{ hashFiles('**/go.sum') }} + + - name: Install Go + uses: actions/setup-go@v2-beta + with: + go-version: ${{ env.GO_DEV_VERSION }} + + - name: Check go.mod tidyness + run: | + go mod tidy + git diff --exit-code go.mod go.sum + + - name: golangci-lint + run: | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin $GOLANGCI_LINT_VERSION + $(go env GOPATH)/bin/golangci-lint run --timeout 10m + + generate: + name: go generate + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Cache Go modules + uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ env.GO_DEV_VERSION }}-${{ hashFiles('**/go.sum') }} + + - name: Install Go + uses: actions/setup-go@v2-beta + with: + go-version: ${{ env.GO_DEV_VERSION }} + + - name: go generate + run: | + go generate ./... + git diff --exit-code + + build_frontend: + name: Build frontend + runs-on: ubuntu-latest + defaults: + run: + working-directory: frontend + + steps: + - uses: actions/checkout@v2 + + - uses: actions/setup-node@v2-beta + with: + node-version: ${{ env.NODE_VERSION }} + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "::set-output name=dir::$(yarn cache dir)" + + - uses: actions/cache@v1 + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: yarn install + run: yarn install --frozen-lockfile + + - name: yarn build + run: yarn build + + docker: + name: Docker + runs-on: ubuntu-latest + needs: [test, style, generate, build_frontend] + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Get version + run: | + export CODIES_VERSION="r$(git rev-list --count HEAD).$(git rev-parse --short HEAD)" + echo Version $CODIES_VERSION + echo "::set-env name=CODIES_VERSION::$CODIES_VERSION" + + - name: Build / push image + uses: whoan/docker-build-with-cache-action@v4 + with: + username: "${{ secrets.DOCKER_USERNAME }}" + password: "${{ secrets.DOCKER_PASSWORD }}" + image_name: zikaeroh/codies + image_tag: "latest,${{ env.CODIES_VERSION }}" + build_extra_args: "--build-arg=version=${{ env.CODIES_VERSION }}" + push_image_and_stages: ${{ github.repository == 'zikaeroh/codies' && github.event_name == 'push' && github.ref == 'refs/heads/master' }} diff --git a/Dockerfile b/Dockerfile index 865c71a..6c652fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,9 @@ RUN go mod download # Manually copying the required files to make this image's cache only include Go code. COPY ./main.go ./main.go COPY ./internal ./internal -RUN go build . + +ARG version +RUN go build -ldflags="-X github.com/zikaeroh/codies/internal/version.version=${version}" . # TODO: Use distroless/static and statically compile above. (https://golang.org/issue/26492) FROM gcr.io/distroless/base:nonroot diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..e52ebfb --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,12 @@ +// Package version provides a global version string. +package version + +var version string + +// Version returns a compile time version string, or "(devel)" if unset. +func Version() string { + if version == "" { + return "(devel)" + } + return version +} diff --git a/main.go b/main.go index bb8382b..37c2ffc 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "github.com/tomwright/queryparam/v4" "github.com/zikaeroh/codies/internal/protocol" "github.com/zikaeroh/codies/internal/server" + "github.com/zikaeroh/codies/internal/version" "golang.org/x/sync/errgroup" "nhooyr.io/websocket" ) @@ -31,11 +32,16 @@ var args = struct { } func main() { + rand.Seed(time.Now().Unix()) + log.SetFlags(log.LstdFlags | log.Lshortfile) + if _, err := flags.Parse(&args); err != nil { // Default flag parser prints messages, so just exit. os.Exit(1) } + log.Printf("starting codies server, version %s", version.Version()) + wsOpts := &websocket.AcceptOptions{ OriginPatterns: args.Origins, } @@ -45,10 +51,6 @@ func main() { wsOpts.OriginPatterns = []string{"*"} } - rand.Seed(time.Now().Unix()) - - log.SetFlags(log.LstdFlags | log.Lshortfile) - g, ctx := errgroup.WithContext(ctxutil.Interrupt()) srv := server.NewServer()