Skip to content

Commit

Permalink
Simplify and clean up Makefile (gravitational#62)
Browse files Browse the repository at this point in the history
* Simplify and clean up Makefile

* Update Makefile

This fixes the `test` command and deploys assets to Teleport

* Update Makefile

don't print as much

* Update enterprise paths

* Don't archive artifacts anymore

* Stop removing `dist` before building

* Explicitly push to a branch on origin

* Update Makefile

initialize teleport submodules after cloning

* Remove build artifacts, such as teleport

* Fetch submodule updates before updating

* Address CR comments

Co-authored-by: Pierre Beaucamp <pierre@gravitational.com>
  • Loading branch information
alex-kovoy and Pierre Beaucamp authored Apr 7, 2020
1 parent dc1dafe commit 56f6961
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 151 deletions.
2 changes: 1 addition & 1 deletion web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ coverage
dist
**/dist
!packages/force/dist
!packages/gravity/dist
!packages/gravity/dist
5 changes: 3 additions & 2 deletions web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM node:12-slim
RUN apt-get update && apt-get install git -y
ENV TZ=America/New_York

RUN mkdir -p web-apps
COPY yarn.lock web-apps/
Expand All @@ -25,4 +24,6 @@ RUN yarn install
# copy the rest of the files and run yarn build command
COPY . .
ARG NPM_SCRIPT
RUN yarn run $NPM_SCRIPT
ARG OUTPUT
# run npm script with optional --output-path parameter
RUN yarn $NPM_SCRIPT $([ -z $OUTPUT ] || echo --output-path=$OUTPUT)
10 changes: 2 additions & 8 deletions web/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ pipeline {
}
stage('test') {
steps {
sh "make clean check"
sh "make clean test"
}
}
stage('gather artifacts') {
steps {
sh "make dist packages/webapps.e/dist"
sh "make build"
}
}
}

post {
success {
archiveArtifacts artifacts: 'dist/**/*,packages/webapps.e/dist/**/*', fingerprint: true
}
}
}
179 changes: 67 additions & 112 deletions web/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,53 @@ CONTAINER_NAME := web-apps-container-$(shell bash -c 'echo $$RANDOM')
ROOT = $(shell pwd)
BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
COMMIT = $(shell git rev-parse --short HEAD)
COMMIT_DESC = $(shell git log --decorate=off --oneline -1)
COMMIT_URL = https://github.com/gravitational/webapps/commit/$(COMMIT)

.PHONY: build
build:
@if [ -d "packages/webapps.e/" ]; then \
$(MAKE) docker-build NPM_CMD=build-all FROM=dist/ TO=dist/; \
else \
$(MAKE) docker-build NPM_CMD=build-oss FROM=dist/ TO=dist/; \
fi;

# Below, we specify which files make up the source a particular library. We will later use those variables as
# dependencies for each target
COMMON_SRC = $(shell find packages -type f -a \( -path 'packages/build/*' -o -path 'packages/design/*' -o -path 'packages/shared/*' \))
COMMON_E_SRC = $(shell find packages/webapps.e/shared -type f 2> /dev/null)
FORCE_SRC = $(COMMON_SRC) $(shell find packages/force -type f -not -path '*/dist/*')
GRAVITY_SRC = $(COMMON_SRC) $(shell find packages/gravity -type f -not -path '*/dist/*')
GRAVITY_E_SRC = $(COMMON_E_SRC) $(GRAVITY_SRC) $(shell find packages/webapps.e/gravity -type f -not -path '*/dist/*' 2> /dev/null)
TELEPORT_SRC = $(COMMON_SRC) $(shell find packages/teleport -type f -not -path '*/dist/*')
TELEPORT_E_SRC = $(COMMON_E_SRC) $(TELEPORT_SRC) $(shell find packages/webapps.e/teleport -type f -not -path '*/dist/*' 2> /dev/null)


# all is the default target which compiles all packages
.PHONY: all
all: packages/gravity/dist packages/teleport/dist packages/webapps.e/teleport/dist packages/webapps.e/gravity/dist
all: packages/force/dist


# The next few recipes are all instructions on how to build a specific target. As a reminder, Makefile recipes have
# the following syntax:
#
# path_to_build_target: build_dependencies
# instructions_to_build_said_target
#
# I.e. the following rule is for building the path `packages/force/dist`, lists all the files we listed earlier
# in `FORCE_SRC` as dependencies for building this path, and has an instruction on how to achieve that (using
# the `docker-build` command.
packages/force/dist: $(FORCE_SRC)
$(MAKE) docker-build PACKAGE_PATH=packages/force NPM_CMD=build-force
.PHONY: test
test:
$(MAKE) docker-build NPM_CMD=test

packages/gravity/dist: $(GRAVITY_SRC)
$(MAKE) docker-build PACKAGE_PATH=packages/gravity NPM_CMD=build-gravity
.PHONY: build-force
build-force:
$(MAKE) docker-build NPM_CMD=build-force FROM=dist/force/ TO=dist/force

packages/teleport/dist: $(TELEPORT_SRC)
$(MAKE) docker-build PACKAGE_PATH=packages/teleport NPM_CMD=build-teleport
.PHONY: build-gravity
build-gravity:
$(MAKE) docker-build NPM_CMD=build-gravity FROM=dist/gravity/ TO=dist/gravity

# The enterprise files are only build if the submodule is available
packages/webapps.e/gravity/dist: $(GRAVITY_E_SRC)
@if [ -d "packages/webapps.e/gravity" ]; then \
$(MAKE) docker-build PACKAGE_PATH=packages/webapps.e/gravity NPM_CMD=build-gravity-e; \
fi;
.PHONY: build-teleport
build-teleport:
$(MAKE) docker-build NPM_CMD=build-teleport FROM=dist/teleport/ TO=dist/teleport

packages/webapps.e/teleport/dist: $(TELEPORT_E_SRC)
@if [ -d "packages/webapps.e/teleport" ]; then \
$(MAKE) docker-build PACKAGE_PATH=packages/webapps.e/teleport NPM_CMD=build-teleport-e; \
fi;
.PHONY: build-gravity-e
build-gravity-e:
$(MAKE) docker-build NPM_CMD=build-gravity-e FROM=dist/e/gravity/ TO=dist/e/gravity;

.PHONY: build-teleport-e
build-teleport-e:
$(MAKE) docker-build NPM_CMD=build-teleport-e; FROM=dist/e/teleport/ TO=dist/e/teleport;

# docker-build lists the common instructions on how to build one of our targets using docker. See the "real" targets,
# such as `packages/gravity/dist` on how to invoke this.
# Calling this target without providing the necessary variables (PACKAGE_PATH and NPM_CMD) will result in errors.
# builds package dists files in docker (TODO: move it to scripts/docker-build.sh)
.PHONY: docker-build
docker-build:
rm -rf $(ROOT)/$(PACKAGE_PATH)/dist
docker build --force-rm=true --build-arg NPM_SCRIPT=$(NPM_CMD) -t $(IMAGE_NAME) .
docker create --name $(CONTAINER_NAME) -t $(IMAGE_NAME) && \
docker cp $(CONTAINER_NAME):/web-apps/$(PACKAGE_PATH)/dist $(ROOT)/$(PACKAGE_PATH)/
docker rm -f $(CONTAINER_NAME)
@if [ "$(TO)" != "" ] || [ "$(FROM)" != "" ]; then \
mkdir -p $(ROOT)/$(TO); \
docker create --name $(CONTAINER_NAME) -t $(IMAGE_NAME) && \
docker cp $(CONTAINER_NAME):/web-apps/$(FROM)/. $(ROOT)/$(TO); \
docker rm -f $(CONTAINER_NAME); \
fi;

# docker-enter is a shorthand for entering the build image, for example for debugging, or in case yarn cannot
# be used locally
# docker-enter is a shorthand for entering the image
.PHONY: docker-enter
docker-enter:
docker run -ti --rm=true -t $(IMAGE_NAME) /bin/bash
Expand All @@ -74,73 +60,42 @@ docker-enter:
docker-clean:
docker rmi --force $(IMAGE_NAME)


# deploy uploads the latest build artifacts for deployment. This rule is usually invoked by our CI servers,
# but may be used manually if your account has the right permissions.
# In essence, this target aggregates the various build artifacts that we compiled earlier & pushes them to a
# special place.
.PHONY: deploy
deploy: dist packages/webapps.e/dist
@if [ "$(shell git --git-dir dist/.git rev-parse --abbrev-ref HEAD)" != "$(BRANCH)" ]; then \
echo "Branch has changed since compilation, please run 'make clean' "; exit 2; \
fi;
@if [ "$(shell git --git-dir packages/webapps.e/dist/.git rev-parse --abbrev-ref HEAD)" != "$(BRANCH)" ]; then \
echo "Branch has changed since compilation, please run 'make clean'"; exit 2; \
fi;
@if [ -d "packages/webapps.e/dist" ]; then \
cd packages/webapps.e/dist; git add -A; git commit -am 'Update build artifacts from $(COMMIT)'; \
git push; \
fi;
cd dist; git add -A; git commit -am 'Update build artifacts from $(COMMIT)'; git push

# Note this is not creating a tar file, thus deviating from the GNU coding standards for Makefiles
dist: packages/gravity/dist packages/teleport/dist packages/force/dist
rm -rf dist
git clone git@github.com:gravitational/webassets.git dist
# update webassets repository with the new /dist files
.PHONY: update-webassets-repo
update-webassets-repo:
@if [ -z "$(TELEPORT_TARGET)" ]; then \
echo "TELEPORT_TARGET is not set"; exit 1; \
fi
# prepare webassets repo
rm -rf dist && git clone git@github.com:gravitational/webassets.git dist
cd dist; git checkout $(BRANCH) || git checkout -b $(BRANCH)
rm -rf dist/force dist/gravity dist/teleport
mkdir -p dist/force && cp -r packages/force/dist/* dist/force
mkdir -p dist/gravity && cp -r packages/gravity/dist/* dist/gravity
mkdir -p dist/teleport && cp -r packages/teleport/dist/* dist/teleport

packages/webapps.e/dist: packages/webapps.e/teleport/dist packages/webapps.e/gravity/dist
rm -rf packages/webapps.e/dist
git clone git@github.com:gravitational/webassets.e.git packages/webapps.e/dist
cd packages/webapps.e/dist; git checkout $(BRANCH) || git checkout -b $(BRANCH)
rm -rf packages/webapps.e/dist/gravity.e packages/webapps.e/dist/teleport.e
mkdir -p packages/webapps.e/dist/gravity.e && cp -r packages/webapps.e/gravity/dist/* packages/webapps.e/dist/gravity.e
mkdir -p packages/webapps.e/dist/teleport.e && cp -r packages/webapps.e/teleport/dist/* packages/webapps.e/dist/teleport.e


# check runs the test suite
.PHONY: check
check: all
docker build --force-rm=true --build-arg NPM_SCRIPT=test -t $(IMAGE_NAME)-test .

# clean removes files that can be generated by this Makefile
# prepare webassets.e repo
cd dist; git submodule update --init --recursive
cd dist/e; git checkout $(BRANCH) || git checkout -b $(BRANCH)
# prepare teleport repo
echo teleport >> .gitignore
rm -rf teleport && git clone git@github.com:gravitational/teleport.git
cd teleport; git checkout $(TELEPORT_TARGET) || git checkout -b $(TELEPORT_TARGET)
cd teleport; git fetch --recurse-submodules && git submodule update --init webassets
# build the dist files
$(MAKE) build
# push dist files to webasset/e repositories
cd dist/e; git add -A .; git commit -am '$(COMMIT_DESC)' -m '$(COMMIT_URL)' --allow-empty; git push origin $(BRANCH)
cd dist; git add -A .; git commit -am '$(COMMIT_DESC)' -m '$(COMMIT_URL)' --allow-empty; git push origin $(BRANCH)
# use temporary file to store commit sha
cd dist; git rev-parse HEAD >> commit_sha;
$(eval WEBAPPS_HEAD=$(shell cat dist/commit_sha))
# update teleport
cd teleport/webassets; git checkout $(WEBAPPS_HEAD)
cd teleport; git add -A .; git commit -am 'Update webassets' -m '$(COMMIT_DESC) $(COMMIT_URL)' --allow-empty
cd teleport; git push origin $(TELEPORT_TARGET)

# clean removes this repo generated artifacts
.PHONY: clean
clean:
rm -rf packages/gravity/dist packages/teleport/dist packages/force/dist
rm -rf packages/webapps.e/gravity/dist packages/webapps.e/teleport/dist

# distcleans removes all files that are not part of the repository
.PHONY: distclean
distclean: clean
rm -rf dist teleport
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +


# Some non-standard targets:

# Some of our other projects use `make test` to launch the test suite
.PHONY: test
test: check

# install installs npm / yarn dependencies
# Note this is not a typical `install` targets, as with most other Makefiles
.PHONY:install
install:
bash -c "./scripts/install.sh"

# init-submodules initializes / updates the submodules in this repo
.PHONY: init-submodules
init-submodules:
Expand Down
4 changes: 0 additions & 4 deletions web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,3 @@ This command will open a new browser window with storybook in it. There
you will see components from all packages so it makes it faster to work
and iterate on shared functionality.


### Deployment

Deployment is automated through our CI system. Each commit to the `master` branch will be built & deployed.
24 changes: 14 additions & 10 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
"test": "jest",
"test-coverage": "jest --coverage && scripts/print-coverage-link.sh",
"tdd": "jest --watch",
"start-force": "yarn workspace @gravitational/force run start",
"start-gravity": "yarn workspace @gravitational/gravity run start",
"start-teleport": "yarn workspace @gravitational/teleport run start",
"start-gravity-e": "yarn workspace @gravitational/gravity.e run start",
"start-teleport-e": "yarn workspace @gravitational/teleport.e run start",
"build-force": "yarn workspace @gravitational/force run build",
"build-gravity": "yarn workspace @gravitational/gravity run build",
"build-teleport": "yarn workspace @gravitational/teleport run build",
"build-gravity-e": "yarn workspace @gravitational/gravity.e run build",
"build-teleport-e": "yarn workspace @gravitational/teleport.e run build"
"start-force": "yarn workspace @gravitational/force start",
"start-gravity": "yarn workspace @gravitational/gravity start",
"start-teleport": "yarn workspace @gravitational/teleport start",
"start-gravity-e": "yarn workspace @gravitational/gravity.e start",
"start-teleport-e": "yarn workspace @gravitational/teleport.e start",
"build-force": "yarn workspace @gravitational/force build --output-path=../../dist/force/app",
"build-gravity": "yarn workspace @gravitational/gravity build --output-path=../../dist/gravity/app",
"build-teleport": "yarn workspace @gravitational/teleport build --output-path=../../dist/teleport/app",
"build-gravity-e": "yarn workspace @gravitational/gravity.e build --output-path=../../../dist/e/gravity/app",
"build-teleport-e": "yarn workspace @gravitational/teleport.e build --output-path=../../dist/e/teleport/app",
"build-oss": "yarn build-force && yarn build-teleport && yarn build-gravity",
"build-e": "yarn build-teleport-e && yarn build-gravity-e",
"build-all": "yarn type-check; yarn build-oss && yarn build-e",
"type-check": "yarn tsc"
},
"private": true,
"workspaces": [
Expand Down
1 change: 1 addition & 0 deletions web/packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"url-loader": "1.1.2",
"webpack": "4.33.0",
"webpack-bundle-analyzer": "^3.3.2",
"clean-webpack-plugin": "3.0.0",
"webpack-cli": "3.3.2",
"webpack-dev-server": "3.1.11"
}
Expand Down
2 changes: 2 additions & 0 deletions web/packages/build/webpack/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

const webpack = require('webpack');
const createConfig = require('./webpack.base');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const baseCfg = createConfig();

process.env.BABEL_ENV = 'production';
Expand Down Expand Up @@ -50,6 +51,7 @@ var cfg = {

plugins: [
// new BundleAnalyzerPlugin(),
new CleanWebpackPlugin(),
new webpack.HashedModuleIdsPlugin(),
baseCfg.plugins.createIndexHtml(),
],
Expand Down
2 changes: 1 addition & 1 deletion web/packages/force/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "g-start",
"build": "tsc && (rm -rf ./dist; mkdir ./dist; g-build --progress --bail)",
"build": "g-build --progress --bail",
"test": "npx jest",
"tdd": "jest . --watch",
"type-check": "tsc",
Expand Down
2 changes: 1 addition & 1 deletion web/packages/gravity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "g-start",
"build": "rm -rf ./dist; mkdir ./dist; g-build --progress --bail",
"build": "g-build --progress --bail",
"test": "jest",
"tdd": "jest . --watch"
},
Expand Down
2 changes: 1 addition & 1 deletion web/packages/teleport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "g-start",
"build": "rm -rf ./dist; mkdir ./dist; g-build --progress --bail",
"build": "g-build --progress --bail",
"test": "npx jest",
"tdd": "jest . --watch"
},
Expand Down
9 changes: 0 additions & 9 deletions web/scripts/install.sh

This file was deleted.

Loading

0 comments on commit 56f6961

Please # to comment.