Skip to content

Commit 473c7f7

Browse files
committed
Merge pull request #1 from goproslowyo/initial
Add a Makefile and GitHub action to run unit tests and coverage report
2 parents 8b46ab7 + 791d5c0 commit 473c7f7

File tree

7 files changed

+135
-4
lines changed

7 files changed

+135
-4
lines changed

.github/workflows/main.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This workflow will sort in{dex,active}.md and commit the changes back to the repository.
2+
name: Build secinfo Binary
3+
4+
# Controls when the workflow will run
5+
on:
6+
# Triggers the workflow on push or pull request events but only for the main branch
7+
push:
8+
branches: [main]
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v3
17+
name: Checkout repository
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Fetch all tags
22+
run: git fetch --force --tags
23+
24+
- name: Set up Go
25+
uses: actions/setup-go@v3
26+
with:
27+
go-version: 1.18
28+
29+
- name: Login to registry
30+
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
31+
32+
- name: Run GoReleaser
33+
uses: goreleaser/goreleaser-action@v2
34+
with:
35+
distribution: goreleaser
36+
version: latest
37+
args: release --rm-dist
38+
env:
39+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/tests.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ on:
55
branches: ["main"]
66
pull_request:
77
branches: ["main"]
8+
# Allows you to run this workflow manually from the Actions tab
9+
workflow_dispatch:
810

911
jobs:
10-
build:
12+
test:
1113
runs-on: ubuntu-latest
1214
steps:
1315
- uses: actions/checkout@v3
16+
name: Checkout repository
1417
with:
1518
# default fetch-depth is insufficent to find previous coverage notes
1619
fetch-depth: 10
@@ -20,11 +23,18 @@ jobs:
2023
with:
2124
go-version: 1.18
2225

23-
- name: Test
26+
- name: Run tests and coverage report
2427
run: make test
2528

2629
- uses: gwatts/go-coverage-action@v1
2730
id: coverage
31+
name: Post coverage report to pull request
2832
with:
2933
test-args: '["-v"]'
3034
coverage-threshold: 85
35+
36+
- uses: actions/upload-artifact@v3
37+
name: Upload coverage artifact
38+
with:
39+
name: coverage-report
40+
path: coverage.html

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Binaries for programs and plugins
22
secinfo
33

4+
# Gorelease binaries
5+
dist/
6+
47
# Test binary, built with `go test -c`
58
*.test
69

.goreleaser.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
builds:
2+
- ldflags:
3+
- -s -w -extldflags "-static" -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
4+
tags:
5+
- osusergo
6+
- netgo
7+
- static
8+
env:
9+
- CGO_ENABLED=0
10+
# targets:
11+
# - linux_amd64_v4
12+
goos:
13+
- linux
14+
goarch:
15+
- amd64
16+
goamd64:
17+
- v4
18+
# hooks:
19+
# post: upx --ultra-brute "{{ .Path }}" && upx -vt "{{ .Path }}"
20+
21+
dockers:
22+
- goos: linux # GOOS of the built binaries/packages that should be used.
23+
# GOARCH of the built binaries/packages that should be used.
24+
goarch: amd64
25+
# GOAMD64 of the built binaries/packages that should be used.
26+
goamd64: "v4"
27+
# Templates of the Docker image names.
28+
extra_files:
29+
- go.mod
30+
- go.sum
31+
- secinfo.go
32+
- streamers
33+
image_templates:
34+
- "ghcr.io/goproslowyo/secinfo:{{ .Tag }}"
35+
- "ghcr.io/goproslowyo/secinfo:latest"
36+
# skip_push: "true"

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:latest as builder
2+
3+
COPY go.mod go.sum /build/
4+
COPY secinfo.go /build/
5+
COPY streamers /build/streamers
6+
7+
WORKDIR /build
8+
9+
RUN apt update && apt install -y upx
10+
RUN CGO_ENABLED=0 go build -v -ldflags '-s -w -extldflags "-static"' -tags 'osusergo,netgo,static' -asmflags 'all=-trimpath={{.Env.GOPATH}}' .
11+
RUN upx --ultra-brute secinfo && upx -t secinfo
12+
13+
FROM scratch
14+
15+
WORKDIR /app
16+
17+
COPY --from=builder /build/secinfo .
18+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
19+
20+
CMD ["/app/secinfo"]

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ cover: test
1212
clean:
1313
rm -rf coverage.out coverage.html
1414
rm -rf secinfo secinfo.test
15+
rm -rf dist
1516

1617
build:
17-
rm secinfo
18-
CGO_ENABLED=0 go build -v -tags 'osusergo,netgo,static' -ldflags '-s -w -extldflags "-static"' .
18+
@echo ${VERSION}
19+
CGO_ENABLED=0 GOAMD64=v4 go build -v -trimpath -tags 'osusergo,netgo,static' -ldflags '-s -w -extldflags "-static"' .
1920

2021
docker-build:
2122
docker build -t secinfo:${VERSION} .
2223

24+
release:
25+
goreleaser release --snapshot --rm-dist
26+
2327
docker-run:
2428
docker run -it -v ${PWD}/templates:/app/templates -v ${PWD}/index.md:/app/index.md -v ${PWD}/streamers.csv:/app/streamers.csv secinfo:${VERSION}
2529

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ Check the `Makefile` for all options but you've got `make {docs,test,cover,clean
1212

1313
When running `docker-run`, we assume you're providing `streamers.csv` from the infosecstreams repo. You can check the invocation of the `docker run ...` command in the Makefile for further details.
1414

15+
### Test Mode
16+
17+
When developing, you'll likely not want to actually hit the API and use the internet. Instead you can reference static json files.
18+
19+
If the `SECINFO_TEST` environment variable is set (to literally any non-empty string), we'll use the test files instead of hitting the API.
20+
21+
```go
22+
// Check environ SECINFO_TEST exists
23+
if os.Getenv("SECINFO_TEST") == "" { ... }
24+
else {
25+
// Read active.json into active struct
26+
f, _ := ioutil.ReadFile("active.json")
27+
_ = json.Unmarshal([]byte(f), &active)
28+
// Read inactive.json into inactive struct
29+
f, _ = ioutil.ReadFile("inactive.json")
30+
_ = json.Unmarshal([]byte(f), &inactive)
31+
}
32+
```
33+
1534
## Usage
1635

1736
Ensure there's a `streamers.csv` in the CWD of the secinfo binary.

0 commit comments

Comments
 (0)