From 0fd35f9668e51de802af423c313ed490b56d607a Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Wed, 8 Mar 2023 10:02:56 +0000 Subject: [PATCH] build: update ci build stages --- .dockerignore | 10 +++ .github/workflows/binary_release.yaml | 37 ---------- .github/workflows/docker_release.yaml | 29 -------- .github/workflows/release.yaml | 100 ++++++++++++++++++++++++++ .goreleaser.yaml | 2 +- Dockerfile | 31 +++++--- cmd/service/main.go | 4 +- internal/sub/js_sub.go | 1 - 8 files changed, 135 insertions(+), 79 deletions(-) create mode 100644 .dockerignore delete mode 100644 .github/workflows/binary_release.yaml delete mode 100644 .github/workflows/docker_release.yaml create mode 100644 .github/workflows/release.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0720f68 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.github +.deepsource.toml +.goreleaser.yaml + +**/.env +**/.git +**/.gitignore + +**/docker-compose* +**/Dockerfile* \ No newline at end of file diff --git a/.github/workflows/binary_release.yaml b/.github/workflows/binary_release.yaml deleted file mode 100644 index 5b4912a..0000000 --- a/.github/workflows/binary_release.yaml +++ /dev/null @@ -1,37 +0,0 @@ -name: binary_release - -on: - push: - tags: - - "v*" - -jobs: - goreleaser: - runs-on: ubuntu-latest - container: - image: goreleaser/goreleaser-cross - environment: build - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - name: Workaround Git Security Warning - run: | - # Workaround a bug in github actions: - # https://github.com/actions/runner-images/issues/6775. - git config --global --add safe.directory "$GITHUB_WORKSPACE" - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.20.1 - - - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v2 - with: - version: latest - args: release --rm-dist - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/docker_release.yaml b/.github/workflows/docker_release.yaml deleted file mode 100644 index 8471fa8..0000000 --- a/.github/workflows/docker_release.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: docker_release - -on: - push: - tags: - - "v*" - -jobs: - build-publish: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - uses: docker/login-action@v1.14.1 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: docker compose build push - env: - DOCKER_BUILDKIT: 1 - COMPOSE_DOCKER_CLI_BUILD: 1 - run: | - export TAG=$GITHUB_REF_NAME - docker-compose -f docker-compose.build.yaml build --progress plain - docker-compose -f docker-compose.build.yaml push - export TAG=latest - docker-compose -f docker-compose.build.yaml build --progress plain - docker-compose -f docker-compose.build.yaml push diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..e49c451 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,100 @@ +name: release + +on: + push: + tags: + - 'v*' + +jobs: + docker: + runs-on: ubuntu-latest + steps: + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Check out repo + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Login to GHCR Docker registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set outputs + run: | + echo "RELEASE_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + echo "RELEASE_SHORT_COMMIT=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + + - name: Build and push image + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + platforms: linux/amd64 + push: true + build-args: |- + BUILD_COMMIT=${{ env.RELEASE_SHORT_COMMIT }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache + tags: | + ghcr.io/grassrootseconomics/cic-custodial/cic-custodial:latest + ghcr.io/grassrootseconomics/cic-custodial/cic-custodial:v${{ env.RELEASE_TAG }} + + goreleaser: + runs-on: ubuntu-latest + container: + image: goreleaser/goreleaser-cross + environment: build + + steps: + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - uses: actions/cache@v3 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/Library/Caches/go-build + %LocalAppData%\go-build + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Workaround Git Security Warning + run: | + # Workaround a bug in github actions: + # https://github.com/actions/runner-images/issues/6775. + git config --global --add safe.directory "$GITHUB_WORKSPACE" + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 'stable' + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d72234f..6e994fb 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,7 +10,7 @@ builds: - amd64 main: ./cmd/service ldflags: - - -s -w + - -X main.build={{.ShortCommit}} -s -w archives: - format: tar.gz diff --git a/Dockerfile b/Dockerfile index 6e301f1..ed46005 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,31 @@ -# build FROM golang:1-bullseye as build -WORKDIR /build -COPY . . -RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o cic-custodial -ldflags="-s -w" cmd/service/*.go -# main +ENV CGO_ENABLED=1 +ENV GOOS=linux +ENV GOARCH=amd64 + +ENV BUILD_COMMIT=${BUILD_COMMIT} + +WORKDIR /build + +COPY go.* . +RUN go mod download + +COPY . . +RUN go build -o cic-custodial -ldflags="-X main.build=${BUILD_COMMIT} -s -w" cmd/service/* + FROM debian:bullseye-slim + ENV DEBIAN_FRONTEND=noninteractive -RUN set -x && \ - apt-get update && \ - apt-get install -y ca-certificates && \ - rm -rf /var/cache/apt/archives /var/lib/apt/lists/* + WORKDIR /service + +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=build /build/cic-custodial . COPY migrations migrations/ COPY config.toml . COPY queries.sql . -CMD ["./cic-custodial"] + EXPOSE 5000 + +CMD ["./cic-custodial"] diff --git a/cmd/service/main.go b/cmd/service/main.go index dfa83f2..4f582ff 100644 --- a/cmd/service/main.go +++ b/cmd/service/main.go @@ -23,6 +23,8 @@ type ( ) var ( + build string + confFlag string debugFlag bool migrationsFolderFlag string @@ -34,7 +36,7 @@ var ( func init() { flag.StringVar(&confFlag, "config", "config.toml", "Config file location") - flag.BoolVar(&debugFlag, "log", false, "Enable debug logging") + flag.BoolVar(&debugFlag, "debug", false, "Enable debug logging") flag.StringVar(&migrationsFolderFlag, "migrations", "migrations/", "Migrations folder location") flag.StringVar(&queriesFlag, "queries", "queries.sql", "Queries file location") flag.Parse() diff --git a/internal/sub/js_sub.go b/internal/sub/js_sub.go index 022ef50..29a45a8 100644 --- a/internal/sub/js_sub.go +++ b/internal/sub/js_sub.go @@ -68,7 +68,6 @@ func (s *Sub) Process() error { if err != nil { if errors.Is(err, nats.ErrTimeout) { s.logg.Debug("sub: no msg to pull") - time.Sleep(waitDelay) continue } else if errors.Is(err, nats.ErrConnectionClosed) { return nil