Skip to content

Commit c9a0456

Browse files
committed
updated build scripts
1 parent 291d423 commit c9a0456

File tree

7 files changed

+141
-51
lines changed

7 files changed

+141
-51
lines changed

.cspell.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
"language": "en",
88
// words - list of words to be always considered correct
99
"words": [
10+
"Buildx",
1011
"censys",
1112
"certgraph",
12-
"crtsh"
13+
"crtsh",
14+
"elif",
15+
"GOARCH",
16+
"golangci",
17+
"GOPROXY",
18+
"goreleaser",
19+
"ldflags",
20+
"serv",
21+
"trimpath"
1322
],
1423
// flagWords - list of words to be always considered incorrect
1524
// This is useful for offensive words and common spelling errors.

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Dockerfile
22
certgraph
3-
build/
3+
dist/
44
*.json

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
certgraph
2-
build/
2+
dist/

.goreleaser.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# .goreleaser.yml
2+
version: 2
3+
project_name: certgraph
4+
5+
before:
6+
hooks:
7+
- go mod tidy
8+
9+
builds:
10+
- id: certgraph
11+
main: .
12+
binary: certgraph
13+
goos:
14+
- linux
15+
- windows
16+
- darwin
17+
goarch:
18+
- amd64
19+
- arm64
20+
ignore:
21+
- goos: windows
22+
goarch: arm64
23+
flags:
24+
- -trimpath
25+
ldflags:
26+
- -s -w -X main.version={{.Version}}
27+
28+
archives:
29+
- # This single archive definition handles all OSes.
30+
# The default format is 'tar.gz', which is used for linux and darwin.
31+
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
32+
format_overrides:
33+
- goos: windows
34+
formats: ['zip'] # Override the format to 'zip' for Windows builds.
35+
36+
checksum:
37+
name_template: 'checksums.txt'
38+
39+
changelog:
40+
sort: asc
41+
filters:
42+
exclude:
43+
- '^docs:'
44+
- '^test:'

Dockerfile

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
FROM golang:alpine
1+
# build stage
2+
FROM golang:alpine AS build-env
3+
RUN apk update && apk add --no-cache make git
24

3-
RUN apk add --update git make
5+
# Accept VERSION as a build argument
6+
ARG VERSION
7+
ENV VERSION=${VERSION}
48

5-
WORKDIR /src/certgraph
6-
ADD . .
9+
WORKDIR /go/app/
10+
COPY go.mod go.sum ./
11+
RUN go mod download
712

8-
ENV CGO_ENABLED=0
9-
RUN make install
13+
COPY . .
14+
RUN make
15+
16+
# final stage
17+
FROM alpine
18+
19+
COPY --from=build-env /go/app/certgraph /bin/
20+
21+
USER 1000
1022

1123
ENTRYPOINT [ "certgraph" ]

Makefile

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,44 @@
1-
GIT_DATE := $(shell git log -1 --date=short --pretty='%cd' | tr -d -)
2-
GIT_HASH := $(shell git rev-parse HEAD)
1+
default: certgraph
32

4-
BUILD_FLAGS := -trimpath -ldflags "-w -s -X main.gitDate=$(GIT_DATE) -X main.gitHash=$(GIT_HASH)"
3+
RELEASE_DEPS = fmt lint
4+
include release.mk
5+
6+
BUILD_FLAGS := -trimpath -ldflags "-w -s -X main.version=${VERSION}"
57

6-
PLATFORMS := linux/amd64 linux/386 linux/arm linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/386 openbsd/amd64
78
SOURCES := $(shell find . -maxdepth 1 -type f -name "*.go")
89
ALL_SOURCES = $(shell find . -type f -name '*.go') go.mod docs/*
910

10-
temp = $(subst /, ,$@)
11-
os = $(word 1, $(temp))
12-
arch = $(word 2, $(temp))
13-
ext = $(shell if [ "$(os)" = "windows" ]; then echo ".exe"; fi)
14-
15-
.PHONY: all release fmt clean serv $(PLATFORMS) docker check deps update-deps
16-
17-
all: certgraph
18-
19-
release: $(PLATFORMS)
20-
rm -r build/bin/
11+
.PHONY: release fmt clean serv docker lint deps update-deps
2112

2213
certgraph: $(SOURCES) $(ALL_SOURCES)
23-
go build $(BUILD_FLAGS) -o $@ $(SOURCES)
24-
25-
$(PLATFORMS): $(SOURCES)
26-
CGO_ENABLED=0 GOOS=$(os) GOARCH=$(arch) go build $(BUILD_FLAGS) -o 'build/bin/$(os)/$(arch)/certgraph$(ext)' $(SOURCES)
27-
mkdir -p build/$(GIT_DATE)/; cd build/bin/$(os)/$(arch)/; zip -r ../../../$(GIT_DATE)/certgraph-$(os)-$(arch)-$(GIT_DATE).zip .; cd ../../../
14+
go build $(BUILD_FLAGS) -o $@ .
2815

2916
docker: Dockerfile $(ALL_SOURCES)
30-
docker build -t lanrat/certgraph .
17+
docker build --build-arg VERSION=${VERSION} -t lanrat/certgraph .
3118

3219
deps: go.mod
3320
GOPROXY=direct go mod download
3421
GOPROXY=direct go get -u all
3522

36-
fmt:
37-
gofmt -s -w -l .
23+
update-deps:
24+
go get -u
25+
go mod tidy
3826

39-
install: $(SOURCES) $(ALL_SOURCES)
40-
go install $(BUILD_FLAGS)
27+
fmt:
28+
go fmt ./...
4129

4230
clean:
43-
rm -rf certgraph build/
44-
45-
check: | lint check1 check2 vulncheck
46-
47-
check1:
48-
golangci-lint run
49-
50-
check2:
51-
staticcheck -f stylish -checks all ./...
52-
53-
vulncheck:
54-
govulncheck ./...
31+
rm -rf certgraph dist/
5532

5633
lint:
57-
golint ./...
34+
golangci-lint run
5835

5936
serv: certgraph
6037
./certgraph --serve 127.0.0.1:8080
6138

62-
update-deps:
63-
go get -u
64-
go mod tidy
65-
6639
test:
6740
go test -v ./... | grep -v "\[no test files\]"
41+
42+
.PHONY: goreleaser
43+
goreleaser:
44+
goreleaser release --snapshot --clean

release.mk

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Get the current version from git.
2+
# `git describe` will use the most recent tag.
3+
# `--tags` ensures any tag is considered, not just annotated ones.
4+
# `--always` ensures that if no tags are present, the commit hash is used.
5+
# `--dirty` will append "-dirty" if the working directory has uncommitted changes.
6+
VERSION ?= $(shell git describe --tags --always --dirty)
7+
8+
# Release target to create a new semantic version tag
9+
.PHONY: release
10+
release: $(RELEASE_DEPS)
11+
# 1. Check for a clean working directory
12+
@if ! git diff --quiet; then \
13+
echo "Error: Working directory is not clean. Commit or stash changes before releasing."; \
14+
exit 1; \
15+
fi
16+
17+
# This shell 'if' statement runs at execution time, not parse time.
18+
@if [ -z "$(BUMP)" ]; then \
19+
echo "Error: BUMP is not set. Usage: make release BUMP=patch|minor|major"; \
20+
exit 1; \
21+
fi
22+
23+
# 2. Get the latest git tag, or start at v0.0.0
24+
@CURRENT_TAG=$$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0"); \
25+
CURRENT_VERSION=$$(echo $$CURRENT_TAG | sed 's/^v//'); \
26+
MAJOR=$$(echo $$CURRENT_VERSION | cut -d. -f1); \
27+
MINOR=$$(echo $$CURRENT_VERSION | cut -d. -f2); \
28+
PATCH=$$(echo $$CURRENT_VERSION | cut -d. -f3); \
29+
\
30+
if [ "$(BUMP)" = "patch" ]; then \
31+
PATCH=$$((PATCH + 1)); \
32+
elif [ "$(BUMP)" = "minor" ]; then \
33+
MINOR=$$((MINOR + 1)); \
34+
PATCH=0; \
35+
elif [ "$(BUMP)" = "major" ]; then \
36+
MAJOR=$$((MAJOR + 1)); \
37+
MINOR=0; \
38+
PATCH=0; \
39+
else \
40+
echo "Error: Invalid BUMP value. Use 'patch', 'minor', or 'major'."; \
41+
exit 1; \
42+
fi; \
43+
\
44+
NEW_TAG="v$${MAJOR}.$${MINOR}.$${PATCH}"; \
45+
echo "Current version: $$CURRENT_TAG"; \
46+
echo "Creating new version: $$NEW_TAG"; \
47+
git tag $$NEW_TAG; \
48+
git push origin $$NEW_TAG;

0 commit comments

Comments
 (0)