diff --git a/README.md b/README.md index 3fec5bdf296e595d767066fc5c0c92fc32b52f21..0f3a2518b45854b5a6fc592543f089c65be055a0 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ In your own project make sure the following files are present in the root of you 1. Docker file that has all the necessary information to run your code 2. **Example Dockerfile** for a standard python project with a ```main.py``` and some libraries in a ```requirements.txt``` added as a template here 2. docker-compose.yaml - 1. docker-compose file with at least your own service and a network and the location of the environment file + 1. docker-compose file with at least your own service, a network for internal docker container communication and the location of the environment file 2. **Example docker-compose** for a single service added as a template here 3. gitlab-ci.yml 1. the gitlab-ci.yml template that is in this project @@ -18,14 +18,19 @@ In your own project make sure the following files are present in the root of you ### On gitlab To make sure variables and the build+deploy job are not dependent, add the following variables to gitlab - -1. Go to the CI/CD variables section (for example: https://git.astron.nl/templates/docker-deploy/-/settings/ci_cd -> variables) -2. Expand the section and add the following variables **as a variable**: +1. Create a new gitlab environment here: https://git.astron.nl/grafana/docker-deploy/-/environments/new +2. Go to the CI/CD variables section (for example: https://git.astron.nl/templates/docker-deploy/-/settings/ci_cd -> variables) +3. Expand the section and add the following variables **as a variable** and don't forget to set your environment!: 1. DEPLOY_HOST 1. Where the docker service(s) should run. 2. SERVICE_DIR 1. Where the static files are stored like the docker-compose.yaml. -3. Expand the section and add the variables that are used by your docker service **as a file** called DOT_ENV + 3. DEPLOY_USER: The user that has read, write, and execute permissions for your `SERVICE_DIR` + 1. The private key `SSH_PRIVATE_KEY` for an ssh connection to your `DEPLOY_HOST`. + 2. The known hosts file `KNOWN_HOSTS` which ensures it deploys to the correct machines. + + _Note: it's best to inherit the private key and known hosts file from the group settings or change this setup with [deploy tokens](https://git.astron.nl/help/user/project/deploy_tokens/index.md)_ +4. Add the environment variables that are used by your docker service **as a file** called `DOT_ENV` and don't forget to set your environment!: - + diff --git a/docker-compose.yml b/docker-compose.yml index 575100b5a566470dc7293f3136136add9742d686..88882e2c8c6a12a52445850131846c00688874a0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,6 @@ services: container_name: my_service image: git.astron.nl:5000/location/my_service:latest networks: - - adex_network - volumes: - - $HOME/shared:/shared + - my_network_name env_file: - - /docker_compose/my_service/.env + - /docker_compose/my_service/.env \ No newline at end of file diff --git a/gitlab-vars.png b/gitlab-vars.png new file mode 100644 index 0000000000000000000000000000000000000000..5202e9ab43b3ff53ffc6817faea887971299ee6c Binary files /dev/null and b/gitlab-vars.png differ diff --git a/gitlab.vars.png b/gitlab.vars.png deleted file mode 100644 index 418ed2ffad6bfe689f76d8a683c8d9b7293ad554..0000000000000000000000000000000000000000 Binary files a/gitlab.vars.png and /dev/null differ diff --git a/template.gitlab-ci.yml b/template.gitlab-ci.yml index 90a6c228a3e0db79846b3f8997ff0cf2debfc6c9..a9b09268ff7993326bec5920b74700f978ad11b7 100644 --- a/template.gitlab-ci.yml +++ b/template.gitlab-ci.yml @@ -24,7 +24,7 @@ docker-build: - docker build --pull -t $CI_REGISTRY_IMAGE$DOCKER_IMAGE_TAG . - docker push $CI_REGISTRY_IMAGE$DOCKER_IMAGE_TAG -deploy-job: +.base-deploy-job: image: ubuntu:latest stage: deploy before_script: @@ -37,17 +37,27 @@ deploy-job: - | cat > ~/.ssh/config << EOF Host * - User gitlab-deploy + User $DEPLOY_USER StrictHostKeyChecking no EOF + - cp $KNOWN_HOSTS ~/.ssh/known_hosts script: - - rsync -avz docker-compose.yaml $DEPLOY_HOST:$PATH_TO_SERVICE + - rsync -avz docker-compose.yml $DEPLOY_HOST:$SERVICE_DIR - rsync -avz $DOT_ENV $DEPLOY_HOST:$SERVICE_DIR/.env - | ssh $DEPLOY_HOST -C \ "cd $SERVICE_DIR &&\ chmod 600 $SERVICE_DIR/.env &&\ docker pull $CI_REGISTRY_IMAGE${DOCKER_IMAGE_TAG} &&\ - docker-compose up -d" + docker-compose up -d --force-recreate" - echo "Application successfully deployed." - when: manual \ No newline at end of file + when: manual + +deploy-to-your-host: + extends: .base-deploy-job + environment: your-host + variables: + DEPLOY_USER: $DEPLOY_USER + DEPLOY_HOST: $DEPLOY_HOST + SERVICE_DIR: $SERVICE_DIR + DOT_ENV: $DOT_ENV \ No newline at end of file