From e24b0f366084c66ec25be9be8cf20179132cbcab Mon Sep 17 00:00:00 2001 From: Alexander Detsch Date: Sun, 4 Apr 2021 15:35:36 +0200 Subject: [PATCH] added ci/cd --- .dockerignore | 2 ++ .gitlab-ci.yml | 53 +++++++++++++++++++++++++++++++++++++++ Dockerfile | 12 +++++++++ deployment/deployment.yml | 22 ++++++++++++++++ deployment/hosts.yml | 10 ++++++++ docker-compose.yml | 12 +++++++++ 6 files changed, 111 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 deployment/deployment.yml create mode 100644 deployment/hosts.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6be8c8f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +.dockerignore diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..b4c3ab9 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,53 @@ +stages: + - build + - deploy + +variables: + PY_COLORS: '1' + ANSIBLE_FORCE_COLOR: '1' + ANSIBLE_PYTHON_INTERPRETER: auto_silent + ANSIBLE_STDOUT_CALLBACK: debug + +docker-build-master: + # Official docker image. + image: docker:latest + stage: build + tags: + - docker + services: + - docker:dind + before_script: + - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY + script: + - docker build --pull -t "$CI_REGISTRY_IMAGE" . + - docker push "$CI_REGISTRY_IMAGE" + only: + - master + +docker-build: + # Official docker image. + image: docker:latest + stage: build + tags: + - docker + services: + - docker:dind + before_script: + - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY + script: + - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" . + - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" + except: + - main + +deploy-master: + #when: manual + stage: deploy + image: + name: cytopia/ansible:latest-tools + entrypoint: [""] + script: + - chmod 600 "${ANSIBLE_SSH_KEY_PRIVATE}" + - ansible-playbook -i ./deployment/hosts.yml --private-key "${ANSIBLE_SSH_KEY_PRIVATE}" "./deployment/deployment.yml" + only: + - main diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c1e875b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM node:lts-alpine as build-stage +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +RUN npm run build + +# production stage +FROM nginx:stable-alpine as production-stage +COPY --from=build-stage /app/dist /usr/share/nginx/html +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/deployment/deployment.yml b/deployment/deployment.yml new file mode 100644 index 0000000..a3f5eeb --- /dev/null +++ b/deployment/deployment.yml @@ -0,0 +1,22 @@ +--- +- hosts: snickers-docker + gather_facts: no + vars: + verbose: false + compose_folder: "/mnt/docker-storage/radiator-web/" + + tasks: + - name: Ensures {{ compose_folder }} dir exists + stat: + path: "{{ compose_folder }}docker-compose.yml" + register: file_details + + - name: Fail if docker-compose file doesn't exist + fail: + msg: "docker-compose.yml doesn't exist in expected location (expected in {{ compose_folder }})" + when: not file_details.stat.exists + + - name: Update container image + community.general.docker_compose: + project_src: "{{ compose_folder }}" + pull: yes diff --git a/deployment/hosts.yml b/deployment/hosts.yml new file mode 100644 index 0000000..45c6cea --- /dev/null +++ b/deployment/hosts.yml @@ -0,0 +1,10 @@ +all: + vars: + ansible_python_interpreter: /usr/bin/python3 + hosts: + snickers-docker: + ansible_host: 192.168.200.11 + ansible_port: 22 + ansible_user: root + ansible_ssh_extra_args: '-o StrictHostKeyChecking=no' + ansible_ssh_common_args: '-o ProxyCommand="ssh -o StrictHostKeyChecking=no -i "${ANSIBLE_SSH_KEY_PRIVATE}" -W %h:%p root@vserver8.alex-detsch.de -p 52"' diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..83d560f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +web: + image: registry.alex-detsch.de/fem/radiator/radiator-web + restart: always + healthcheck: + test: "curl localhost" + interval: "60s" + timeout: "3s" + start_period: "5s" + retries: 3 + stop_grace_period: 3s + ports: + - "1499:80"