Skip to content

Commit d1e1aaa

Browse files
authored
Makefile Rework (#253)
1 parent f514f68 commit d1e1aaa

File tree

7 files changed

+117
-29
lines changed

7 files changed

+117
-29
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ jobs:
7272
DOCKER_BUILDKIT: 1
7373

7474
- name: Build Tests
75-
run: make build-test
75+
run: make build-tests
7676

7777
- name: Run tests with Makefile
7878
run: make run-tests
7979
env:
80-
DOCKER_BUILDKIT: 1
80+
DOCKER_BUILDKIT: 1

Dockerfile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ARG CONTEXT=prod
22

3-
FROM golang:1.24.4-alpine as base
3+
FROM golang:1.24.4-alpine AS base
44

55
## Setup
66
ARG CONTEXT
@@ -24,18 +24,21 @@ EXPOSE 9008
2424
## Healthcheck
2525
HEALTHCHECK CMD ["/app/healthcheck"]
2626

27+
## Command
28+
COPY ./dev/command.sh ./
29+
RUN chmod +x command.sh
30+
CMD ["./command.sh"]
31+
2732
# Development Image
2833

29-
FROM base as dev
34+
FROM base AS dev
3035

3136
RUN ["go", "install", "github.com/githubnemo/CompileDaemon@latest"]
3237

33-
## Command
34-
CMD CompileDaemon -log-prefix=false -build="go build ./cmd/server" -command="./server"
3538

3639
# Testing Image
3740

38-
FROM base as tests
41+
FROM base AS tests
3942

4043
COPY dev/container-tests.sh ./dev/container-tests.sh
4144

@@ -48,17 +51,16 @@ RUN apk add --no-cache \
4851

4952
## Command
5053
STOPSIGNAL SIGKILL
51-
CMD ["sleep", "inf"]
5254

5355
# Production Image
5456

55-
FROM base as builder
57+
FROM base AS builder
5658

5759
RUN CGO_ENABLED=0 go build ./cmd/openslides && \
5860
CGO_ENABLED=0 go build ./cmd/server && \
5961
CGO_ENABLED=0 go build ./cmd/healthcheck
6062

61-
FROM scratch as client
63+
FROM scratch AS client
6264

6365
WORKDIR /
6466
ENV APP_CONTEXT=prod
@@ -67,7 +69,7 @@ COPY --from=builder /app/openslides .
6769

6870
ENTRYPOINT ["/openslides"]
6971

70-
FROM scratch as prod
72+
FROM scratch AS prod
7173

7274
## Setup
7375
ARG CONTEXT

Makefile

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,59 @@
1-
SERVICE=manage
1+
override SERVICE=manage
2+
3+
# Build images for different contexts
24

35
build-prod:
4-
docker build ./ --tag "openslides-$(SERVICE)" --build-arg CONTEXT="prod" --target "prod"
6+
docker build ./ $(ARGS) --tag "openslides-$(SERVICE)" --build-arg CONTEXT="prod" --target "prod"
57

68
build-dev:
7-
docker build ./ --tag "openslides-$(SERVICE)-dev" --build-arg CONTEXT="dev" --target "dev"
9+
docker build ./ $(ARGS) --tag "openslides-$(SERVICE)-dev" --build-arg CONTEXT="dev" --target "dev"
810

9-
build-test:
10-
docker build ./ --tag "openslides-$(SERVICE)-tests" --build-arg CONTEXT="tests" --target "tests"
11+
build-tests:
12+
docker build ./ $(ARGS) --tag "openslides-$(SERVICE)-tests" --build-arg CONTEXT="tests" --target "tests"
1113

12-
all: openslides
14+
# Tests
1315

1416
run-tests:
1517
bash dev/run-tests.sh
1618

17-
test:
19+
lint:
20+
bash dev/run-lint.sh -l
21+
22+
gofmt:
23+
gofmt -l -s -w .
24+
25+
########################## Deprecation List ##########################
26+
27+
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
28+
29+
deprecation-warning:
30+
@echo "\033[1;33m DEPRECATION WARNING: This make command is deprecated and will be removed soon! \033[0m"
31+
32+
deprecation-warning-alternative: | deprecation-warning
33+
@echo "\033[1;33m Please use the following command instead: $(ALTERNATIVE) \033[0m"
34+
35+
run-dev run-dev-attach run-dev-attached run-dev-standalone run-bash run-dev-interactive stop-dev:
36+
@make deprecation-warning-alternative ALTERNATIVE="dev and derivative maketargets are now only available in main repository. (use 'make dev-help' in main repository for more information)"
37+
38+
all: | deprecation-warning openslides
39+
40+
test: | deprecation-warning
1841
# Attention: This steps should be the same as in .github/workflows/test.yml.
1942
test -z "$(shell gofmt -l .)"
2043
go vet ./...
2144
go install golang.org/x/lint/golint@latest
2245
golint -set_exit_status ./...
2346
go test -timeout 10s -race ./...
2447

25-
go-build:
48+
go-build: | deprecation-warning
2649
go build ./cmd/openslides
2750

28-
protoc:
51+
protoc: | deprecation-warning
2952
protoc --go_out=. --go_opt=paths=source_relative \
3053
--go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative \
3154
proto/manage.proto
3255

33-
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
34-
35-
openslides:
56+
openslides: | deprecation-warning
3657
docker build . --target builder --tag openslides-manage-builder
3758
docker run --interactive --tty --volume $(dir $(mkfile_path)):/build/ --rm openslides-manage-builder sh -c " \
3859
if [ $(shell whoami) != root ]; then \

dev/command.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
# Timeout argument to be used in GitHub Workflows
3+
4+
if [ "$APP_CONTEXT" = "dev" ]; then exec CompileDaemon -log-prefix=false -build="go build ./cmd/server" -command="./server"; fi
5+
if [ "$APP_CONTEXT" = "tests" ]; then sleep inf; fi

dev/container-tests.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ trap 'kill $DOCKERD_PID' EXIT INT TERM ERR
88

99
RETRY=0
1010
MAX=10
11-
until docker info >/dev/null 2>&1
12-
do
11+
until docker info >/dev/null 2>&1; do
1312
if [ "$RETRY" -ge "$MAX" ]
1413
then
1514
echo "Dockerd setup error"
@@ -30,4 +29,4 @@ go test -timeout 60s -race ./... || CATCH=1
3029
gofmt -l . || CATCH=1
3130
golint -set_exit_status ./... || CATCH=1
3231

33-
exit $CATCH
32+
exit $CATCH

dev/run-lint.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
# Executes all linters. Should errors occur, CATCH will be set to 1, causing an erroneous exit code.
4+
5+
echo "########################################################################"
6+
echo "###################### Run Linters #####################################"
7+
echo "########################################################################"
8+
9+
# Parameters
10+
while getopts "ls" FLAG; do
11+
case "${FLAG}" in
12+
l) LOCAL=true ;;
13+
s) SKIP_SETUP=true ;;
14+
*) echo "Can't parse flag ${FLAG}" && break ;;
15+
esac
16+
done
17+
18+
# Setup
19+
CONTAINER_NAME="manage-tests"
20+
IMAGE_TAG=openslides-manage-tests
21+
DOCKER_EXEC="docker exec ${CONTAINER_NAME}"
22+
23+
# Safe Exit
24+
trap 'if [ -z "$LOCAL" ] && [ -z "$SKIP_SETUP" ]; then docker stop $CONTAINER_NAME &> /dev/null && docker rm $CONTAINER_NAME &> /dev/null; fi' EXIT
25+
26+
# Execution
27+
if [ -z "$LOCAL" ]
28+
then
29+
# Setup
30+
if [ -z "$SKIP_SETUP" ]
31+
then
32+
make build-tests >/dev/null 2>&1
33+
docker run -d --name "${CONTAINER_NAME}" "${IMAGE_TAG}"
34+
fi
35+
36+
# Container Mode
37+
eval "$DOCKER_EXEC go vet ./..."
38+
eval "$DOCKER_EXEC golint -set_exit_status ./..."
39+
eval "$DOCKER_EXEC gofmt -l ."
40+
else
41+
# Local Mode
42+
go vet ./...
43+
golint -set_exit_status ./...
44+
gofmt -l -s -w .
45+
fi

dev/run-tests.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
#!/bin/bash
22

3+
set -e
4+
35
# Executes all tests. Should errors occur, CATCH will be set to 1, causing an erroneous exit code.
46

57
echo "########################################################################"
68
echo "###################### Run Tests and Linters ###########################"
79
echo "########################################################################"
810

11+
# Parameters
12+
while getopts "s" FLAG; do
13+
case "${FLAG}" in
14+
s) SKIP_BUILD=true ;;
15+
*) echo "Can't parse flag ${FLAG}" && break ;;
16+
esac
17+
done
18+
919
# Setup
20+
CONTAINER_NAME="manage-tests"
1021
IMAGE_TAG=openslides-manage-tests
22+
LOCAL_PWD=$(dirname "$0")
1123

1224
# Safe Exit
13-
trap 'docker stop $(docker ps -a -q --filter ancestor=${IMAGE_TAG})' EXIT
25+
trap 'docker stop "$CONTAINER_NAME" &> /dev/null && docker rm "$CONTAINER_NAME" &> /dev/null' EXIT
1426

1527
# Execution
16-
make build-test
17-
docker run --privileged -t ${IMAGE_TAG} ./dev/container-tests.sh
28+
if [ -z "$SKIP_BUILD" ]; then make build-tests &> /dev/null; fi
29+
docker run -d --privileged --name "$CONTAINER_NAME" ${IMAGE_TAG}
30+
docker exec "$CONTAINER_NAME" ./dev/container-tests.sh
31+
32+
# Linters
33+
bash "$LOCAL_PWD"/run-lint.sh -s

0 commit comments

Comments
 (0)