Skip to content

Commit

Permalink
Migrated to go 1.18 and imrovements in CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontes11 committed Jun 6, 2022
1 parent 26685ff commit a9573b7
Show file tree
Hide file tree
Showing 31 changed files with 339 additions and 2,093 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: CI

on:
pull_request:
branches:
- "**"
push:
branches:
- "**"

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Checkout code
uses: actions/checkout@v3

- name: GolangCI Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.46.2

build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Checkout code
uses: actions/checkout@v2

- name: Build
run: make build

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Checkout code
uses: actions/checkout@v2

- name: Test
run: make cover

- name: Coverage
uses: actions/upload-artifact@v2
with:
name: cover
path: ./cover.html
83 changes: 75 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,91 @@ name: Release
on:
push:
tags:
- "v*"
- "*"

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Fetch tags
run: git fetch --force --tags

- name: Setup QEMU
uses: docker/setup-qemu-action@v1

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1
id: buildx

- name: Check out code
uses: actions/checkout@v2
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Prepare
id: prep
run: |
VERSION=sha-${GITHUB_SHA::8}
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF/refs\/tags\//}
fi
echo ::set-output name=BUILD_DATE::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
echo ::set-output name=VERSION::${VERSION}
- name: Publish multi-arch Catalog Docker image
uses: docker/build-push-action@v2
with:
push: true
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
build-args: |
SERVICE=catalog
platforms: linux/arm64,linux/amd64
tags: |
docker.io/gotwaygateway/catalog:${{ steps.prep.outputs.VERSION }}
docker.io/gotwaygateway/catalog:latest
labels: |
org.opencontainers.image.title=catalog
org.opencontainers.image.description=${{ github.event.repository.description }}
org.opencontainers.image.source=${{ github.event.repository.html_url }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ steps.prep.outputs.VERSION }}
org.opencontainers.image.created=${{ steps.prep.outputs.BUILD_DATE }}
- name: Publish multi-arch Stock Docker image
uses: docker/build-push-action@v2
with:
push: true
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
build-args: |
SERVICE=stock
platforms: linux/arm64,linux/amd64
tags: |
docker.io/gotwaygateway/stock:${{ steps.prep.outputs.VERSION }}
docker.io/gotwaygateway/stock:latest
labels: |
org.opencontainers.image.title=stock
org.opencontainers.image.description=${{ github.event.repository.description }}
org.opencontainers.image.source=${{ github.event.repository.html_url }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ steps.prep.outputs.VERSION }}
org.opencontainers.image.created=${{ steps.prep.outputs.BUILD_DATE }}
- name: Release
- name: GoReleaser
uses: goreleaser/goreleaser-action@v3
with:
version: latest
args: release
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: ./scripts/release.sh
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
37 changes: 37 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
linters-settings:
dupl:
threshold: 100
gocyclo:
min-complexity: 15
lll:
line-length: 140
misspell:
locale: US

linters:
disable-all: true
enable:
- deadcode
- dupl
- errcheck
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- nestif
- staticcheck
- typecheck
- unused
- varcheck
# not compatible with go 1.18 yet
# - bodyclose
# - noctx
# - structcheck

run:
timeout: 5m
go: "1.18"
24 changes: 24 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
project_name: services
builds:
- id: catalog
main: ./cmd/catalog
binary: catalog
goos:
- linux
goarch:
- arm64
- amd64
env:
- CGO_ENABLED=0
- id: stock
main: ./cmd/stock
binary: stock
goos:
- linux
goarch:
- arm64
- amd64
env:
- CGO_ENABLED=0
archives:
- name_template: "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
16 changes: 10 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
FROM golang:1.15-alpine3.12 AS builder
FROM golang:1.18.3-alpine3.16 AS builder

ARG SERVICE

ENV WORKDIR /go/src/gotway
RUN test -n "$SERVICE" || (echo "Build argument \"SERVICE\" not set" && false)
ENV WORKDIR /go/src/services
RUN mkdir -p ${WORKDIR}
WORKDIR ${WORKDIR}

RUN apk update && \
apk add --no-cache --update make bash git ca-certificates && \
update-ca-certificates

COPY . .

RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' -o bin/app cmd/$SERVICE/*.go
RUN make build-$SERVICE

FROM alpine:3.12.0
FROM alpine:3.16.0

COPY --from=builder /go/src/gotway/bin/app /app
COPY --from=builder /go/src/services/bin/app /app

CMD [ "/app" ]
98 changes: 98 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
ROOT := $(shell git rev-parse --show-toplevel)
OS := $(shell uname -s | awk '{print tolower($$0)}')
ARCH := amd64
VERSION := $(shell git describe --abbrev=0 --tags)
LD_FLAGS := -X main.version=$(VERSION) -s -w
SOURCE_FILES ?= ./internal/... ./pkg/... ./cmd/...

export CGO_ENABLED := 0
export GO111MODULE := on
export GOBIN := $(shell pwd)/bin

.PHONY: all
all: help

.PHONY: help
help: ### Show targets documentation
ifeq ($(OS), linux)
@grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
else
@awk -F ':.*###' '$$0 ~ FS {printf "%15s%s\n", $$1 ":", $$2}' \
$(MAKEFILE_LIST) | grep -v '@awk' | sort
endif

GOLANGCI_LINT := $(GOBIN)/golangci-lint
GOLANGCI_LINT_VERSION := v1.46.2
golangci-lint:
$(call go-install,github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION))

GORELEASER := $(GOBIN)/goreleaser
GORELEASER_VERSION := v1.9.2
goreleaser:
$(call go-install,github.com/goreleaser/goreleaser@$(GORELEASER_VERSION))

.PHONY: generate
generate: vendor ### Generate code
@bash ./hack/hack.sh

.PHONY: docker-redis
docker-redis: ### Spin up docker redis
@docker compose -f docker-compose.redis.yml up -d

.PHONY: docker
docker: ### Spin up docker services
@docker compose -f docker-compose.redis.yml -f docker-compose.yml up -d

.PHONY: clean
clean: ### Clean build files
@rm -rf ./bin
@go clean

.PHONY: build
build: build-catalog build-stock ### Build all binaries

.PHONY: build-%
build-%: clean ### Build binary
@go build -tags netgo -a -v -ldflags "${LD_FLAGS}" -o ./bin/app ./cmd/$*/*.go
@chmod +x ./bin/*

.PHONY: run-%
run-%: lint ### Quick run
@CGO_ENABLED=1 go run -race cmd/$*/*.go

.PHONY: deps
deps: ### Optimize dependencies
@go mod tidy

.PHONY: vendor
vendor: ### Vendor dependencies
@go mod vendor

.PHONY: lint
lint: golangci-lint ### Lint
$(GOLANGCI_LINT) run

.PHONY: release
release: goreleaser ### Dry-run release
$(GORELEASER) release --snapshot --rm-dist

.PHONY: test-clean
test-clean: ### Clean test cache
@go clean -testcache ./...

.PHONY: test
test: ### Run tests
@go test -v -coverprofile=cover.out -timeout 10s ./...

.PHONY: cover
cover: test ### Run tests and generate coverage
@go tool cover -html=cover.out -o=cover.html

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install
@[ -f $(1) ] || { \
go install $(1) ; \
}
endef
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# service-examples
[![CI](https://github.com/gotway/service-examples/actions/workflows/ci.yml/badge.svg)](https://github.com/gotway/service-examples/actions/workflows/ci.yml)
[![Release](https://github.com/gotway/service-examples/actions/workflows/release.yml/badge.svg)](https://github.com/gotway/service-examples/actions/workflows/release.yml)

Service examples used for testing purposes
Expand All @@ -8,4 +9,3 @@ Service examples used for testing purposes
|-------|------|-----|
|[Catalog](./cmd/catalog)|[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/ac7596f337b868ab0e6c#?env%5BGotway%20Local%5D=W3sia2V5IjoidXJsIiwidmFsdWUiOiJodHRwczovL2xvY2FsaG9zdDo4MDAwIiwiZW5hYmxlZCI6dHJ1ZX0seyJrZXkiOiJ1cmxDYXRhbG9nIiwidmFsdWUiOiJodHRwOi8vbG9jYWxob3N0OjkwMDAiLCJlbmFibGVkIjp0cnVlfSx7ImtleSI6InVybFJvdXRlIiwidmFsdWUiOiJodHRwOi8vbG9jYWxob3N0OjExMDAwIiwiZW5hYmxlZCI6dHJ1ZX0seyJrZXkiOiJ1cmxTdG9jayIsInZhbHVlIjoiaHR0cDovL2xvY2FsaG9zdDoxMDAwMCIsImVuYWJsZWQiOnRydWV9LHsia2V5IjoicHJvZHVjdElkIiwidmFsdWUiOiIxMjM0IiwiZW5hYmxlZCI6dHJ1ZX0seyJrZXkiOiJwcm9kdWN0SWQyIiwidmFsdWUiOiI0NTYiLCJlbmFibGVkIjp0cnVlfSx7ImtleSI6InByb2R1Y3RJZDMiLCJ2YWx1ZSI6Ijc4OSIsImVuYWJsZWQiOnRydWV9XQ==)|[gotwaygateway/catalog](https://hub.docker.com/r/gotwaygateway/catalog/tags)|
|[Stock](./cmd/stock)|[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/bdb7fe928c1e93fb15e5#?env%5BGotway%20Local%5D=W3sia2V5IjoidXJsIiwidmFsdWUiOiJodHRwczovL2xvY2FsaG9zdDo4MDAwIiwiZW5hYmxlZCI6dHJ1ZX0seyJrZXkiOiJ1cmxDYXRhbG9nIiwidmFsdWUiOiJodHRwOi8vbG9jYWxob3N0OjkwMDAiLCJlbmFibGVkIjp0cnVlfSx7ImtleSI6InVybFJvdXRlIiwidmFsdWUiOiJodHRwOi8vbG9jYWxob3N0OjExMDAwIiwiZW5hYmxlZCI6dHJ1ZX0seyJrZXkiOiJ1cmxTdG9jayIsInZhbHVlIjoiaHR0cDovL2xvY2FsaG9zdDoxMDAwMCIsImVuYWJsZWQiOnRydWV9LHsia2V5IjoicHJvZHVjdElkIiwidmFsdWUiOiIxMjM0IiwiZW5hYmxlZCI6dHJ1ZX0seyJrZXkiOiJwcm9kdWN0SWQyIiwidmFsdWUiOiI0NTYiLCJlbmFibGVkIjp0cnVlfSx7ImtleSI6InByb2R1Y3RJZDMiLCJ2YWx1ZSI6Ijc4OSIsImVuYWJsZWQiOnRydWV9XQ==)|[gotwaygateway/stock](https://hub.docker.com/r/gotwaygateway/stock/tags)|
|[Route](./cmd/route)|[Go client](./cmd/route/client)|[gotwaygateway/route](https://hub.docker.com/r/gotwaygateway/route/tags)|
17 changes: 0 additions & 17 deletions cmd/catalog/Makefile

This file was deleted.

Loading

0 comments on commit a9573b7

Please sign in to comment.