diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..7cc8194a5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +Dockerfile +.dockerignore +docker.sh + +.idea +.vscode +.git +.gitignore +.travis.yml + +*/node_modules +*/_build + +**/npm-*.log +**/yarn-*.log +**/catapult-*.log diff --git a/.travis.yml b/.travis.yml index 2baeae191..940297919 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,8 @@ language: node_js node_js: - "12" - +services: + - docker addons: apt: sources: @@ -29,4 +30,17 @@ before_script: - sudo systemctl start mongod - sh yarn_setup.sh -script: cd ${SUBPROJECT} && yarn run lint && yarn run test:travis +script: cd ${SUBPROJECT} && yarn run lint && yarn run test:travis && cd .. + +jobs: + include: + - stage: docker alpha deploy + env: + - SUBPROJECT=rest + script: /bin/sh docker.sh + if: branch = master AND type != pull_request + - stage: docker release deploy + env: + - SUBPROJECT=rest + script: /bin/sh docker.sh --release + if: tag IS present diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..d941eb6a5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM node:12.18.1-alpine +RUN apk add --update\ + python \ + python3 \ + build-base \ + zeromq-dev \ + && rm -rf /var/cache/apk/* +WORKDIR /app +COPY . /app/catapult-rest +RUN cd catapult-rest \ + && ./yarn_setup.sh +RUN cd catapult-rest/rest \ + && yarn build +WORKDIR /app/catapult-rest/rest diff --git a/docker.sh b/docker.sh new file mode 100644 index 000000000..c3e8b2b99 --- /dev/null +++ b/docker.sh @@ -0,0 +1,44 @@ +#!/bin/sh + +if [ "${DOCKER_IMAGE_NAME}" = "" ] +then + echo "Docker deployment error. Env DOCKER_IMAGE_NAME has not been provided" + exit 128 +fi + +if [ "${DOCKER_USERNAME}" = "" ] +then + echo "Docker deployment error. Env DOCKER_USERNAME has not been provided" + exit 128 +fi + +if [ "${DOCKER_PASSWORD}" = "" ] +then + echo "Docker deployment error. Env DOCKER_PASSWORD has not been provided" + exit 128 +fi + +echo "Login into docker..." +echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + +cd rest +CURRENT_VERSION=$(npm run version --silent) +cd .. + +if [ "$1" = "--release" ] +then + VERSION="${CURRENT_VERSION}" +else + VERSION="${CURRENT_VERSION}-alpha" +fi + +echo "Creating image ${DOCKER_IMAGE_NAME}:${VERSION}" +docker build -t "${DOCKER_IMAGE_NAME}:${VERSION}" . +docker images + +# Quick test, the expected error is ENOENT: no such file or directory, open '../resources/rest.json' +echo "Testing image ${DOCKER_IMAGE_NAME}:${VERSION}. NOTE: ENOENT: no such file or directory, open '../resources/rest.json is expected" +docker run --rm "${DOCKER_IMAGE_NAME}:${VERSION}" npm start + +echo "Pushing image ${DOCKER_IMAGE_NAME}:${VERSION}" +docker push "${DOCKER_IMAGE_NAME}:${VERSION}" diff --git a/rest/package.json b/rest/package.json index 6e95483c4..9307861ba 100644 --- a/rest/package.json +++ b/rest/package.json @@ -7,6 +7,7 @@ "clean": "rimraf _build && mkdir _build", "build": "ncp src/ _build", "rebuild": "npm run clean && npm run build", + "version": "echo $npm_package_version", "start": "node _build/index.js", "start:debug": "node src/index.js", "test": "mocha --full-trace --recursive",