From b0bff53bbd60ec77d9c7a347d38df21c53f6e639 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 26 Jun 2022 15:26:20 -0700 Subject: [PATCH] start refactoring the Makefile to build artifacts in parallel (eventually using Zig for cross compilation). --- .github/workflows/build.yaml | 85 ---------------------- .github/workflows/ci.yaml | 78 ++++++++++++++++++++ Makefile | 133 +++++++++++++++++++++++------------ README.md | 2 +- go.mod | 2 +- 5 files changed, 167 insertions(+), 133 deletions(-) delete mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml deleted file mode 100644 index b41c0c7..0000000 --- a/.github/workflows/build.yaml +++ /dev/null @@ -1,85 +0,0 @@ -name: CI -# This workflow is triggered on pushes & pull requests -on: [pull_request] - -jobs: - build: - name: Build - runs-on: ubuntu-latest - container: techknowlogick/xgo:go-1.17.x - - # Service containers to run with `build` (Required for end-to-end testing) - services: - influxdb: - image: influxdb:2.2 - env: - DOCKER_INFLUXDB_INIT_MODE: setup - DOCKER_INFLUXDB_INIT_USERNAME: admin - DOCKER_INFLUXDB_INIT_PASSWORD: password12345 - DOCKER_INFLUXDB_INIT_ORG: scrutiny - DOCKER_INFLUXDB_INIT_BUCKET: metrics - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: my-super-secret-auth-token - ports: - - 8086:8086 - env: - PROJECT_PATH: /go/src/github.com/analogj/scrutiny - CGO_ENABLED: 1 - steps: - - name: Git - run: | - apt-get update && apt-get install -y software-properties-common - add-apt-repository ppa:git-core/ppa && apt-get update && apt-get install -y git - git --version - - name: Checkout - uses: actions/checkout@v2 - - name: Test - run: | - mkdir -p $(dirname "$PROJECT_PATH") - cp -a $GITHUB_WORKSPACE $PROJECT_PATH - cd $PROJECT_PATH - - go mod vendor - go test -race -coverprofile=coverage.txt -covermode=atomic -v -tags "static" $(go list ./... | grep -v /vendor/) - - name: Generate coverage report - uses: codecov/codecov-action@v2 - with: - files: ${{ env.PROJECT_PATH }}/coverage.txt - flags: unittests - fail_ci_if_error: true - verbose: true - - name: Build Binaries - run: | - - cd $PROJECT_PATH - make all - - - name: Archive - uses: actions/upload-artifact@v2 - with: - name: binaries.zip - path: | - /build/scrutiny-web-linux-amd64 - /build/scrutiny-collector-metrics-linux-amd64 - /build/scrutiny-web-linux-arm64 - /build/scrutiny-collector-metrics-linux-arm64 - /build/scrutiny-web-linux-arm-5 - /build/scrutiny-collector-metrics-linux-arm-5 - /build/scrutiny-web-linux-arm-6 - /build/scrutiny-collector-metrics-linux-arm-6 - /build/scrutiny-web-linux-arm-7 - /build/scrutiny-collector-metrics-linux-arm-7 - /build/scrutiny-web-windows-4.0-amd64.exe - /build/scrutiny-collector-metrics-windows-4.0-amd64.exe -# /build/scrutiny-web-darwin-arm64 -# /build/scrutiny-collector-metrics-darwin-arm64 -# /build/scrutiny-web-darwin-amd64 -# /build/scrutiny-collector-metrics-darwin-amd64 -# /build/scrutiny-web-freebsd-amd64 -# /build/scrutiny-collector-metrics-freebsd-amd64 - - uses: codecov/codecov-action@v2 - with: - file: ${{ env.PROJECT_PATH }}/coverage.txt - flags: unittests - fail_ci_if_error: false - - diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..e924dd5 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,78 @@ +name: CI +# This workflow is triggered on pushes & pull requests +on: [pull_request] + +jobs: + test: + name: Test + runs-on: ubuntu-latest + container: ghcr.io/packagrio/packagr:latest-golang + + # Service containers to run with `build` (Required for end-to-end testing) + services: + influxdb: + image: influxdb:2.2 + env: + DOCKER_INFLUXDB_INIT_MODE: setup + DOCKER_INFLUXDB_INIT_USERNAME: admin + DOCKER_INFLUXDB_INIT_PASSWORD: password12345 + DOCKER_INFLUXDB_INIT_ORG: scrutiny + DOCKER_INFLUXDB_INIT_BUCKET: metrics + DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: my-super-secret-auth-token + ports: + - 8086:8086 + env: + PROJECT_PATH: /go/src/github.com/analogj/scrutiny + CGO_ENABLED: 1 + steps: + - name: Git + run: | + apt-get update && apt-get install -y software-properties-common + add-apt-repository ppa:git-core/ppa && apt-get update && apt-get install -y git + git --version + - name: Checkout + uses: actions/checkout@v2 + - name: Test + run: | + make clean binary-test-coverage + - name: Generate coverage report + uses: codecov/codecov-action@v2 + with: + files: ${{ github.workspace }}/coverage.txt + flags: unittests + fail_ci_if_error: true + verbose: true + build: + name: Build ${{ matrix.cfg.goos }}/${{ matrix.cfg.goarch }} + needs: release + runs-on: ${{ matrix.cfg.on }} + env: + GOOS: ${{ matrix.cfg.goos }} + GOARCH: ${{ matrix.cfg.goarch }} + GOARM: ${{ matrix.cfg.goarm }} + strategy: + matrix: + cfg: + - { on: ubuntu-latest, goos: linux, goarch: amd64 } + - { on: ubuntu-latest, goos: linux, goarch: arm, goarm: 5 } + - { on: ubuntu-latest, goos: linux, goarch: arm, goarm: 6 } + - { on: ubuntu-latest, goos: linux, goarch: arm, goarm: 7 } + - { on: ubuntu-latest, goos: linux, goarch: arm64 } + - { on: macos-latest, goos: darwin, goarch: amd64 } + - { on: macos-latest, goos: darwin, goarch: arm64 } + - { on: macos-latest, goos: freebsd, goarch: amd64 } + - { on: macos-latest, goos: freebsd, goarch: arm64 } + - { on: windows-latest, goos: windows, goarch: amd64 } + - { on: windows-latest, goos: windows, goarch: arm64 } + steps: + - name: Build Binaries + run: | + make clean binary-all + + - name: Archive + uses: actions/upload-artifact@v2 + with: + name: binaries.zip + path: | + scrutiny-web-* + scrutiny-collector-metrics-* \ No newline at end of file diff --git a/Makefile b/Makefile index b5c4fc1..e7f80c4 100644 --- a/Makefile +++ b/Makefile @@ -1,66 +1,107 @@ -export CGO_ENABLED = 1 +.ONESHELL: # Applies to every targets in the file! .ONESHELL instructs make to invoke a single instance of the shell and provide it with the entire recipe, regardless of how many lines it contains. +######################################################################################################################## +# Global Env Settings +######################################################################################################################## +export CGO_ENABLED = 1 GO_WORKSPACE ?= /go/src/github.com/analogj/scrutiny -BINARY=\ - linux/amd64 \ - linux/arm-5 \ - linux/arm-6 \ - linux/arm-7 \ - linux/arm64 \ +COLLECTOR_BINARY_NAME = scrutiny-collector-metrics +WEB_BINARY_NAME = scrutiny-web +LD_FLAGS = +ifdef STATIC +LD_FLAGS := $(LD_FLAGS) -extldflags=-static +endif +ifdef GOOS +COLLECTOR_BINARY_NAME := $(COLLECTOR_BINARY_NAME)-$(GOOS) +WEB_BINARY_NAME := $(WEB_BINARY_NAME)-$(GOOS) +LD_FLAGS := $(LD_FLAGS) -X main.goos=$(GOOS) +endif +ifdef GOARCH +COLLECTOR_BINARY_NAME := $(COLLECTOR_BINARY_NAME)-$(GOARCH) +WEB_BINARY_NAME := $(WEB_BINARY_NAME)-$(GOARCH) +LD_FLAGS := $(LD_FLAGS) -X main.goarch=$(GOARCH) +endif +ifdef GOARM +COLLECTOR_BINARY_NAME := $(COLLECTOR_BINARY_NAME)-$(GOARM) +WEB_BINARY_NAME := $(WEB_BINARY_NAME)-$(GOARM) +endif +ifeq ($(OS),Windows_NT) +COLLECTOR_BINARY_NAME := $(COLLECTOR_BINARY_NAME).exe +WEB_BINARY_NAME := $(WEB_BINARY_NAME).exe +endif -.ONESHELL: # Applies to every targets in the file! .ONESHELL instructs make to invoke a single instance of the shell and provide it with the entire recipe, regardless of how many lines it contains. -.PHONY: all $(BINARY) -all: $(BINARY) windows/amd64 +######################################################################################################################## +# Binary +######################################################################################################################## +.PHONY: all +all: binary-all + +.PHONY: binary-all +binary-all: binary-web binary-collector binary-frontend -$(BINARY): OS = $(word 1,$(subst /, ,$*)) -$(BINARY): ARCH = $(word 2,$(subst /, ,$*)) -$(BINARY): build/scrutiny-web-%: - @echo "building web binary (OS = $(OS), ARCH = $(ARCH))" - xgo -v --targets="$(OS)/$(ARCH)" -ldflags "-extldflags=-static -X main.goos=$(OS) -X main.goarch=$(ARCH)" -out scrutiny-web -tags "static netgo sqlite_omit_load_extension" ${GO_WORKSPACE}/webapp/backend/cmd/scrutiny/ +.PHONY: binary-clean +binary-clean: + go clean - chmod +x "/build/scrutiny-web-$(OS)-$(ARCH)" - file "/build/scrutiny-web-$(OS)-$(ARCH)" || true - ldd "/build/scrutiny-web-$(OS)-$(ARCH)" || true +.PHONY: binary-dep +binary-dep: + go mod vendor - @echo "building collector binary (OS = $(OS), ARCH = $(ARCH))" - xgo -v --targets="$(OS)/$(ARCH)" -ldflags "-extldflags=-static -X main.goos=$(OS) -X main.goarch=$(ARCH)" -out scrutiny-collector-metrics -tags "static netgo" ${GO_WORKSPACE}/collector/cmd/collector-metrics/ +.PHONY: binary-test +binary-test: binary-dep + go test -v -tags "static" ./... - chmod +x "/build/scrutiny-collector-metrics-$(OS)-$(ARCH)" - file "/build/scrutiny-collector-metrics-$(OS)-$(ARCH)" || true - ldd "/build/scrutiny-collector-metrics-$(OS)-$(ARCH)" || true +.PHONY: binary-test-coverage +binary-test-coverage: binary-dep + go test -race -coverprofile=coverage.txt -covermode=atomic -v -tags "static" ./... -windows/amd64: export OS = windows -windows/amd64: export ARCH = amd64 -windows/amd64: - @echo "building web binary (OS = $(OS), ARCH = $(ARCH))" - xgo -v --targets="$(OS)/$(ARCH)" -ldflags "-extldflags=-static -X main.goos=$(OS) -X main.goarch=$(ARCH)" -out scrutiny-web -tags "static netgo sqlite_omit_load_extension" ${GO_WORKSPACE}/webapp/backend/cmd/scrutiny/ +.PHONY: binary-collector +binary-collector: binary-dep + go build -ldflags "$(LD_FLAGS)" -o $(COLLECTOR_BINARY_NAME) -tags "static netgo" ./collector/cmd/collector-metrics/ +ifneq ($(OS),Windows_NT) + chmod +x $(COLLECTOR_BINARY_NAME) + file $(COLLECTOR_BINARY_NAME) || true + ldd $(COLLECTOR_BINARY_NAME) || true + ./$(COLLECTOR_BINARY_NAME) || true +endif - @echo "building collector binary (OS = $(OS), ARCH = $(ARCH))" - xgo -v --targets="$(OS)/$(ARCH)" -ldflags "-extldflags=-static -X main.goos=$(OS) -X main.goarch=$(ARCH)" -out scrutiny-collector-metrics -tags "static netgo" ${GO_WORKSPACE}/collector/cmd/collector-metrics/ +.PHONY: binary-web +binary-web: binary-dep + go build -ldflags "$(LD_FLAGS)" -o $(WEB_BINARY_NAME) -tags "static netgo sqlite_omit_load_extension" ./webapp/backend/cmd/scrutiny/ +ifneq ($(OS),Windows_NT) + chmod +x $(WEB_BINARY_NAME) + file $(WEB_BINARY_NAME) || true + ldd $(WEB_BINARY_NAME) || true + ./$(WEB_BINARY_NAME) || true +endif +.PHONY: binary-frontend +# reduce logging, disable angular-cli analytics for ci environment +binary-frontend: export NPM_CONFIG_LOGLEVEL = warn +binary-frontend: export NG_CLI_ANALYTICS = false +binary-frontend: + cd webapp/frontend + npm install -g @angular/cli@9.1.4 + mkdir -p $(CURDIR)/dist + npm ci + npm run build:prod -- --output-path=$(CURDIR)/dist + +############# +TARGETARCH ?= amd64 + +.PHONY: docker-web docker-collector: @echo "building collector docker image" - docker build --build-arg TARGETARCH=amd64 -f docker/Dockerfile.collector -t analogj/scrutiny-dev:collector . + docker build --build-arg TARGETARCH=$(TARGETARCH) -f docker/Dockerfile.collector -t analogj/scrutiny-dev:collector . +.PHONY: docker-web docker-web: @echo "building web docker image" - docker build --build-arg TARGETARCH=amd64 -f docker/Dockerfile.web -t analogj/scrutiny-dev:web . + docker build --build-arg TARGETARCH=$(TARGETARCH) -f docker/Dockerfile.web -t analogj/scrutiny-dev:web . +.PHONY: docker-omnibus docker-omnibus: @echo "building omnibus docker image" - docker build --build-arg TARGETARCH=amd64 -f docker/Dockerfile -t analogj/scrutiny-dev:omnibus . - -# reduce logging, disable angular-cli analytics for ci environment -frontend: export NPM_CONFIG_LOGLEVEL = warn -frontend: export NG_CLI_ANALYTICS = false -frontend: - cd webapp/frontend - npm install -g @angular/cli@9.1.4 - mkdir -p $(CURDIR)/dist - npm ci - npm run build:prod -- --output-path=$(CURDIR)/dist - -# clean: -# rm scrutiny-collector-metrics-* scrutiny-web-* + docker build --build-arg TARGETARCH=$(TARGETARCH) -f docker/Dockerfile -t analogj/scrutiny-dev:omnibus . diff --git a/README.md b/README.md index 3eb5620..6871fe7 100644 --- a/README.md +++ b/README.md @@ -243,7 +243,7 @@ scrutiny-collector-metrics run --debug --log-file /tmp/collector.log | freebsd | collector only. see [#238](https://github.com/AnalogJ/scrutiny/issues/238) | | | macos-amd64 | | :white_check_mark: | | macos-arm64 | | :white_check_mark: | -| windows-amd64 | :white_check_mark: | | +| windows-amd64 | :white_check_mark: | WIP, see [#15](https://github.com/AnalogJ/scrutiny/issues/15) | # Contributing diff --git a/go.mod b/go.mod index 618b5c0..b920df4 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/analogj/go-util v0.0.0-20190301173314-5295e364eb14 + github.com/citilinkru/libudev v1.0.0 github.com/containrrr/shoutrrr v0.4.4 github.com/fatih/color v1.10.0 github.com/gin-gonic/gin v1.6.3 @@ -24,7 +25,6 @@ require ( require ( github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect - github.com/citilinkru/libudev v1.0.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deepmap/oapi-codegen v1.8.2 // indirect