diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..ce51329 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,27 @@ +stages: + - build + - deploy + +variables: + PIPELINE_WORK_DIR: "./" + DOCKERFILE_PATH: "./" + API_CFG: "./config/default.json" + CONTAINER_NAME: "enruta-etaapi" + PUBLIC_PORT: 7001 + PRIVATE_PORT: 3000 + +build-job: + stage: build + script: + - . ./scripts/ci_functions.sh + - build_docker $CONTAINER_NAME + only: + - master + +deploy-job: + stage: deploy + script: + - . ./scripts/ci_functions.sh + - deploy + only: + - master diff --git a/Dockerfile b/Dockerfile index 3bfa3f2..302666a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,20 @@ # Use an official Python runtime as a parent image -FROM node:14-alpine +FROM node:18-alpine # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app -COPY sections /app/sections -COPY lib /app/lib COPY config /app/config +COPY lib /app/lib +COPY sections /app/sections COPY index.js /app COPY package.json /app COPY dotenv /app/.env -RUN npm install 2>/dev/null +RUN npm install --include=dev -EXPOSE 8080 +EXPOSE 3000 ENV ROOT_PATH="/app" diff --git a/dotenv b/dotenv index c3fa0d8..646618c 100644 --- a/dotenv +++ b/dotenv @@ -1,4 +1,4 @@ -SERVER_PORT=8080 +SERVER_PORT=3000 ROOT_PATH=/home/josepablocb/Documents/Work/EnRuta/SysS/ETAAPI API_CONFIG=config/apiConfig.json ############################ diff --git a/package.json b/package.json index e47a5d0..b3437ff 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "ETA API", "main": "index.js", "scripts": { - "start": "nodemon index.js", + "start": "node index.js", "dev": "nodemon index.js" }, "repository": { diff --git a/scripts/ci_functions.sh b/scripts/ci_functions.sh new file mode 100755 index 0000000..7e27905 --- /dev/null +++ b/scripts/ci_functions.sh @@ -0,0 +1,45 @@ +#! /bin/bash + +# Requirements +# Docker: Node v18-alpine +function build_docker(){ + if [[ $# -lt 1 ]]; then + echo $0 "[conatiner name]" + return -1 + fi + CONTAINER_NAME=$1 + + set -x + docker rmi -f "$AWS_ECR_REPO/$CONTAINER_NAME" + docker buildx build --no-cache -t $AWS_ECR_REPO/$CONTAINER_NAME ./ + set +x +} + +function deploy_uservice(){ + # CONTAINER NAME + # PUBLIC PORT + # PRIVATE PORT + # ECR REPO + if [[ $# -lt 4 ]]; then + echo "$0 [container_name] [public port] [private port] [ecr_repo]" + return -1 + fi + container_name=$1 + public_port=$2 + private_port=$3 + ecr_repo=$4 + + docker stop $container_name && docker rm $container_name + docker run -p"$public_port:$private_port" -d --restart unless-stopped --name $container_name $ecr_repo/$container_name:latest +} + +function deploy_local(){ + # docker login --username $AWS_ECR_USER --password $DOCKER_PWD $AWS_ECR_REPO + deploy_uservice $CONTAINER_NAME $PUBLIC_PORT $PRIVATE_PORT $AWS_ECR_REPO +} + +function deploy(){ + set -x + deploy_local + set +x +}