Skip to content

Commit

Permalink
🧘 deploy: Simplify deployment process (#234)
Browse files Browse the repository at this point in the history
Simplify deployment process by copying files from `frontend/**` to
`out/firebase/public/**` once only. Copy all firebase configs to out/firebase
and run deployment from there. Clean up various little make things along the
way.

This merges the following commits:
* build: Move version.json generation
* make: Rework frontend-serve target
* build: Remove separate `frontend` build step
* scripts: Reorder functions for readability
* deploy: Move deployment prep folder to out/
* make: Simplify deploy target naming

     .github/workflows/cicd.yaml |  4 ++--
     .gitignore                  |  2 +-
     Makefile                    | 30 +++++++++++-------------------
     scripts/firebase-deploy     | 31 ++++++++++++++++---------------
     4 files changed, 30 insertions(+), 37 deletions(-)

Pull-Request: #234
  • Loading branch information
juliaogris committed Jan 6, 2024
2 parents 8fe18be + ebd7285 commit cd0fb92
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- run: ./bin/make ci
env:
TERM: vt100
- run: ./bin/make firebase-deploy
- run: ./bin/make deploy
env:
FIREBASE_SERVICE_ACCOUNT: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_EVY_LANG }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -35,7 +35,7 @@ jobs:
GITHUB_APP_ID: ${{ secrets.EVYLANGBOT_GITHUB_APP_ID }}
GITHUB_APP_PEM: ${{ secrets.EVYLANGBOT_GITHUB_APP_PEM }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: ./bin/make firebase-deploy-prod
- run: ./bin/make deploy-prod
env:
FIREBASE_SERVICE_ACCOUNT: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_EVY_LANG }}

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/.hermit/
/firebase/*.log
/firebase/.firebase/
/firebase/public/
/frontend/evy.wasm
/frontend/version.json
/frontend/wasm_exec.js
/out/
30 changes: 11 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tiny: go-version | $(O)
GOOS=wasip1 GOARCH=wasm tinygo build -o $(O)/evy-unopt.wasm -no-debug -ldflags='$(GO_LDFLAGS)' -stack-size=512kb ./pkg/wasm
wasm-opt -O3 $(O)/evy-unopt.wasm -o frontend/evy.wasm
cp -f $$(tinygo env TINYGOROOT)/targets/wasm_exec.js frontend/
echo '{ "version": "$(VERSION)" }' | jq > frontend/version.json

## Tidy go modules with "go mod tidy"
tidy:
Expand All @@ -54,6 +55,7 @@ fmt:
clean::
-rm -f frontend/evy.wasm
-rm -f frontend/wasm_exec.js
-rm -f frontend/version.json

.PHONY: build go-version install tidy tiny

Expand Down Expand Up @@ -125,15 +127,9 @@ godoc: install
# --- frontend -----------------------------------------------------------------
NODELIB = .hermit/node/lib

## Build frontend, typically iterate with npm and inside frontend
frontend: tiny | $(O)
rm -rf $(O)/public
cp -r frontend $(O)/public
echo '{ "version": "$(VERSION)" }' | jq > $(O)/public/version.json

## Build frontend and serve on free port
frontend-serve: frontend
servedir $(O)/public
## Serve frontend on free port
serve:
servedir frontend

## Format code with prettier
prettier: | $(NODELIB)
Expand All @@ -146,30 +142,26 @@ check-prettier: | $(NODELIB)
$(NODELIB):
@mkdir -p $@

.PHONY: check-prettier frontend frontend-serve prettier
.PHONY: check-prettier prettier serve

# --- firebase -----------------------------------------------------------------
# --- deploy -----------------------------------------------------------------

## Deploy to live channel on firebase prod, use with care!
## `firebase login` for first time local usage
firebase-deploy-prod: frontend
deploy-prod: tiny
./scripts/firebase-deploy prod live

## Deploy to live channel on firebase stage.
## `firebase login` for first time local usage
firebase-deploy-stage: frontend
deploy-stage: tiny
./scripts/firebase-deploy stage live

## Deploy to dev (or other) channel on firebase stage.
## `firebase login` for first time local usage
firebase-deploy: frontend
deploy: tiny
./scripts/firebase-deploy stage

## Run firebase emulator for auth, hosting and datastore
firebase-emulate: frontend
firebase --config firebase/firebase.json emulators:start

.PHONY: firebase-deploy firebase-deploy-prod firebase-emulate
.PHONY: deploy deploy-prod deploy-stage

# --- scripts ------------------------------------------------------------------
SCRIPTS = scripts/firebase-deploy .github/scripts/app_token
Expand Down
31 changes: 16 additions & 15 deletions scripts/firebase-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ main() {

environ=$(get_environment "${1:-stage}") || exit $?
channel=$(get_channel "${2:-}")
rm -rf firebase/public
cp -r out/public firebase
rm -rf out/firebase
cp -r firebase out/
cp -r frontend out/firebase/public
if [[ "${channel}" == "live" ]]; then
update_links "${environ}"
# `firebase deploy` must be used with live channel
firebase --config firebase/firebase.json --project "${environ}" deploy --only hosting
firebase --config out/firebase/firebase.json --project "${environ}" deploy --only hosting
exit 0
fi

# `firebase hosting:channel:deploy` must be used with preview/non-live channels
result=$(firebase --json --config firebase/firebase.json --project "${environ}" hosting:channel:deploy "${channel}")
result=$(firebase --json --config out/firebase/firebase.json --project "${environ}" hosting:channel:deploy "${channel}")
check_deploy_error "${result}"
urls=$(get_urls "${result}")
printf "Deployed to \n%s\n" "${urls}"
Expand Down Expand Up @@ -76,12 +77,22 @@ get_pr_num() {
echo "${pr_num}"
}

update_links() {
local environ="$1" domain="evystage.dev"
if [[ "${environ}" == "prod" ]]; then
domain="evy.dev"
fi

echo "update links $domain"
find frontend -name "*.html" -exec bash -c 'update_links_in_file "$1" "$2"' _ ${domain} {} \;
}

# update_links_in_file updates links in the given HTML file to subdomain,
# e.g.: href="/docs" → href="https://docs.evy.dev" relevant subdomains are:
# discord, docs, learn and play.
update_links_in_file() {
local domain="$1" file="$2" target
target="firebase/${file#out/}"
target="out/firebase/public/${file#frontend/}"
sed -E \
-e 's (href|value)="/(discord|docs|learn|play) \1="https://\2.'"${domain} g" \
"${file}" >"${target}"
Expand All @@ -90,16 +101,6 @@ update_links_in_file() {

export -f update_links_in_file

update_links() {
local environ="$1" domain="evystage.dev"
if [[ "${environ}" == "prod" ]]; then
domain="evy.dev"
fi

echo "update links $domain"
find out/public -name "*.html" -exec bash -c 'update_links_in_file "$1" "$2"' _ ${domain} {} \;
}

check_deploy_error() {
local result="$1" status
status=$(jq -r '.status' <<<"${result}")
Expand Down

0 comments on commit cd0fb92

Please sign in to comment.