-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MAINT] initial work on local dev env (#13)
* initial work on local dev env * minor changes on makefile * pre-commit x permission added * readme updated and minor improvments in docker conf
- Loading branch information
1 parent
b7b9f53
commit f58ceac
Showing
11 changed files
with
293 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env sh | ||
|
||
echo Running pre-commit hook | ||
|
||
# Check for required binaries, otherwise don't run | ||
if ! command -v grep &> /dev/null | ||
then | ||
echo "grep could not be found - skipping pre-commit" | ||
exit 0 | ||
fi | ||
|
||
if ! command -v sed &> /dev/null | ||
then | ||
echo "sed could not be found - skipping pre-commit" | ||
exit 0 | ||
fi | ||
|
||
if ! command -v xargs &> /dev/null | ||
then | ||
echo "xargs could not be found - skipping pre-commit" | ||
exit 0 | ||
fi | ||
|
||
# Run pre-commit, this checks if we changed any golang files and runs the checks. | ||
# The files are then git-added | ||
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep .go | sed 's| |\\ |g') | ||
if [ -n "$FILES" ]; then | ||
make check | ||
if [ $? -ne 0 ]; then | ||
echo "Error running make check - please fix before committing" | ||
echo "if this is a mistake you can skip the checks with 'git commit --no-verify'" | ||
exit 1 | ||
fi | ||
echo "$FILES" | xargs git add | ||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ web/node_modules | |
web/dist/files | ||
release | ||
gitness | ||
.idea | ||
coverage.out |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
ifndef GOPATH | ||
GOPATH := $(shell go env GOPATH) | ||
endif | ||
ifndef GOBIN | ||
GOBIN := $(shell go env GOPATH)/bin | ||
endif | ||
ifndef DOCKER_BUILD_OPTS | ||
DOCKER_BUILD_OPTS := | ||
endif | ||
|
||
tools = $(addprefix $(GOBIN)/, golangci-lint goimports govulncheck) | ||
deps = $(addprefix $(GOBIN)/, wire dbmate mockgen) | ||
|
||
ifneq (,$(wildcard ./.local.env)) | ||
include ./.local.env | ||
export | ||
endif | ||
|
||
.DEFAULT_GOAL := all | ||
|
||
init: ## Install git hooks to perform pre-commit checks | ||
git config core.hooksPath .githooks | ||
git config commit.template .gitmessage | ||
|
||
all: dep tools generate lint build test ## Build and run the test for gitness | ||
@echo "Run `make start` to start the services" | ||
|
||
dep: $(deps) ## Install the deps required to generate code and build gitness | ||
@echo "Installing dependencies" | ||
@go mod download | ||
|
||
tools: $(tools) ## Install tools required for the build | ||
@echo "Installed tools" | ||
|
||
mocks: $(mocks) | ||
@echo "Generating Test Mocks" | ||
|
||
generate: $(mocks) wire_gen.go mocks/mock_client.go | ||
@echo "Generating Code" | ||
|
||
build: generate ## Build the gitness service binary | ||
@echo "Building Gitness Server" | ||
CGO_ENABLED=0 go build -ldflags="-X github.com/harness/gitness/version.GitCommit=${GIT_COMMIT} -X github.com/harness/gitness/version.Version=${GITNESS_VERSION}" -o ./gitness . | ||
|
||
test: generate ## Run the go tests | ||
@echo "Running tests" | ||
go test -v -coverprofile=coverage.out ./internal/... | ||
go tool cover -html=coverage.out | ||
|
||
run: dep ## Run the gitness binary from source | ||
@go run -race -ldflags="-X github.com/harness/gitness/version.Version=1.0.0" . | ||
|
||
clean-db: ## delete all data from local database | ||
psql postgresql://gitness:gitness@localhost:5432/gitness -f scripts/db/cleanup.sql | ||
|
||
populate-db: ## inject sample data into local database | ||
psql postgresql://gitness:gitness@localhost:5432/gitness -f scripts/db/sample_data.sql | ||
|
||
update-tools: delete-tools $(tools) ## Update the tools by deleting and re-installing | ||
|
||
delete-tools: ## Delete the tools | ||
@rm $(tools) || true | ||
|
||
|
||
######################################### | ||
# Docker environment commands | ||
# The following targets relate to running gitness and its dependent services | ||
######################################### | ||
start: ## Run all dependent services and start the gitness server locally - the service will listen on :3000 by default | ||
docker-compose -f ./docker/docker-compose.yml up ${DOCKER_BUILD_OPTS} --remove-orphans | ||
|
||
stop: ## Stop all services | ||
docker-compose -f ./docker/docker-compose.yml down --remove-orphans | ||
|
||
dev: ## Run local dev environment this starts the services which gitness depends on | ||
docker-compose -f ./docker/docker-compose.yml up ${DOCKER_BUILD_OPTS} --remove-orphans db redis | ||
|
||
test-env: stop ## Run test environment - this runs all services and the gitness in test mode. | ||
docker-compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.test.yml up -d ${DOCKER_BUILD_OPTS} --remove-orphans | ||
|
||
image: ## Build the gitness docker image | ||
@echo "Building Gitness Image" | ||
@docker build --build-arg GITNESS_VERSION=latest \ | ||
--build-arg GIT_COMMIT=${GIT_COMMIT} \ | ||
--build-arg GITHUB_ACCESS_TOKEN=${GITHUB_ACCESS_TOKEN} \ | ||
-t gitness:latest \ | ||
-f ./docker/Dockerfile . | ||
|
||
e2e: generate test-env ## Run e2e tests | ||
chmod +x wait-for-gitness.sh && ./wait-for-gitness.sh | ||
go test -p 1 -v -coverprofile=e2e_cov.out ./tests/... -env=".env.local" | ||
|
||
|
||
########################################### | ||
# Code Formatting and linting | ||
########################################### | ||
|
||
format: tools # Format go code and error if any changes are made | ||
@echo "Formating ..." | ||
@goimports -w . | ||
@echo "Formatting complete" | ||
|
||
sec: | ||
@echo "Vulnerability detection $(1)" | ||
@govulncheck ./... | ||
|
||
lint: tools generate # lint the golang code | ||
@echo "Linting $(1)" | ||
@golangci-lint run | ||
|
||
########################################### | ||
# Code Generation | ||
# | ||
# Some code generation can be slow, so we only run it if | ||
# the source file has changed. | ||
########################################### | ||
wire_gen.go: cli/server/wire.go ## Update the wire dependency injection if wire.go has changed. | ||
@echo "Updating wire_gen.go" | ||
go generate ./cli/server/wire_gen.go | ||
|
||
mocks/mock_client.go: internal/store/store.go client/client.go | ||
go generate mocks/mock.go | ||
|
||
########################################### | ||
# Install Tools and deps | ||
# | ||
# These targets specify the full path to where the tool is installed | ||
# If the tool already exists it wont be re-installed. | ||
########################################### | ||
|
||
# Install golangci-lint | ||
$(GOBIN)/golangci-lint: | ||
@echo "🔘 Installing golangci-lint... (`date '+%H:%M:%S'`)" | ||
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin | ||
|
||
# Install goimports to format code | ||
$(GOBIN)/goimports: | ||
@echo "🔘 Installing goimports ... (`date '+%H:%M:%S'`)" | ||
@go install golang.org/x/tools/cmd/goimports | ||
|
||
# Install wire to generate dependency injection | ||
$(GOBIN)/wire: | ||
go install github.com/google/wire/cmd/wire@latest | ||
|
||
# Install dbmate to perform db migrations | ||
$(GOBIN)/dbmate: | ||
go install github.com/amacneil/[email protected] | ||
|
||
# Install mockgen to generate mocks | ||
$(GOBIN)/mockgen: | ||
go install github.com/golang/mock/mockgen@latest | ||
|
||
$(GOBIN)/govulncheck: | ||
go install golang.org/x/vuln/cmd/govulncheck@latest | ||
|
||
help: ## show help message | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
.PHONY: delete-tools update-tools help format lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
version: "3.9" | ||
|
||
services: | ||
db: | ||
image: postgres | ||
restart: always | ||
environment: | ||
POSTGRES_DB: gitness | ||
POSTGRES_USER: gitness | ||
POSTGRES_PASSWORD: gitness | ||
PGDATA: /var/lib/postgresql/data/pgdata | ||
ports: | ||
- "5432:5432" | ||
expose: | ||
- 5432 | ||
volumes: | ||
- ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh | ||
- gitness_data:/var/lib/postgresql/data | ||
|
||
migration: | ||
build: | ||
context: ../db | ||
dockerfile: ./Dockerfile | ||
environment: | ||
DATABASE_URL: postgres://gitness:gitness@db:5432/gitness?sslmode=disable | ||
depends_on: | ||
- db | ||
|
||
redis: | ||
image: redis:6.0 | ||
container_name: cache | ||
ports: | ||
- "6379:6379" | ||
expose: | ||
- 6379 | ||
|
||
volumes: | ||
gitness_data: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
CREATE DATABASE gitness_test; | ||
GRANT ALL PRIVILEGES ON DATABASE gitness_test TO "$POSTGRES_USER"; | ||
EOSQL |
Empty file.
Empty file.
Oops, something went wrong.