Skip to content

Commit 2c3d2d4

Browse files
authored
Feat: Cross-platform Build (#123)
* feat: multi-platform build * doc: update readme * chore: no docker push according to code review
1 parent 49a79c6 commit 2c3d2d4

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*.dylib
99

1010
# build
11-
dtm
11+
dtm*
12+
build/output
13+
build/working_dir
1214

1315
# Test binary, build with `go test -c`
1416
*.test

Makefile

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
MKFILE_PATH=$(abspath $(lastword $(MAKEFILE_LIST)))
2+
BUILD_PATH=$(patsubst %/,%,$(dir $(MKFILE_PATH)))/build/working_dir
3+
14
help: ## Display this help.
2-
@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)
5+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-18s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
36

47
build: fmt vet ## Build dtm & plugins locally.
58
go mod tidy
@@ -9,10 +12,45 @@ build: fmt vet ## Build dtm & plugins locally.
912
go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/argocdapp_0.0.1.so ./cmd/argocdapp/
1013
go build -trimpath -gcflags="all=-N -l" -o dtm ./cmd/devstream/
1114

12-
build-core: fmt vet ## Build dtm core only, locally.
15+
build-core-only: fmt vet ## Build dtm core only, without plugins, locally.
1316
go mod tidy
1417
go build -trimpath -gcflags="all=-N -l" -o dtm ./cmd/devstream/
1518

19+
clean:
20+
rm -rf .devstream
21+
rm -f dtm*
22+
rm -rf build/working_dir
23+
24+
build-release: build-darwin-arm64 build-darwin-amd64 build-linux-amd64 ## Build for all platforms for release.
25+
26+
build-darwin-arm64: ## Build for darwin/arm64 for release.
27+
go mod tidy
28+
mkdir -p .devstream
29+
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/githubactions-darwin-arm64_0.0.1.so ./cmd/githubactions/
30+
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/argocd-darwin-arm64_0.0.1.so ./cmd/argocd/
31+
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/argocdapp-darwin-arm64_0.0.1.so ./cmd/argocdapp/
32+
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -trimpath -gcflags="all=-N -l" -o dtm-darwin-arm64 ./cmd/devstream/
33+
34+
build-darwin-amd64: ## Cross-platform build for darwin/amd64.
35+
go mod tidy
36+
mkdir -p .devstream
37+
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/githubactions-darwin-amd64_0.0.1.so ./cmd/githubactions/
38+
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/argocd-darwin-amd64_0.0.1.so ./cmd/argocd/
39+
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o .devstream/argocdapp-darwin-amd64_0.0.1.so ./cmd/argocdapp/
40+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -trimpath -gcflags="all=-N -l" -o dtm-darwin-amd64 ./cmd/devstream/
41+
42+
build-linux-amd64: ## Cross-platform build for linux/amd64
43+
echo "Building in ${BUILD_PATH}"
44+
mkdir -p .devstream
45+
rm -rf ${BUILD_PATH} && mkdir ${BUILD_PATH}
46+
docker buildx build --platform linux/amd64 --load -t mericodev/stream-builder:v0.0.1 -f build/package/Dockerfile .
47+
cp -r go.mod go.sum cmd internal build/package/build_linux_amd64.sh ${BUILD_PATH}/
48+
chmod +x ${BUILD_PATH}/build_linux_amd64.sh
49+
docker run --rm --platform linux/amd64 -v ${BUILD_PATH}:/devstream mericodev/stream-builder:v0.0.1
50+
mv ${BUILD_PATH}/output/*.so .devstream/
51+
mv ${BUILD_PATH}/output/dtm* .
52+
rm -rf ${BUILD_PATH}
53+
1654
fmt: ## Run 'go fmt' & goimports against code.
1755
go install golang.org/x/tools/cmd/goimports@latest
1856
goimports -local="github.com/merico-dev/stream" -d -w cmd

README.md

+32-7
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,28 @@ See [docs/architecture.md](./docs/architecture.md).
5757

5858
## Build
5959

60+
```makefile
61+
$ make help
62+
63+
Usage:
64+
make <target>
65+
help Display this help.
66+
build Build dtm & plugins locally.
67+
build-core-only Build dtm core only, without plugins, locally.
68+
build-release Build for all platforms for release.
69+
build-darwin-arm64 Build for darwin/arm64 for release.
70+
build-darwin-amd64 Cross-platform build for darwin/amd64.
71+
build-linux-amd64 Cross-platform build for linux/amd64
72+
fmt Run 'go fmt' & goimports against code.
73+
vet Run go vet against code.
74+
```
75+
76+
## Test
77+
78+
Unit/functional test:
79+
6080
```bash
61-
# build dtm & plugins
62-
make build
63-
# build dtm only
64-
make build-core
81+
go test ./...
6582
```
6683

6784
## Configuration
@@ -70,15 +87,23 @@ See [examples/config.yaml](./examples/config.yaml).
7087

7188
## Run
7289

73-
The CLI tool is named `dtm`, which is an acronym of **d**evs**t**rea**m** or *devops toolchain manager*:
90+
To install/reinstall/update, run:
7491

7592
```bash
76-
./dtm install -f examples/config.yaml
93+
./dtm apply -f examples/config.yaml
94+
```
95+
96+
To delete, run:
97+
98+
```bash
99+
./dtm delete -f examples/config.yaml
77100
```
78101

79102
## Why `dtm`?
80103

81-
Inspired by [`git`](https://github.com/git/git#readme), the name is (depending on your mood):
104+
Q: The CLI tool is named `dtm`, while the tool itself is called DevStream. What the heck?! Where is the consistency?
105+
106+
A: Inspired by [`git`](https://github.com/git/git#readme), the name is (depending on your mood):
82107

83108
- a symmetric, scientific acronym of **d**evs**t**rea**m**.
84109
- "devops toolchain manager": you're in a good mood, and it actually works for you.

build/package/Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1.17 as builder
2+
3+
WORKDIR /devstream
4+
5+
# cache deps before building so that source changes don't invalidate our downloaded layer
6+
COPY go.mod go.sum /devstream/
7+
8+
RUN go mod download
9+
10+
# the script will be copied to a mounted volume before docker run
11+
CMD ["./build_linux_amd64.sh"]

build/package/build_linux_amd64.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
export GOOS=linux
6+
export GOARCH=amd64
7+
go build -trimpath -gcflags="all=-N -l" -o output/dtm-${GOOS}-${GOARCH} ./cmd/devstream/
8+
go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o output/githubactions-${GOOS}-${GOARCH}_0.0.1.so ./cmd/githubactions/
9+
go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o output/argocd-${GOOS}-${GOARCH}_0.0.1.so ./cmd/argocd/
10+
go build -buildmode=plugin -trimpath -gcflags="all=-N -l" -o output/argocdapp-${GOOS}-${GOARCH}_0.0.1.so ./cmd/argocdapp/

0 commit comments

Comments
 (0)