Skip to content

Feature/production ready #440

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

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ jobs:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4
- name: Build the Docker image
run: make compose/rebuild
- name: Run test in Docker image
- name: Lint in Docker image
run: make compose/lint
- name: Test in Docker image
run: make compose/test
- name: Run in Docker image
run: make compose/run
- name: Tag Docker image
run: >
Expand Down
29 changes: 18 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ RUN ls -alh
# CMD []
WORKDIR ${WORKDIR}
###############################################################################
FROM development AS builder
FROM base AS builder

ENV WORKDIR=/app
WORKDIR ${WORKDIR}

RUN make clean && npm ci --verbose --omit-dev
COPY ./src ${WORKDIR}/src
COPY ./package.json ${WORKDIR}/package.json
COPY ./package-lock.json ${WORKDIR}/package-lock.json
COPY ./Makefile ${WORKDIR}/

# CMD []
RUN rm -vfr node_modules && npm ci --verbose --omit=dev
RUN npm run build

CMD ["ls", "-alh"]

###############################################################################
### In testing stage, can't use USER, due permissions issue
Expand All @@ -100,21 +106,22 @@ CMD ["make", "test"]
## in the production phase, "good practices" such as
## WORKSPACE and USER are maintained
##
FROM builder AS production
FROM base AS production

ENV LOG_LEVEL=info
ENV BRUTEFORCE=false
ENV WORKDIR=/app
WORKDIR ${WORKDIR}

# TODO find a way to exclude /src/ and exclude *.test.js files
COPY --from=builder /app/dist ${WORKDIR}/dist
COPY --from=builder /app/node_modules ${WORKDIR}/node_modules

COPY ./Makefile ${WORKDIR}/
COPY ./package.json ${WORKDIR}/package.json
COPY ./package-lock.json ${WORKDIR}/package-lock.json
COPY ./Makefile ${WORKDIR}/

COPY ./.babelrc /app/.babelrc
COPY ./.eslintrc /app/.eslintrc
COPY ./.prettierrc /app/.prettierrc
COPY ./jest.config.js /app/jest.config.js
COPY --from=builder /app/node_modules /app/node_modules
RUN ls -alh

USER node
CMD ["npm", "run", "test"]
CMD ["ls", "-alh"]
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ env:

clean:
${NPM} run jest:clean
rm -vfr ./build
rm -vfr ./node_modules
rm -vfr ./coverage
mkdir -p ./coverage
touch ./coverage/.gitkeep

build: dependencies
${NPM} run build

dependencies:
@echo "################################################################################"
@echo "## Dependencies: ###############################################################"
Expand Down Expand Up @@ -93,10 +97,12 @@ upgrade: update
compose/build: env
docker-compose --profile lint build
docker-compose --profile testing build
docker-compose --profile production build

compose/rebuild: env
docker-compose --profile lint build --no-cache
docker-compose --profile testing build --no-cache
docker-compose --profile production build --no-cache

compose/lint/markdown: compose/build
docker-compose --profile lint build
Expand All @@ -113,7 +119,13 @@ compose/test/static: compose/build

compose/lint: compose/lint/markdown compose/lint/yaml compose/test/styling compose/test/static

compose/test: compose/build
docker-compose --profile testing run --rm algorithm-exercises-js-test make test

compose/run: compose/build
docker-compose --profile testing run --rm algorithm-exercises-js make test
docker-compose --profile production run --rm algorithm-exercises-js make run

all: env dependencies test

run:
ls -alh
12 changes: 6 additions & 6 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---

services:
algorithm-exercises-js:
image: algorithm-exercises-js:latest
algorithm-exercises-js-test:
image: algorithm-exercises-js:test
build:
context: .
target: testing
Expand Down Expand Up @@ -37,13 +37,13 @@ services:
- ./:/app
profiles: ["development"]

algorithm-exercises-js-prod:
image: algorithm-exercises-js:prod
algorithm-exercises-js:
image: algorithm-exercises-js:latest
build:
context: .
target: production
volumes:
- ./:/app
# volumes:
# - ./:/app
profiles: ["production"]

## REFERENCES:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"name": "projecteuler.net",
"name": "algorithm-exercises-js",
"version": "1.0.0",
"description": "Solutions to the first 100 problems from [Project Euler](https://projecteuler.net/)",
"main": "src/problem0000-template.js",
"description": "Algorithm exercises solved in Javascript ECMA6, running with Jest testing suite. Developed with TDD.",
"main": "src/problem0000.js",
"scripts": {
"build": "npx --yes copyfiles --verbose -R --up 1 -e \"**/*.test.js\" -e \"**/*.config.js\" \"**/*.js\" dist/",
"start": "npm run test",
"lint": "npx eslint . --color --max-warnings=0 && echo '✔ Your code looks good.'",
"lint:watch": "npx esw . --color --max-warnings=0 --watch",
"lint": "npx --yes eslint . --color --max-warnings=0 && echo '✔ Your code looks good.'",
"lint:watch": "npx --yes esw . --color --max-warnings=0 --watch",
"jest:ci": "node --experimental-vm-modules ./node_modules/.bin/jest --no-cache --ci --color --detectOpenHandles --forceExit --runInBand --debug",
"jest:clean": "jest --clearCache && watchman watch-del-all",
"jest:clean": "npx --yes jest --clearCache && npx --yes watchman watch-del-all",
"jest:bruteforce": "BRUTEFORCE=true node --experimental-vm-modules ./node_modules/.bin/jest --no-cache --ci --color --detectOpenHandles --forceExit --runInBand --debug",
"jest:watch": "node --experimental-vm-modules ./node_modules/.bin/jest --no-cache --ci --color --detectOpenHandles --forceExit --runInBand --debug --watchAll",
"style:format": "npx prettier --write 'src/**/*.js'",
"style:check": "npx prettier --check 'src/**/*.js'",
"test": "jest",
"style:format": "npx --yes prettier --write 'src/**/*.js'",
"style:check": "npx --yes prettier --check 'src/**/*.js'",
"test": "npm run jest:ci",
"test:all": "npm run lint && npm run jest && echo 'Done.'",
"test:watch": "concurrently -k -s first --names \"LINT,TEST\" -p \"[{name}]\" \"npm run lint:watch\" \"npm run jest:watch\"",
"test:watch": "npx --yes concurrently -k -s first --names \"LINT,TEST\" -p \"[{name}]\" \"npm run lint:watch\" \"npm run jest:watch\"",
"update-all": "npm install $(npm outdated | cut -d' ' -f 1 | sed '1d' | xargs -I '$' echo '$@latest' | xargs echo)"
},
"author": "Gonzalo Diaz <[email protected]>",
Expand Down