Skip to content

Commit

Permalink
feat: add Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 3, 2024
1 parent 9c5c0a3 commit 84fda34
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 63 deletions.
43 changes: 43 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool
*.out

# Go workspace file
go.work
go.work.sum

# Dependency directories
# vendor/

# Build results
dist/
*.prof

# IDE configuration files
.idea/
.vscode/

# Environment variable files
.env

# Temporary files
*.tmp
*.log
*.swp
*.swo
.DS_Store

# Configuration files
*.toml

# Exclude deployment-related files
deployments/
51 changes: 31 additions & 20 deletions .github/workflows/check.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
name: check
name: CI

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on: ["push"]
on:
push:

jobs:
check:
strategy:
matrix:
os: [ ubuntu-22.04 ]
go: [ '1.23' ]
os: [ubuntu-22.04]
go: ['1.23']
name: Check ${{ matrix.os }} @ Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- uses: actions/cache@v4

- name: Cache Go Modules and Build
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
Expand All @@ -30,11 +34,16 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- run: make init
- run: make lint
- run: make coverage
- name: Initialize Project
run: make init

- name: Run Linter
run: make lint

- name: Upload coverage to Codecov
- name: Run Tests and Generate Coverage
run: make coverage

- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand All @@ -46,44 +55,46 @@ jobs:
if: github.ref == 'refs/heads/main'
strategy:
matrix:
os: [ ubuntu-22.04 ]
go: [ '1.22' ]
name: Benchmark comparison ${{ matrix.os }} @ Go ${{ matrix.go }}
os: [ubuntu-22.04]
go: ['1.22']
name: Benchmark Comparison ${{ matrix.os }} @ Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
continue-on-error: true
steps:
- name: Checkout Code (Previous)
- name: Checkout Previous Code
uses: actions/checkout@v4
with:
ref: ${{ github.base_ref }}
path: previous

- name: Checkout Code (New)
- name: Checkout New Code
uses: actions/checkout@v4
with:
path: new

- name: Set up Go ${{ matrix.go }}
- name: Set up Go for Benchmarking
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: Install Dependencies

- name: Install Benchmark Dependencies
run: go install golang.org/x/perf/cmd/benchstat@latest

- name: Init (Previous)
- name: Initialize Previous Code
working-directory: previous
run: make init

- name: Run Benchmark (Previous)
working-directory: previous
run: make benchmark test-options="-count=2" | tee benchmark.txt

- name: Init (New)
- name: Initialize New Code
working-directory: new
run: make init

- name: Run Benchmark (New)
working-directory: new
run: make benchmark test-options="-count=2" | tee benchmark.txt

- name: Run Benchstat
- name: Compare Benchmark Results
run: benchstat previous/benchmark.txt new/benchmark.txt
41 changes: 26 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
# Edit at https://www.toptal.com/developers/gitignore?templates=go,intellij

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
Expand All @@ -15,23 +12,37 @@
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
# Output of the go coverage tool
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# Intellij
.idea
# Dependency directories (uncomment to include)
# vendor/

# Generated files
dist/
*.prof

### IDEs ###

# IntelliJ IDEA
.idea/

# Visual Studio Code
.vscode/

# Environment variable files
.env

# VS Code
.vscode
# Other temporary files
*.tmp
*.log
*.swp
*.swo
.DS_Store

#
dist
.*.toml
*.prof
### Configuration files ###
*.toml
78 changes: 50 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,72 +1,94 @@
-include .env

CURRENT_DIR = $(shell realpath .)
MODULE_DIRS = $(shell find $(CURRENT_DIR) -name go.mod -exec dirname {} \;)

DOCKER_IMAGE = $(shell basename -s .git $(shell git config --get remote.origin.url))
DOCKER_TAG = $(shell git tag --sort=-v:refname | grep -v '/' | head -n 1 || echo "latest")
DOCKERFILE = deployments/Dockerfile

CGO_ENABLED ?= 1

.PHONY: init generate build clean tidy update sync check test coverage benchmark lint fmt vet staticcheck doc docker-build

.PHONY: init
init:
@cp .go.work go.work
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go install -v ./...'
@$(MAKE) install-tools
@$(MAKE) install-modules

install-tools:
@go install honnef.co/go/tools/cmd/staticcheck@latest
@go install golang.org/x/tools/cmd/godoc@latest

.PHONY: generate
install-modules:
@for dir in $(MODULE_DIRS); do \
cd $$dir && go install -v ./...; \
done

generate:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go generate ./...'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go generate ./...; \
done

.PHONY: build
build:
@go clean -cache
@mkdir -p dist
@cd cmd && go build -ldflags "-s -w" -o ../dist ./...
@cd cmd && CGO_ENABLED=$(CGO_ENABLED) go build -ldflags "-s -w" -o ../dist ./...

.PHONY: clean
clean:
@go clean -cache
@rm -rf dist

.PHONY: tidy
tidy:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go mod tidy'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go mod tidy; \
done

.PHONY: update
update:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go get -u all && go mod tidy'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go get -u all && go mod tidy; \
done

.PHONY: sync
sync:
@go work sync

.PHONY: check
check: lint test

.PHONY: test
test:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go test -race $(test-options) ./...'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go test -race $(test-options) ./...; \
done

.PHONY: coverage
coverage:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go test -race --coverprofile=coverage.out --covermode=atomic $(test-options) ./...'
@find $(realpath .) -name go.mod | xargs dirname | grep '${CURRENT_DIR}/' | xargs -I {} sh -c 'cd {}; cat coverage.out >> '${CURRENT_DIR}/coverage.out' && rm coverage.out'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go test -race --coverprofile=coverage.out --covermode=atomic $(test-options) ./...; \
cat coverage.out >> $(CURRENT_DIR)/coverage.out && rm coverage.out; \
done

.PHONY: benchmark
benchmark:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go test -run="-" -bench=".*" -benchmem $(test-options) ./...'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go test -run="-" -bench=".*" -benchmem $(test-options) ./...; \
done

.PHONY: lint
lint: fmt vet staticcheck

.PHONY: fmt
fmt:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go fmt ./...'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go fmt ./...; \
done

.PHONY: vet
vet:
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; go vet ./...'
@for dir in $(MODULE_DIRS); do \
cd $$dir && go vet ./...; \
done

.PHONY: staticcheck
staticcheck: init
@find $(realpath .) -name go.mod | xargs dirname | xargs -I {} sh -c 'cd {}; staticcheck ./...'
@for dir in $(MODULE_DIRS); do \
cd $$dir && staticcheck ./...; \
done

.PHONY: doc
doc: init
@godoc -http=:6060

docker-build:
docker build --no-cache -t $(DOCKER_IMAGE):$(DOCKER_TAG) -f $(DOCKERFILE) $(CURRENT_DIR)
16 changes: 16 additions & 0 deletions deployments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.23 as builder

WORKDIR /app

COPY ./ .

RUN make init
RUN make build CGO_ENABLED=0

FROM alpine:latest

WORKDIR /root/

COPY --from=builder /app/dist/uniflow ./uniflow

ENTRYPOINT ["./uniflow"]

0 comments on commit 84fda34

Please sign in to comment.