From 4d1e8dcf69f48f613418de4ba92e96c3be4c541f Mon Sep 17 00:00:00 2001 From: FyloZ Date: Fri, 12 Feb 2021 10:58:44 -0500 Subject: [PATCH] CI/CD --- .gitlab-ci.yml | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 17 +++++++++++++ ng.Dockerfile | 17 +++++++++++++ nginx.conf | 28 ++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 ng.Dockerfile create mode 100644 nginx.conf diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..01ae339 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,65 @@ +variables: + CI_REGISTRY_IMAGE_NG: "$CI_REGISTRY_IMAGE:latest-ng" + CI_REGISTRY_IMAGE: "$CI_REGISTRY_IMAGE:latest" + +before_script: + - docker info + - docker login -u $CI_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY + +stages: + - build + - package + - deploy + +build: + stage: build + script: + - docker pull $CI_REGISTRY_IMAGE_NG || true + - docker build --cache-from $CI_REGISTRY_IMAGE_NG -f ng.Dockerfile -t $CI_REGISTRY_IMAGE_NG . + - docker push $CI_REGISTRY_IMAGE_NG + +package: + stage: package + needs: ['build'] + variables: + PACKAGE_CONTAINER_NAME: "cre_frontend_package" + ARTIFACT_NAME: "ColorRecipesExplorer-frontend-$CI_PIPELINE_IID" + script: + - apk update + - apk add --no-cache zip + - mkdir dist + - docker run --name $PACKAGE_CONTAINER_NAME $CI_REGISTRY_IMAGE_NG ng build --configuration=$ANGULAR_CONFIGURATION --output-hashing=none --stats-json --source-map=false + - docker cp $PACKAGE_CONTAINER_NAME:/usr/src/cre/dist/color-recipes-explorer-frontend/ dist/ + - zip -r $ARTIFACT_NAME.zip dist/ + - docker build -t $CI_REGISTRY_IMAGE --build-arg ARTIFACT_NAME=$ARTIFACT_NAME . + - docker push $CI_REGISTRY_IMAGE + after_script: + - docker stop $PACKAGE_CONTAINER_NAME || true + - docker rm $PACKAGE_CONTAINER_NAME || true + artifacts: + paths: + - $ARTIFACT_NAME.zip + expire_in: 1 week + +deploy: + stage: deploy + image: alpine:latest + needs: ['package'] + variables: + DEPLOYED_CONTAINER_NAME: "cre_frontend" + before_script: + - apk update + - apk add --no-cache openssh-client + - mkdir -p ~/.ssh + - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa + - chmod 700 ~/.ssh/id_rsa + - eval $(ssh-agent -s) + - ssh-add ~/.ssh/id_rsa + - ssh-keyscan -p $DEPLOYMENT_SERVER_SSH_PORT -H $DEPLOYMENT_SERVER >> ~/.ssh/known_hosts + - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' + script: + - ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker stop $DEPLOYED_CONTAINER_NAME || true && docker rm $DEPLOYED_CONTAINER_NAME || true" + - ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker login -u $CI_USERNAME -p $CI_ACCESS_TOKEN $CI_REGISTRY && docker pull $CI_REGISTRY_IMAGE" + - ssh -p $DEPLOYMENT_SERVER_SSH_PORT $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER "docker run -d -p $PORT:80 --name=$DEPLOYED_CONTAINER_NAME $CI_REGISTRY_IMAGE" + + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f6ea636 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM nginx:mainline-alpine + +WORKDIR /usr/bin/cre/ + +ARG ARTIFACT_NAME=ColorRecipesExplorer-ng +COPY $ARTIFACT_NAME.zip . +COPY frontend.nginx.conf /etc/nginx/nginx.conf + +RUN apk update +RUN apk add --no-cache zip + +RUN unzip $ARTIFACT_NAME.zip +RUN rm $ARTIFACT_NAME.zip + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/ng.Dockerfile b/ng.Dockerfile new file mode 100644 index 0000000..e62e76a --- /dev/null +++ b/ng.Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:latest +WORKDIR /usr/src/cre/ + +RUN apk update +RUN apk add --no-cache nodejs +RUN apk add --no-cache npm + +RUN npm install -g typescript@3.9.7 +RUN npm install -g @angular/cli || true + +ENV NG_CLI_ANALYTICS=ci + +COPY package.json . + +RUN npm install + +COPY . . diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..4cf11fc --- /dev/null +++ b/nginx.conf @@ -0,0 +1,28 @@ +events { + worker_connections 1024; +} + +http { + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + + server { + include /etc/nginx/mime.types; + listen 80; + root /usr/bin/cre/dist/color-recipes-explorer-frontend; + index index.html; + + location / { + try_files $uri$args $uri$args/ /index.html; + } + } + +}