diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index de45de6907bcd88d0226a77dbaca892858d0f07e..f57fb5f6d6c33e5e3a3dfa0ce1b38025bdd1a847 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -62,3 +62,27 @@ build: - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - docker push $CI_REGISTRY_IMAGE:latest +deploy: + stage: deploy + image: alpine + before_script: + # install envsubst and ssh-add + - apk add gettext openssh-client + script: + # create the new docker-compose.yml + - envsubst < docker-compose.tmpl > docker-compose.yml + # start ssh-agent and import ssh private key + - eval `ssh-agent` + - ssh-add <(echo "$SSH_PRIVATE_KEY") + # add server to list of known hosts + - mkdir -p ~/.ssh + - chmod 700 ~/.ssh + - touch ~/.ssh/known_hosts + - chmod 600 ~/.ssh/known_hosts + - echo $SSH_HOST_KEY >> ~/.ssh/known_hosts + # upload docker-compose to the server + - scp docker-compose.yml monitoring@$SERVER:/home/monitoring/ + # docker login and restart services + - ssh monitoring@$SERVER "cd /home/monitoring; + docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY; + docker compose up -d" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f9db6450912615110c3c77d7d79e58c15ca490f6..010f4a2361e73cb7aace539748d090213c8f40e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ #### Step 1 : composer -FROM cylab/php74 AS composer +FROM cylab/php:7.4 AS composer COPY . /var/www/html WORKDIR /var/www/html diff --git a/docker-compose.tmpl b/docker-compose.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..f31b644006ad9fb4f6511cce5af674cc47d1882b --- /dev/null +++ b/docker-compose.tmpl @@ -0,0 +1,41 @@ +# +# docker-compose.tmpl +# https://cylab.be/blog/229/continuous-deployment-with-gitlab-and-docker +# $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA +# + +version: "3.7" + +services: + web: + image: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + depends_on: + - redis + - mysql + ports: + - "80" + volumes: + - ./volumes/web:/var/www/html/storage + restart: "unless-stopped" + environment: + WAIT_HOSTS: mysql:3306 + env_file: env + labels: + - traefik.http.routers.amc.rule=Host(`monitoring.cylab.be`) + - traefik.http.routers.amc.tls=true + - traefik.http.routers.amc.tls.certresolver=letsencrypt + + redis: + image: redis:4-alpine + volumes: + - ./volumes/redis:/data + restart: "unless-stopped" + + mysql: + image: mysql:5.7 + volumes: + - ./volumes/mysql:/var/lib/mysql + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: laravel + restart: "unless-stopped"