Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ci test workflow #6

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .ecrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Exclude": [
".git",
"go.mod",
"go.sum",
"vendor",
"fixtures",
"LICENSE",
"drivers/hetznercloud/driver.go",
"_test.go",
"Makefile"
]
}
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.go]
indent_style = tab

[*.md]
trim_trailing_whitespace = false
indent_size = 1

[Makefile]
indent_style = tab
39 changes: 37 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
.env
/dist
### IDEs ###
.idea/
.vscode/*
!.vscode/settings.json
!.vscode/launch.json
!.vscode/extensions.json

### GO ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
vendor/
__debug_bin

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

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

### Docker ###
docker-compose.yml

### Other ##
# runetime or build relicts
*.sqlite
*.out
/.env
/.pnpm-store
extras/
/build/
/dist/

docs/venv
40 changes: 40 additions & 0 deletions .woodpecker/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
when:
- event: pull_request
- event: push
branch: [main]

variables:
- &golang_image "golang:1.20.2"
- &when
- path: &when_path # related config files
- ".woodpecker/test.yml"
- ".golangci.yml"
# go source code
- "**/*.go"
- "go.*"
# schema changes
- "pipeline/schema/**"
branch:
exclude: ${CI_REPO_DEFAULT_BRANCH}
event: push
- path: *when_path
event: [pull_request, tag, deployment]

steps:
lint:
image: *golang_image
group: test
commands:
- make lint
when: *when

lint-editorconfig:
image: mstruebing/editorconfig-checker
group: test

test:
image: *golang_image
group: test
commands:
- make test
when: *when
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COPY . .
ARG TARGETOS TARGETARCH
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-s -w -extldflags "-static"' -o dist/woodpecker-autoscaler ./cmd/woodpecker-autoscaler
make build

FROM --platform=$BUILDPLATFORM scratch
ENV GODEBUG=netdns=go
Expand Down
21 changes: 21 additions & 0 deletions Containerfile.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# docker build --rm -f docker/Dockerfile.make -t woodpecker/make:local .
FROM golang:1.20-alpine as golang_image
FROM node:18-alpine

RUN apk add --no-cache --update make gcc binutils-gold musl-dev && \
corepack enable

# Build packages.
COPY --from=golang_image /usr/local/go /usr/local/go
COPY Makefile /
ENV PATH=$PATH:/usr/local/go/bin

# Cache tools
RUN make install-tools && \
mv /root/go/bin/* /usr/local/go/bin/ && \
chmod 755 /usr/local/go/bin/*

WORKDIR /build
RUN chmod -R 777 /root

CMD [ "/bin/sh" ]
97 changes: 97 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
GO_PACKAGES ?= $(shell go list ./... | grep -v /vendor/)

TARGETOS ?= linux
TARGETARCH ?= amd64

LDFLAGS := -s -w -extldflags "-static"
CGO_ENABLED := 0

HAS_GO = $(shell hash go > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
ifeq ($(HAS_GO),GO)
CGO_CFLAGS ?= $(shell go env CGO_CFLAGS)
endif
CGO_CFLAGS ?=

# If the first argument is "in_docker"...
ifeq (in_docker,$(firstword $(MAKECMDGOALS)))
# use the rest as arguments for "in_docker"
MAKE_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# Ignore the next args
$(eval $(MAKE_ARGS):;@:)

in_docker:
@[ "1" -eq "$(shell docker image ls woodpecker/make:local -a | wc -l)" ] && docker buildx build -f ./docker/Dockerfile.make -t woodpecker/make:local --load . || echo reuse existing docker image
@echo run in docker:
@docker run -it \
--user $(shell id -u):$(shell id -g) \
-e CI_COMMIT_SHA="$(CI_COMMIT_SHA)" \
-e TARGETOS="$(TARGETOS)" \
-e TARGETARCH="$(TARGETARCH)" \
-e CGO_ENABLED="$(CGO_ENABLED)" \
-e GOPATH=/tmp/go \
-e HOME=/tmp/home \
-v $(PWD):/build --rm woodpecker/make:local make $(MAKE_ARGS)
else

# Proceed with normal make

##@ General

all: help

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

format: install-tools ## Format source code
@gofumpt -extra -w .

.PHONY: clean
clean: ## Clean build artifacts
go clean -i ./...
rm -rf build
@[ "1" != "$(shell docker image ls woodpecker/make:local -a | wc -l)" ] && docker image rm woodpecker/make:local || echo no docker image to clean


install-tools: ## Install development tools
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
fi ; \
hash lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install github.com/rs/zerolog/cmd/lint@latest; \
fi ; \
hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go install mvdan.cc/gofumpt@latest; \
fi ; \

##@ Test

.PHONY: lint
lint: install-tools ## Lint code
@echo "Running golangci-lint"
golangci-lint run --timeout 10m
@echo "Running zerolog linter"
lint github.com/woodpecker-ci/autoscaler/cmd/woodpecker-autoscaler

test-autoscaler: ## Test autoscaler code
go test -race -cover -coverprofile autoscaler-coverage.out -timeout 30s ${GO_PACKAGES}

test: test-autoscaler ## Run all tests

##@ Build

build:
CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '${LDFLAGS}' -o dist/woodpecker-autoscaler github.com/woodpecker-ci/autoscaler/cmd/woodpecker-autoscaler

endif