Skip to content

Commit ddc8a80

Browse files
authored
use different ui build strategy (#196)
* use different ui build strategy * fix failing build test * remove forgotten comment
1 parent dff544c commit ddc8a80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+435
-1303
lines changed

.dockerignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.vscode/
2+
.idea/
3+
.DS_Store
4+
dist/*
5+
*.iml
6+
!defaults.ini
7+
*.ini

.github/workflows/go-test.yml

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ jobs:
1313
cache-dependency-path: go.sum
1414
- uses: actions/setup-node@v3
1515
with:
16-
node-version: 16
16+
node-version: 20
1717
cache: 'npm'
18-
cache-dependency-path: ui/package-lock.json
18+
cache-dependency-path: ui/yarn.lock
1919
- name: Install dependencies
20-
run: go get .
20+
run: go mod download
2121
- name: Build UI
2222
working-directory: ./ui
2323
run: |
24-
npm ci
25-
npm run build
24+
yarn install --frozen-lockfile
25+
yarn run build
2626
- name: Build Backend
2727
run: |
28-
go generate ./...
2928
go build -v ./...
3029
- name: Test
3130
run: go test -v ./...

.github/workflows/release.yml

-13
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ jobs:
2727
with:
2828
go-version: '1.21'
2929
cache: true
30-
-
31-
name: Install Node.js
32-
uses: actions/setup-node@v3
33-
with:
34-
node-version: 16
35-
cache: 'npm'
36-
cache-dependency-path: ui/package-lock.json
37-
-
38-
name: Build UI
39-
working-directory: ./ui
40-
run: |
41-
npm ci
42-
npm run build
4330
-
4431
name: Run GoReleaser
4532
uses: goreleaser/goreleaser-action@v4

.gitignore

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
.vscode/
2+
.idea/
3+
.DS_Store
4+
vendor/
5+
dist/*
6+
ui/dist/*
7+
!ui/dist/gitkeep
8+
*.iml
9+
debug.test
10+
coverage.out
11+
.*.swp
12+
node_modules/
113
/bin/
214
/*.ini
315
!defaults.ini
@@ -6,6 +18,3 @@
618
*.test
719
*.xml
820
*.swp
9-
10-
# Goreleaser and UI artifacts
11-
/dist/

.goreleaser.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
# Make sure to check the documentation at https://goreleaser.com
33
before:
44
hooks:
5-
# You may remove this if you don't use go modules.
65
- go mod tidy
7-
# you may remove this if you don't need go generate
8-
- go generate ./...
6+
- make build-ui
97
builds:
108
-
119
env:
@@ -18,9 +16,13 @@ builds:
1816
- arm64
1917
mod_timestamp: '{{ .CommitTimestamp }}'
2018
flags:
19+
- -v
2120
- -trimpath
2221
ldflags:
23-
- -s -w -X github.com/drewhammond/chefbrowser/internal/common/version.version={{.Version}} -X github.com/drewhammond/chefbrowser/internal/common/version.commitHash={{.Commit}} -X github.com/drewhammond/chefbrowser/internal/common/version.date={{.CommitDate}}
22+
- -s -w
23+
- -X github.com/drewhammond/chefbrowser/internal/common/version.version={{ .Version }}
24+
- -X github.com/drewhammond/chefbrowser/internal/common/version.commitHash={{ .Commit }}
25+
- -X github.com/drewhammond/chefbrowser/internal/common/version.date={{ .CommitDate }}
2426
release:
2527
draft: true
2628
replace_existing_draft: true

Dockerfile

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
1+
ARG BASE_IMAGE=alpine:3.18
12
ARG USERNAME=chefbrowser
23
ARG UID=1001
34
ARG GID=1001
45

5-
FROM alpine:3.17 as alpine
6+
FROM $BASE_IMAGE as cb-base
67
ARG USERNAME
78
ARG UID
89
ARG GID
910
RUN apk add --update --no-cache ca-certificates shadow && \
1011
addgroup -g ${GID} ${USERNAME} && \
1112
adduser -u ${UID} -G ${USERNAME} --disabled-password --system ${USERNAME}
1213

14+
###################
15+
# UI build stage
16+
###################
17+
FROM --platform=$BUILDPLATFORM node:20-alpine3.18 AS ui-builder
18+
WORKDIR /src
19+
COPY ["ui/package.json", "ui/yarn.lock", "./"]
20+
RUN yarn install --network-timeout 200000 && \
21+
yarn cache clean
22+
23+
COPY ["ui/", "."]
24+
25+
ARG TARGETARCH
26+
RUN HOST_ARCH=$TARGETARCH NODE_ENV='production' NODE_ONLINE_ENV='online' NODE_OPTIONS=--max_old_space_size=8192 yarn run build
27+
28+
###################
29+
# Go build stage
30+
###################
31+
FROM --platform=$BUILDPLATFORM golang:1.21 as go-builder
32+
WORKDIR /go/src/github.com/drewhammond/chefbrowser
33+
COPY go.* ./
34+
RUN go mod download
35+
36+
COPY . .
37+
COPY --from=ui-builder /src/dist /go/src/github.com/drewhammond/chefbrowser/ui/dist
38+
ARG TARGETOS
39+
ARG TARGETARCH
40+
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH make build-backend
41+
42+
###################
43+
# Final stage
44+
###################
1345
FROM scratch
14-
ARG USERNAME
1546
ARG UID
1647
ARG GID
17-
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
18-
COPY --from=alpine /etc/passwd /etc/passwd
19-
COPY chefbrowser /go/bin/chefbrowser
48+
COPY --from=cb-base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
49+
COPY --from=cb-base /etc/passwd /etc/passwd
50+
COPY --from=go-builder /go/src/github.com/drewhammond/chefbrowser/dist/chefbrowser /usr/local/bin/
2051
USER ${UID}:${GID}
2152
EXPOSE 8080
22-
ENTRYPOINT ["/go/bin/chefbrowser"]
53+
ENTRYPOINT ["/usr/local/bin/chefbrowser"]

Makefile

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
BINARY_NAME = "chefbrowser"
22
RELEASE?=dev
3-
GIN_MODE?=release
43
DOCKER_NAMESPACE?=drewhammond
54
DOCKER_TAG?=latest
6-
GOOS ?= $(shell go env GOOS)
7-
GOARCH ?= $(shell go env GOARCH)
8-
GOBUILD=CGO_ENABLED=0 go build -trimpath
5+
HOST_GOOS ?= $(shell go env GOOS)
6+
HOST_GOARCH ?= $(shell go env GOARCH)
97
GIT_SHA=$(shell git rev-parse HEAD)
108
DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
119
BUILD_INFO_PATH="github.com/drewhammond/chefbrowser/internal/common/version"
1210
BUILD_INFO=-ldflags "-X $(BUILD_INFO_PATH).version=$(RELEASE) -X $(BUILD_INFO_PATH).commitHash=$(GIT_SHA) -X $(BUILD_INFO_PATH).date=$(DATE)"
11+
GOBUILD=CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -v -trimpath $(BUILD_INFO)
12+
CURRENT_DIR=$(shell pwd)
13+
DIST_DIR=$(CURDIR)/dist
14+
TARGET_ARCH?=linux/amd64
1315

1416
.PHONY: lint
1517
lint:
@@ -25,23 +27,24 @@ fmt:
2527

2628
.PHONY: ui-deps
2729
ui-deps:
28-
cd $(CURDIR)/ui && npm ci
30+
cd $(CURDIR)/ui && yarn install
2931

3032
.PHONY: build
31-
build: build-ui
32-
go generate ./...
33-
$(GOBUILD) -o bin/${BINARY_NAME}-$(GOOS)-$(GOARCH) $(BUILD_INFO) main.go
33+
build: build-ui build-backend
34+
35+
.PHONY: build-backend
36+
build-backend:
37+
$(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME) .
3438

3539
.PHONY: build-ui
3640
build-ui:
37-
rm -rf $(CURDIR)/internal/app/ui/dist/assets
38-
rm -f $(CURDIR)/internal/app/ui/dist/index.html
39-
rm -f $(CURDIR)/internal/app/ui/dist/manifest.json
40-
cd $(CURDIR)/ui && npm run build
41+
docker build -t chefbrowser-ui --platform=$(TARGET_ARCH) --target ui-builder .
42+
find $(CURDIR)/ui/dist -type f -not -name gitkeep -delete || true
43+
docker run --platform=$(TARGET_ARCH) -v $(CURDIR)/ui/dist:/tmp/app --rm -t chefbrowser-ui sh -c 'cp -r ./dist/* /tmp/app/'
4144

4245
.PHONY: build-linux
4346
build-linux:
44-
GOOS=linux GOARCH=amd64 $(MAKE) build
47+
GOOS=linux GOARCH=amd64 TARGET_ARCH=linux/amd64 $(MAKE) build
4548

4649
.PHONY: build-docker
4750
build-docker: build-linux

go.mod

-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ require (
1111
github.com/labstack/echo/v4 v4.11.1
1212
github.com/spf13/cobra v1.7.0
1313
github.com/spf13/viper v1.16.0
14-
github.com/stretchr/testify v1.8.4
1514
go.uber.org/zap v1.25.0
1615
golang.org/x/mod v0.12.0
1716
)
1817

1918
require (
20-
github.com/davecgh/go-spew v1.1.1 // indirect
2119
github.com/fsnotify/fsnotify v1.6.0 // indirect
2220
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
2321
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -28,7 +26,6 @@ require (
2826
github.com/mattn/go-isatty v0.0.19 // indirect
2927
github.com/mitchellh/mapstructure v1.5.0 // indirect
3028
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
31-
github.com/pmezard/go-difflib v1.0.0 // indirect
3229
github.com/spf13/afero v1.9.5 // indirect
3330
github.com/spf13/cast v1.5.1 // indirect
3431
github.com/spf13/jwalterweatherman v1.1.0 // indirect

internal/app/ui/dist/.gitignore

-2
This file was deleted.

internal/app/ui/routes.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ui
22

33
import (
44
"crypto/tls"
5-
"embed"
65
"errors"
76
"fmt"
87
"html/template"
@@ -14,26 +13,18 @@ import (
1413
"github.com/drewhammond/chefbrowser/internal/chef"
1514
"github.com/drewhammond/chefbrowser/internal/common/logging"
1615
"github.com/drewhammond/chefbrowser/internal/common/version"
16+
"github.com/drewhammond/chefbrowser/ui"
1717
"github.com/foolin/goview"
1818
"github.com/foolin/goview/supports/echoview-v4"
1919
"github.com/labstack/echo/v4"
2020
"go.uber.org/zap"
2121
)
2222

23-
// This instruction runs in the build scripts to drop the compiled frontend assets (JS, CSS, and manifest file)
24-
// It's probably not the best way to do this, so let me know if there's a better way to do this!
25-
//
26-
//go:generate pwd
27-
//go:generate ls -ltr ../../../ui/dist/
28-
//go:generate cp -v -r ../../../ui/dist/ ./dist
29-
//go:embed templates/* dist/*
30-
var ui embed.FS
31-
32-
var viteFS = echo.MustSubFS(ui, "dist")
23+
var viteFS = echo.MustSubFS(ui.Embedded, "dist")
3324

3425
func embeddedFH(config goview.Config, tmpl string) (string, error) {
3526
path := filepath.Join(config.Root, tmpl)
36-
bytes, err := ui.ReadFile(path + config.Extension)
27+
bytes, err := ui.Embedded.ReadFile(path + config.Extension)
3728
return string(bytes), err
3829
}
3930

@@ -81,7 +72,7 @@ func (s *Service) RegisterRoutes() {
8172
}
8273

8374
if s.config.App.AppMode == "production" {
84-
mf, _ := ui.ReadFile("dist/manifest.json")
75+
mf, _ := ui.Embedded.ReadFile("dist/manifest.json")
8576
vCfg.Manifest = mf
8677
}
8778

ui/.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.git
3+
Dockerfile

ui/.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ yarn-debug.log*
66
yarn-error.log*
77
pnpm-debug.log*
88
lerna-debug.log*
9-
9+
.yalc
10+
yalc.lock
1011
node_modules
11-
dist
12-
dist-ssr
1312
*.local
1413

1514
# Editor directories and files

ui/dist/gitkeep

Whitespace-only changes.

ui/embed.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ui
2+
3+
import "embed"
4+
5+
// Embedded contains embedded UI resources
6+
//
7+
//go:embed templates/* dist/*
8+
var Embedded embed.FS

0 commit comments

Comments
 (0)