-
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor docker image and CI pipeline #1624
base: master
Are you sure you want to change the base?
Changes from all commits
d267051
da19113
9e00685
d636bfb
bf5ca74
5fbc461
b62b9d3
92327e9
cfeb1bf
d37a9f3
528a1c0
2bddf77
d7f1a04
2700269
5e4c63c
efff286
c39a7ab
1ecd506
36c7690
bba12ac
d1a1f7e
de384b6
14092a1
90788bf
145f284
548a9e9
57f5775
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
data/* | ||
!data/scores.json | ||
**/.git | ||
**/.github | ||
README.md | ||
data | ||
built | ||
doc | ||
node_modules | ||
frontend/lib | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed Added ignores for |
||
Makefile | ||
|
||
# Vim swap files | ||
**/.*.sw[po] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,55 +13,28 @@ on: | |
- '**.md' | ||
|
||
jobs: | ||
test: | ||
name: Run tests | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node_version: | ||
- 14 | ||
- 16 | ||
verify: | ||
name: Verify | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup | ||
uses: actions/setup-node@v2.5.1 | ||
with: | ||
node-version: ${{matrix.node_version}} | ||
fetch-depth: 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting |
||
|
||
# See https://github.com/npm/cli/issues/2610 | ||
- name: Workaround for Node 14 | ||
if: matrix.node_version == '14' | ||
run: sed -i 's/git+ssh/git+https/g' package-lock.json | ||
- name: Build | ||
run: make docker DOCKER_CACHE_OFF=1 | ||
|
||
- name: Install | ||
run: npm ci | ||
- name: Lint | ||
run: make docker-lint | ||
|
||
- name: Test | ||
run: npm test | ||
|
||
|
||
docker: | ||
name: Test docker image | ||
|
||
runs-on: ubuntu-latest | ||
|
||
continue-on-error: true | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Build | ||
run: docker build --tag dr4ft-app . | ||
run: make docker-test | ||
|
||
- name: Run | ||
run: docker run -dp 1337:1337 dr4ft-app | ||
- name: Run docker image | ||
run: make docker-run | ||
|
||
- name: Show info | ||
run: | | ||
|
@@ -70,24 +43,5 @@ jobs: | |
docker ps -a | ||
echo | ||
docker images | ||
|
||
|
||
lint: | ||
name: Run ESLint | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup | ||
uses: actions/setup-node@v2.5.1 | ||
with: | ||
node-version: 16 | ||
|
||
- name: Install | ||
run: npm ci --ignore-scripts | ||
|
||
- name: Run ESLint | ||
run: npm run lint | ||
echo | ||
make docker-logs FOLLOW= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
FROM node:lts-alpine | ||
ENV NPM_CONFIG_LOGLEVEL warn | ||
|
||
# Install "git" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Installing |
||
RUN apk update \ | ||
&& apk add alpine-sdk | ||
ARG VERSION_INFO=noVersion | ||
|
||
ENV NPM_CONFIG_LOGLEVEL warn | ||
ENV PORT=1337 | ||
|
||
# Set working dir as /app | ||
WORKDIR /app | ||
|
||
# Add sources to /app | ||
COPY . . | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running |
||
RUN adduser -S dr4ftuser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Node images already include a non-root user called |
||
RUN chown dr4ftuser -R . | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running |
||
USER dr4ftuser | ||
|
||
# Install the dependencies | ||
RUN npm ci | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running Now, the dependencies and card info are added to the image before most of this code. You can test the difference this makes by running |
||
COPY --chown=node package.json . | ||
COPY --chown=node package-lock.json . | ||
RUN npm ci --ignore-scripts | ||
|
||
# Update card database | ||
COPY --chown=node backend/core backend/core | ||
COPY --chown=node config/ config/ | ||
COPY --chown=node scripts/ scripts/ | ||
RUN npm run download_allsets \ | ||
&& npm run download_booster_rules \ | ||
&& chown node -R data/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including this |
||
|
||
# Add sources to /app | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to order these in such a way that more frequently-changing files would be copied in later layers so the layer cache invalidation would only apply to lower layers. Open to suggestions. Also, I think the |
||
COPY --chown=node LICENSE . | ||
COPY --chown=node .gitignore . | ||
COPY --chown=node .babelrc . | ||
COPY --chown=node .eslintrc.js . | ||
COPY --chown=node .mocharc.yaml . | ||
COPY --chown=node webpack.prod.js . | ||
COPY --chown=node webpack.common.js . | ||
COPY --chown=node webpack.dev.js . | ||
COPY --chown=node app.json . | ||
COPY --chown=node app.js . | ||
COPY --chown=node backend/ backend/ | ||
COPY --chown=node frontend/ frontend/ | ||
|
||
# Publish the port 1337 | ||
EXPOSE 1337 | ||
RUN npm run webpack | ||
|
||
ENV VERSION_INFO=$VERSION_INFO | ||
# Run the server | ||
ENTRYPOINT [ "npm", "start" ] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
VERSION_INFO ?= $$(git describe --tags) | ||
IMAGE ?= dr4ft-app | ||
CONTAINER ?= $(IMAGE)-container | ||
PORT ?= 1337 | ||
|
||
|
||
# Show makefile help by default | ||
.DEFAULT_GOAL = help | ||
|
||
|
||
CACHE_OFF=0 | ||
ifeq ($(CACHE_OFF), 1) | ||
NO_CACHE_FLAG = --no-cache | ||
else | ||
NO_CACHE_FLAG = | ||
endif | ||
.PHONY: docker | ||
docker: ## Build docker image | ||
@echo "Building with version info $(VERSION_INFO)" | ||
docker build $(NO_CACHE_FLAG) \ | ||
--build-arg VERSION_INFO=$(VERSION_INFO) \ | ||
-t $(IMAGE) . | ||
|
||
|
||
.PHONY: docker-run | ||
docker-run: docker-stop docker ## Run app in docker container | ||
docker run -d \ | ||
--name $(CONTAINER) \ | ||
--env "PORT=$(PORT)" \ | ||
-p $(PORT):$(PORT) \ | ||
$(IMAGE) | ||
@echo "##########################################" | ||
@echo "Dr4ft now running at http://localhost:$(PORT)" | ||
@echo "##########################################" | ||
|
||
|
||
.PHONY: docker-stop | ||
docker-stop: ## Stop running docker container | ||
docker stop $(CONTAINER) > /dev/null 2>&1 || true | ||
docker container rm $(CONTAINER) > /dev/null 2>&1 || true | ||
|
||
|
||
.PHONY: docker-logs | ||
FOLLOW := -f | ||
docker-logs: ## Show logs from running docker container | ||
docker logs $(FOLLOW) $(CONTAINER) | ||
|
||
|
||
.PHONY: docker-test | ||
docker-test: docker ## Run tests in docker container | ||
docker run --rm \ | ||
--entrypoint npm \ | ||
$(IMAGE) run test:js | ||
|
||
|
||
.PHONY: docker-lint | ||
docker-lint: docker ## Lint code in docker container | ||
docker run --rm \ | ||
--entrypoint npm \ | ||
$(IMAGE) run lint | ||
|
||
|
||
.PHONY: docker-clean | ||
docker-clean: docker-stop ## Remove any built docker images | ||
docker rmi -f $$(docker images -q $(IMAGE)) > /dev/null 2>&1 || true | ||
|
||
|
||
# Output help string for each target. | ||
# With thanks to: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html | ||
.PHONY: help | ||
help: ## Show this help | ||
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, the
.git
folder was added to the docker image for the sake of building in a version number, namely a commit hash to display in the footer. Now that version number is built in as an environment variable. The other ignores here leave out files that are irrelevant to the docker image build. Any time these files changed required the entire image to be rebuilt.