From ed9a4536971ab2cd62c5498940041bdb692dc77a Mon Sep 17 00:00:00 2001 From: Timo Beller Date: Tue, 26 Jul 2022 13:43:20 +0200 Subject: [PATCH 1/2] docs(typos): Fix ein paar Typos --- .../templates/IaC/docker-compose.adoc | 30 +++++++++---------- templates/IaC/docker-compose.gitlab-ci.yml | 10 +++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/documentation/chapters/templates/IaC/docker-compose.adoc b/documentation/chapters/templates/IaC/docker-compose.adoc index 3b041e4..3e43e92 100644 --- a/documentation/chapters/templates/IaC/docker-compose.adoc +++ b/documentation/chapters/templates/IaC/docker-compose.adoc @@ -7,13 +7,13 @@ |.docker-compose-validate | Validates the syntactic correctness of your compose files. Still can have execution Problems. |=== -Copy files with scp onto from your Repo to the Remote system. -Then executes Docker-compose over a SSH connection. -For this SSH connection you have to Provide a SSH Key under a CI-CD-Variable with the name $SSH_PRIVATE_KEY. -If you already have a different $SSH_PRIVATE_KEY you can overwrite the Variable SSH_PRIVATE_KEY_DEPLOY. +Copy files with scp from your repository onto the remote system. +Then executes `docker compose` over a SSH connection. +For this SSH connection you have to provide an SSH key under a CI-CD-Variable with the name $SSH_PRIVATE_KEY. +If you already have a different $SSH_PRIVATE_KEY you can overwrite the variable SSH_PRIVATE_KEY_DEPLOY. -To Parametrise the Docker-Compose File you can use a .env File. -The .env File should not contain any Secrets. +To parametrise the docker-compose file you can use a .env file. +The .env file should not contain any secrets. For this reason you can create a .env file dynamically. To generate a .env file you can provide a script under the path ENV_SETUP_FILE. @@ -24,7 +24,7 @@ To generate a .env file you can provide a script under the path ENV_SETUP_FILE. #Prints all Enviroments into the .env File. printenv >> .env -#to set a diffrent Project Prefix use this ENV +#to set a different project prefix use this ENV echo COMPOSE_PROJECT_NAME=project-prefix >>.env #Secrets @@ -34,7 +34,7 @@ echo SOME_PASSWORD="$SOME_PASSWORD" >>.env == SSH Problem workaround -Sometimes there is a problem with some SSH servers and Docker. +Sometimes there is a problem with some SSH servers and docker. We have a workaround for this, please override your script with the following: ---- @@ -59,11 +59,11 @@ We have a workaround for this, please override your script with the following: | 22 | SSH_USER -| The user that is used to login into the remote maschine. +| The user that is used to log into the remote maschine. | - | DOCKER_REGISTRY -| The Container Registry that will be used to pull images. +| The container registry that will be used to pull images. | "$CI_REGISTRY" | DOCKER_REGISTRY_PASS @@ -75,8 +75,8 @@ We have a workaround for this, please override your script with the following: | "$CI_REGISTRY_USER" | ENV_SETUP_FILE -| If you want to use a .env file, this skript can be used to create one. -Secrets should not be stored within your repository. Use the CI-CD Variables in the setting page. +| If you want to use a .env file, this script can be used to create one. +Secrets should not be stored within your repository. Use the CI-CD variables in the gitlab setting page. | "setup_env.sh" | SCP_DESTINATION @@ -84,16 +84,16 @@ Secrets should not be stored within your repository. Use the CI-CD Variables in | - | SCP_SOURCE -| Path you want to copy to the remote System. +| Path you want to copy to the remote system. | $CI_PROJECT_DIR | COMPOSE_EXTRA_ARGS -| Additional arguments you want to send to Docker Compose. +| Additional arguments you want to send to docker compose. For example multiple `-f compose.file.extra` definitions. | - | COMPOSE_UP_EXTRA_ARGS -| Additional arguments you want to send to Docker Compose up. +| Additional arguments you want to send to docker compose up. For example multiple `--no-deps --no-build` definitions. | - |=== diff --git a/templates/IaC/docker-compose.gitlab-ci.yml b/templates/IaC/docker-compose.gitlab-ci.yml index 75e96ee..17f093b 100644 --- a/templates/IaC/docker-compose.gitlab-ci.yml +++ b/templates/IaC/docker-compose.gitlab-ci.yml @@ -27,7 +27,7 @@ - chmod 700 ~/.ssh # Add keys to known hosts for Docker Compose - ssh-keyscan $SSH_URL >> ~/.ssh/known_hosts - #Install docker Compose, from release directly + # Install docker-compose, from release directly - wget https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -O /usr/local/bin/docker-compose - chmod +x /usr/local/bin/docker-compose script: @@ -41,7 +41,7 @@ script: - export DOCKER_HOST=unix:///tmp/docker.sock - if [[ -n "$SCP_SOURCE" && -n "$SCP_DESTINATION" ]]; then scp -rp $SCP_SOURCE $SSH_USER@$SSH_URL:$SCP_DESTINATION; fi - # Tunnel Docker socket from remote Maschine to local Maschine. This should be more stable than the current SSH Implementation of Docker + # Tunnel Docker socket from remote machine to local machine. This should be more stable than the current SSH implementation of Docker - rm -f /tmp/docker.sock - ssh -M -S docker-ctrl-socket -fnNT -L /tmp/docker.sock:/var/run/docker.sock "$SSH_USER@$SSH_URL" -p $SSH_PORT -o ControlPersist=no - if [[ -n "$DOCKER_REGISTRY" ]]; then echo "$DOCKER_REGISTRY_PASS" | docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin; fi @@ -52,10 +52,10 @@ image: docker:20.10.16-alpine3.15 stage: verify variables: - DOCKER_COMPOSE_VERSION: 2.7.0 + DOCKER_COMPOSE_VERSION: "v2.7.0" before_script: - #Install docker Compose, from release directly + # Install docker-compose, from release directly - wget https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -O /usr/local/bin/docker-compose - chmod +x /usr/local/bin/docker-compose script: - - docker-compose $COMPOSE_EXTRA_ARGS config + - docker-compose $COMPOSE_EXTRA_ARGS config --quiet From 98abaa2370debb60aa1aaef29ce0fb0956b810a6 Mon Sep 17 00:00:00 2001 From: Timo Beller Date: Tue, 26 Jul 2022 13:51:17 +0200 Subject: [PATCH 2/2] =?UTF-8?q?feat(docker-compose):=20Nutze=20Cache=20f?= =?UTF-8?q?=C3=BCr=20docker=20compose?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/IaC/docker-compose.adoc | 32 ++++++++++------- templates/IaC/docker-compose.gitlab-ci.yml | 34 ++++++++++++++----- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/documentation/chapters/templates/IaC/docker-compose.adoc b/documentation/chapters/templates/IaC/docker-compose.adoc index 3e43e92..beeac26 100644 --- a/documentation/chapters/templates/IaC/docker-compose.adoc +++ b/documentation/chapters/templates/IaC/docker-compose.adoc @@ -3,12 +3,12 @@ .Provided Templates: |=== |Name |Description -|.docker-compose-deploy | Executes Docker Compose against a remote maschine. -|.docker-compose-validate | Validates the syntactic correctness of your compose files. Still can have execution Problems. +|.docker-compose-deploy | Executes docker compose against a remote machine. +|.docker-compose-validate | Validates the syntactic correctness of your compose files. Still can have execution problems. |=== Copy files with scp from your repository onto the remote system. -Then executes `docker compose` over a SSH connection. +Then executes `docker compose` over an SSH connection. For this SSH connection you have to provide an SSH key under a CI-CD-Variable with the name $SSH_PRIVATE_KEY. If you already have a different $SSH_PRIVATE_KEY you can overwrite the variable SSH_PRIVATE_KEY_DEPLOY. @@ -17,18 +17,18 @@ The .env file should not contain any secrets. For this reason you can create a .env file dynamically. To generate a .env file you can provide a script under the path ENV_SETUP_FILE. -.Beispiel für ein setup_env.sh +.Example for a setup_env.sh ---- #!/bin/sh -#Prints all Enviroments into the .env File. +# Prints all enviroments into the .env file. printenv >> .env -#to set a different project prefix use this ENV +# to set a different project prefix use this ENV echo COMPOSE_PROJECT_NAME=project-prefix >>.env -#Secrets -#Some Secret is a Variable within the gitlab CI Variable +# Secrets +# $SOME_PASSWORD is a variable within the gitlab CI variable echo SOME_PASSWORD="$SOME_PASSWORD" >>.env ---- @@ -64,20 +64,20 @@ We have a workaround for this, please override your script with the following: | DOCKER_REGISTRY | The container registry that will be used to pull images. -| "$CI_REGISTRY" +| $CI_REGISTRY | DOCKER_REGISTRY_PASS | The passwort to the docker registry that should be used. -| "$CI_REGISTRY_PASSWORD" +| $CI_REGISTRY_PASSWORD | DOCKER_REGISTRY_USER | The user to the docker registry that should be used. -| "$CI_REGISTRY_USER" +| $CI_REGISTRY_USER | ENV_SETUP_FILE | If you want to use a .env file, this script can be used to create one. Secrets should not be stored within your repository. Use the CI-CD variables in the gitlab setting page. -| "setup_env.sh" +| setup_env.sh | SCP_DESTINATION | Target path for scp to push files to. @@ -96,6 +96,14 @@ For example multiple `-f compose.file.extra` definitions. | Additional arguments you want to send to docker compose up. For example multiple `--no-deps --no-build` definitions. | - + +| DOCKER_COMPOSE_VERSION +| Version of `docker compose`. +| v2.7.0 + +| DOCKER_COMPOSE +| Local path to `docker compose`. If this file does not exist, the script will download it from github. +| $CI_PROJECT_DIR/docker-compose |=== :template: IaC/docker-compose.gitlab-ci.yml diff --git a/templates/IaC/docker-compose.gitlab-ci.yml b/templates/IaC/docker-compose.gitlab-ci.yml index 17f093b..38cda07 100644 --- a/templates/IaC/docker-compose.gitlab-ci.yml +++ b/templates/IaC/docker-compose.gitlab-ci.yml @@ -3,6 +3,7 @@ stage: deploy variables: DOCKER_COMPOSE_VERSION: "v2.7.0" + DOCKER_COMPOSE: "$CI_PROJECT_DIR/docker-compose" SCP_DESTINATION: "" SCP_SOURCE: $CI_PROJECT_DIR COMPOSE_EXTRA_ARGS: "" @@ -17,6 +18,10 @@ SSH_PRIVATE_KEY_DEPLOY: $SSH_PRIVATE_KEY rules: - when: manual + cache: + key: "docker-compose-$DOCKER_COMPOSE_VERSION" + paths: + - $CI_PROJECT_DIR/docker-compose # https://docs.gitlab.com/ee/ci/ssh_keys/ before_script: - if [ -f "$ENV_SETUP_FILE" ]; then chmod +x ./$ENV_SETUP_FILE; ./$ENV_SETUP_FILE; fi @@ -27,15 +32,18 @@ - chmod 700 ~/.ssh # Add keys to known hosts for Docker Compose - ssh-keyscan $SSH_URL >> ~/.ssh/known_hosts - # Install docker-compose, from release directly - - wget https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -O /usr/local/bin/docker-compose + # If missing, install docker-compose from release directly + - > + if [[ ! -f "$DOCKER_COMPOSE" ]]; then + wget https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -O $DOCKER_COMPOSE + fi - chmod +x /usr/local/bin/docker-compose script: - export DOCKER_HOST=ssh://$SSH_USER@$SSH_URL:$SSH_PORT - if [[ -n "$SCP_SOURCE" && -n "$SCP_DESTINATION" ]]; then scp -rp $SCP_SOURCE $SSH_USER@$SSH_URL:$SCP_DESTINATION; fi - if [[ -n "$DOCKER_REGISTRY" ]]; then echo "$DOCKER_REGISTRY_PASS" | docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin; fi - - docker-compose $COMPOSE_EXTRA_ARGS pull - - docker-compose $COMPOSE_EXTRA_ARGS up -d --remove-orphans $COMPOSE_UP_EXTRA_ARGS + - $DOCKER_COMPOSE $COMPOSE_EXTRA_ARGS pull + - $DOCKER_COMPOSE $COMPOSE_EXTRA_ARGS up -d --remove-orphans $COMPOSE_UP_EXTRA_ARGS .broken-ssh-connection-workaround: script: @@ -45,17 +53,25 @@ - rm -f /tmp/docker.sock - ssh -M -S docker-ctrl-socket -fnNT -L /tmp/docker.sock:/var/run/docker.sock "$SSH_USER@$SSH_URL" -p $SSH_PORT -o ControlPersist=no - if [[ -n "$DOCKER_REGISTRY" ]]; then echo "$DOCKER_REGISTRY_PASS" | docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin; fi - - docker-compose $COMPOSE_EXTRA_ARGS pull - - docker-compose $COMPOSE_EXTRA_ARGS up -d --remove-orphans $COMPOSE_UP_EXTRA_ARGS + - $DOCKER_COMPOSE $COMPOSE_EXTRA_ARGS pull + - $DOCKER_COMPOSE $COMPOSE_EXTRA_ARGS up -d --remove-orphans $COMPOSE_UP_EXTRA_ARGS .docker-compose-validate: image: docker:20.10.16-alpine3.15 stage: verify variables: DOCKER_COMPOSE_VERSION: "v2.7.0" + DOCKER_COMPOSE: "$CI_PROJECT_DIR/docker-compose" + cache: + key: "docker-compose-$DOCKER_COMPOSE_VERSION" + paths: + - $CI_PROJECT_DIR/docker-compose before_script: - # Install docker-compose, from release directly - - wget https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -O /usr/local/bin/docker-compose + # If missing, install docker-compose from release directly + - > + if [[ ! -f "$DOCKER_COMPOSE" ]]; then + wget https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-linux-x86_64 -O $DOCKER_COMPOSE + fi - chmod +x /usr/local/bin/docker-compose script: - - docker-compose $COMPOSE_EXTRA_ARGS config --quiet + - $DOCKER_COMPOSE $COMPOSE_EXTRA_ARGS config --quiet