Skip to content

Commit

Permalink
add production build setup and aws CICD config files
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkySharks committed Apr 1, 2019
1 parent ffe2734 commit 3853363
Show file tree
Hide file tree
Showing 18 changed files with 701 additions and 273 deletions.
41 changes: 41 additions & 0 deletions .bin/pr-checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env sh

# used for script output
message () {
echo ""
echo "==========================================================================="
echo ""
echo " " "$1"
echo ""
echo "==========================================================================="
echo ""
}
STATUS=0

message "Installing dependencies with yarn..."
yarn install --frozen-lockfile

# production build - needed for some of the subsequent checks, like storybook
message "Running production build..."
cp ./docker/.env.docker.prod ./packages/venia-concept/.env
yarn run build

# jest tests - does not fail ci, failures are evaluated by danger check below
message "Running unit tests with Jest..."
yarn run test:ci

message "Running Coveralls..."
yarn run coveralls

message "Running Storybook..."
yarn workspace @magento/venia-concept run storybook:build

message "Running Bundlesize..."
yarn run bundlesize

# danger ci - eslint, prettier:validate, unit test evaluation, etc.
message "Running Danger..."
yarn run danger || STATUS=$?

echo "Exit Status: $STATUS"
exit $STATUS
23 changes: 23 additions & 0 deletions .codebuild/buildspec.deploy.demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# buildspec for deploying latest changes to main development branch
version: 0.2
phases:
install:
commands:
- echo install step...
pre_build:
commands:
- echo logging in to AWS ECR...
- $(aws ecr get-login --no-include-email --region us-east-1)
build:
commands:
- echo build Docker image on `date`
- docker build -f Dockerfile.prod -t pwa-demo:latest .
- docker tag pwa-demo:latest 276375911640.dkr.ecr.us-east-1.amazonaws.com/pwa-demo
post_build:
commands:
- echo build Docker image complete `date`
- echo push latest Docker images to ECR...
- docker push 276375911640.dkr.ecr.us-east-1.amazonaws.com/pwa-demo
artifacts:
files:
- Dockerrun.aws.json
26 changes: 26 additions & 0 deletions .codebuild/buildspec.pr.build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 0.2
phases:
pre_build:
commands:
# get the PR number from version env var, originally in the format "pr/2"
- PR_ID_TAG=$(echo $CODEBUILD_SOURCE_VERSION | cut -d'/' -f 2)
- echo getting PR ID... PR_ID_TAG = $PR_ID_TAG
- echo logging in to AWS ECR...
- $(aws ecr get-login --no-include-email --region us-east-1)
build:
commands:
- echo build Docker image on `date` for github branch $CODEBUILD_SOURCE_VERSION
- docker build -f Dockerfile.prod -t pwa-pr-build:$PR_ID_TAG .
- docker tag pwa-pr-build:$PR_ID_TAG 276375911640.dkr.ecr.us-east-1.amazonaws.com/pwa-pr-build:$PR_ID_TAG
post_build:
commands:
- echo build Docker image complete `date` for github branch $CODEBUILD_SOURCE_VERSION
- echo push latest Docker images to ECR...
- docker push 276375911640.dkr.ecr.us-east-1.amazonaws.com/pwa-pr-build:$PR_ID_TAG
# change the Dockerrun.aws.json to use the pwa-pr-build registry address pointing at this pr image build
- sed -i "s/pwa-demo:latest/pwa-pr-build:${PR_ID_TAG}/g" Dockerrun.aws.json
artifacts:
files:
- Dockerrun.aws.json
name: pr-$(echo $CODEBUILD_SOURCE_VERSION | cut -d'/' -f 2)

18 changes: 18 additions & 0 deletions .codebuild/buildspec.pr.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 0.2
phases:
install:
commands:
- echo entering install step...
# for yarn
- curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
- echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
- apt-get update -y
- apt-get install -y yarn
build:
commands:
- echo running pr-checks script `date`
# contains all the pr check task scripts
- yarn run ci:pr-checks
post_build:
commands:
- echo pr test tasks complete `date`
34 changes: 13 additions & 21 deletions Dockerfile → Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COPY packages/venia-concept/package.json ./packages/venia-concept/package.json
COPY package.json yarn.lock babel.config.js browserslist.js magento-compatibility.js ./

# install dependencies with yarn
RUN yarn install
RUN yarn install --frozen-lockfile

# copy over the rest of the package files
COPY packages ./packages
Expand All @@ -35,24 +35,16 @@ COPY ${ENVFILEPATH} ./packages/venia-concept/.env
# build the app
RUN yarn run build

#######################################################################################
# UNCOMMENT FOR PRODUCTION BUILD - not as necessary for dev env to have non-root user #
#######################################################################################
# # MULTI-STAGE BUILD
# FROM node:10.14.1-alpine
# # working directory
# WORKDIR /usr/src/app
# # copy build from previous stage
# COPY --from=build /usr/src/app .
# # create and set non-root USER
# RUN addgroup -g 1001 appuser && \
# adduser -S -u 1001 -G appuser appuser
# RUN chown -R appuser:appuser /usr/src/app && \
# chmod 755 /usr/src/app
# USER appuser
#######################################################################################

# Pass the `WEBPACK_HOST` arg from docker-compose args and set it to the HOST
ARG WEBPACK_HOST
# MULTI-STAGE BUILD
FROM node:10.14.1-alpine
# working directory
WORKDIR /usr/src/app
# node:alpine comes with a configured user and group
RUN chown -R node:node /usr/src/app
# copy build from previous stage
COPY --from=build /usr/src/app .
USER node
# Pass the `HOST` arg from docker-compose args and set it to the HOST
ARG HOST
# command to run application
CMD [ "yarn", "workspace", "@magento/venia-concept", "run", "watch", "-- --host ${WEBPACK_HOST}"]
CMD [ "yarn", "workspace", "@magento/venia-concept", "run", "watch", "-- --host ${HOST}"]
46 changes: 46 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
FROM node:10.14.1-alpine as build
# working directory
WORKDIR /usr/src/app

# global environment setup : yarn + dependencies needed to support node-gyp
RUN apk --no-cache --virtual add \
python \
make \
g++ \
yarn

# copy just the dependency files and configs needed for install
COPY packages/peregrine/package.json ./packages/peregrine/package.json
COPY packages/pwa-buildpack/package.json ./packages/pwa-buildpack/package.json
COPY packages/upward-js/package.json ./packages/upward-js/package.json
COPY packages/upward-spec/package.json ./packages/upward-spec/package.json
COPY packages/venia-concept/package.json ./packages/venia-concept/package.json
COPY package.json yarn.lock babel.config.js browserslist.js magento-compatibility.js ./

# install dependencies with yarn
RUN yarn install --frozen-lockfile

# copy over the rest of the package files
COPY packages ./packages

# copy configuration env file from host file system to venia-concept .env for build
COPY ./docker/.env.docker.prod ./packages/venia-concept/.env

ENV NODE_ENV=production
# build the app
RUN yarn run build


# MULTI-STAGE BUILD
FROM node:10.14.1-alpine
# working directory
WORKDIR /usr/src/app
# node:alpine comes with a configured user and group
RUN chown -R node:node /usr/src/app
# copy build from previous stage
COPY --from=build /usr/src/app .
USER node
EXPOSE 8080
ENV NODE_ENV=production
# command to run application
CMD [ "yarn", "stage:venia" ]
12 changes: 12 additions & 0 deletions Dockerrun.aws.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "276375911640.dkr.ecr.us-east-1.amazonaws.com/pwa-demo:latest",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "8080"
}
]
}
24 changes: 22 additions & 2 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const tasks = [
const result = execa.sync('yarn', [
'run',
'--silent',
'prettier:check',
'prettier:validate',
'--loglevel=debug'
]);
stdout = result.stdout;
Expand Down Expand Up @@ -271,6 +271,7 @@ const tasks = [
'All tests must pass before this PR can be merged\n\n\n' +
failSummary
);
throw new Error(failSummary);
}

// function mergeJunitReports() {
Expand Down Expand Up @@ -306,6 +307,25 @@ const tasks = [
// }
];

const runTasks = async tasks => {
const errors = [];
for (const task of tasks) {
try {
await task();
} catch (e) {
errors.push({ task: task.name, error: e.stdout || e });
}
}
return errors;
};

(async () => {
for (const task of tasks) await task();
const errors = await runTasks(tasks);
if (errors.length) {
errors.map(e => {
console.log(codeFence(`ERROR ON TASK: ${e.task}`));
console.log(e.error);
});
throw 'Danger found errors. See stack trace above.';
}
})();
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ services:
restart: unless-stopped

pwa:
hostname: ${PWA_STUDIO_PUBLIC_PATH}
hostname: ${PWA_STUDIO_HOST}
# build pwa using the Dockerfile from the PWD
build:
context: .
dockerfile: Dockerfile
dockerfile: Dockerfile.dev
args:
WEBPACK_HOST: ${PWA_STUDIO_PUBLIC_PATH}
HOST: ${PWA_STUDIO_HOST}
ENVFILEPATH: ${ENVFILEPATH}
# list of directories and files on the host system to volume mount into the container
# changes made to files in the container and on the host file system are mapped to one another
Expand All @@ -42,7 +42,7 @@ services:
- nginx-proxy
environment:
# environment variables consumed by the nginx-proxy service
VIRTUAL_HOST: ${PWA_STUDIO_PUBLIC_PATH}
VIRTUAL_HOST: ${PWA_STUDIO_HOST}
VIRTUAL_PORT: ${PWA_STUDIO_PORTS_DEVELOPMENT}
expose:
- ${PWA_STUDIO_PORTS_DEVELOPMENT}
2 changes: 1 addition & 1 deletion docker/.env.docker → docker/.env.docker.dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
###############################################################################

PWA_STUDIO_PUBLIC_PATH=pwa-docker.localhost
PWA_STUDIO_HOST=pwa-docker.localhost
PWA_STUDIO_PORTS_DEVELOPMENT=8080
ENABLE_SERVICE_WORKER_DEBUGGING=0
PWA_STUDIO_HOT_RELOAD_WITH_POLLING=0
Expand Down
16 changes: 16 additions & 0 deletions docker/.env.docker.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
########## .env.docker ########################################################
#
# See packages/venia-concept/.env.dist file for full option details
#
###############################################################################

NODE_ENV=production
PORT=8080
PWA_STUDIO_HOST=localhost
# magento enterprise edition - in production mode
MAGENTO_BACKEND_URL=https://m231-pwa-ent-1.testsonfire.com/
MAGENTO_BUILDPACK_PROVIDE_SECURE_HOST=0
UPWARD_JS_UPWARD_PATH=venia-upward.yml
UPWARD_JS_BIND_LOCAL=1
UPWARD_JS_LOG_URL=1
BRAINTREE_TOKEN=sandbox_8yrzsvtm_s2bg8fs563crhqzk
2 changes: 1 addition & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ After `docker/run-docker` is executed from the root of the repository, the defau

## Configure a custom domain

The domain is configurable. Just set `PWA_STUDIO_PUBLIC_PATH` key to the new domain under `docker/.env.docker`, or pass a custom .env file with the `PWA_STUDIO_PUBLIC_PATH` key set. All required fields can be found in `docker/.env.docker`. See how to pass the custom .env file below.
The domain is configurable. Just set `PWA_STUDIO_HOST` key to the new domain under `docker/.env.docker`, or pass a custom .env file with the `PWA_STUDIO_HOST` key set. All required fields can be found in `docker/.env.docker`. See how to pass the custom .env file below.

## Pass custom .env file configuration through cli args (optional)

Expand Down
4 changes: 2 additions & 2 deletions docker/run-docker
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
####################################################################################
# Run this file from the root of the repository to build and run a PWA container #
####################################################################################
CONFIG_ENV_FILE=./docker/.env.docker
CONFIG_ENV_FILE=./docker/.env.docker.dev
STATUS=0

# accepts -e <envfile_path> to set a separate environment variable file configuration
Expand Down Expand Up @@ -42,7 +42,7 @@ env_setup () {
echo "ENVFILEPATH=$CONFIG_ENV_FILE" >> $ENVFILE
cat $ENVFILE
. $ENVFILE
DOMAIN=$PWA_STUDIO_PUBLIC_PATH
DOMAIN=$PWA_STUDIO_HOST
}

create_certificate () {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"scripts": {
"build": "yarn workspaces run build",
"build:dev": "yarn workspaces run build:dev",
"ci:pr-checks": "./.bin/pr-checks",
"clean:all": "yarn workspaces run clean && rimraf ./node_modules",
"clean:dist": "yarn workspaces run clean",
"coveralls": "cat ./coverage/lcov.info | coveralls",
Expand All @@ -29,7 +30,6 @@
"postbuild": "rimraf \"./packages/*/{esm,dist}/{,**/}__*__\"",
"prettier": "prettier --write '@(packages|scripts)/**/*.@(js|css)' '*.js'",
"prettier:validate": "prettier-check '@(packages|scripts)/**/*.@(js|css)' '*.js'",
"prettier:check": "prettier --list-different '@(packages|scripts)/**/*.@(js|css)' '*.js'",
"stage:venia": "yarn workspace @magento/venia-concept run start; cd - >/dev/null",
"stats:venia": "yarn workspace @magento/venia-concept run build:analyze && yarn workspace @magento/venia-concept run stats",
"storybook:venia": "yarn workspace @magento/venia-concept run storybook",
Expand All @@ -50,7 +50,7 @@
"chalk": "~2.4.2",
"chokidar": "~2.1.2",
"coveralls": "~3.0.3",
"danger": "~7.0.14",
"danger": "~7.0.17",
"eslint": "~5.14.1",
"eslint-plugin-babel": "~5.3.0",
"eslint-plugin-graphql": "~3.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/pwa-buildpack/src/WebpackTools/PWADevServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const PWADevServer = {
debug('configure() invoked', config);
PWADevServer.validateConfig('.configure(config)', config);
const devServerConfig = {
public: process.env.PWA_STUDIO_PUBLIC_PATH || '',
public: process.env.PWA_STUDIO_HOST || '',
contentBase: false, // UpwardPlugin serves static files
compress: true,
hot: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/venia-concept/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"venia": "./bin/venia.js"
},
"scripts": {
"build": "yarn run clean && yarn run build:esm && yarn run validate-queries && yarn run build:prod",
"build": "yarn run clean && yarn run build:esm && yarn run build:prod",
"build:analyze": "yarn run clean && mkdir dist && yarn run validate-queries && yarn run build:stats",
"build:dev": "echo 'Skipping venia-concept build...'",
"build:esm": "BABEL_ENV=development babel src --out-dir esm --root-mode 'upward' --source-maps --copy-files",
Expand Down
2 changes: 1 addition & 1 deletion pwa-devdocs/_drafts/docker-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

## Configure a custom domain

The domain is configurable. Just set `PWA_STUDIO_PUBLIC_PATH` key to the new domain under `docker/.env.docker`, or pass a custom .env file with the `PWA_STUDIO_PUBLIC_PATH` key set. All required fields can be found in `docker/.env.docker`. See how to pass the custom .env file below.
The domain is configurable. Just set `PWA_STUDIO_HOST` key to the new domain under `docker/.env.docker`, or pass a custom .env file with the `PWA_STUDIO_HOST` key set. All required fields can be found in `docker/.env.docker`. See how to pass the custom .env file below.

## Pass custom .env file configuration through cli args (optional)

Expand Down

0 comments on commit 3853363

Please sign in to comment.