Commit c1c244b6 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Soporta deploy anónimo, actualiza variables y docs

Realiza correcciones de naming en variables que rompen compatibilidad
con la versión anterior.

Permite no definir credenciales para el registry de Docker, en caso de
que el proyecto no sea privado y no se requieran. Omite con ello los
valores por defecto y el uso de variables definidas externamente.

Documenta mejor algunas variables y actualiza para reflejar los cambios.
parent d411140c
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ For REDMIC, we use this image from GitLabCI configuration, but you can use it di

```
$ docker run --rm --name docker-deploy \
	-e STACK=your-stack-name -e SSH_REMOTE=ssh-user@ssh-host -e GITLAB_DEPLOY_KEY="<your-private-key>" \
	-e STACK=your-stack-name -e SSH_REMOTE=ssh-user@ssh-host -e DEPLOY_KEY="<your-private-key>" \
	registry.gitlab.com/redmic-project/docker/docker-deploy:latest \
	<action> <VAR1>=<value1> <VAR2>=<value2> ...
```
@@ -33,13 +33,15 @@ For environment variables, you may define these variables (**bold** are mandator

* **STACK** / **SERVICE**: Name of Docker stack (Swarm mode) or service (standard mode) to deploy.
* **SSH_REMOTE**: SSH user and host of remote machine where you are going to deploy.
* **GITLAB_DEPLOY_KEY**: Private key paired with a public key accepted by remote machine, used to authenticate.
* *COMPOSE_FILE*: Default `docker-compose.yml`.
* *DEPLOY_PATH*: Default `~`.
* *DEPLOY_DIR_NAME*: Default `deploy`.
* *DEFAULT_DEPLOY_FILES*: Default `docker-compose*.yml .env`.
* *REGISTRY_USER*: Default `gitlab-ci-token`.
* *SERVICES_TO_CHECK*: Names of services to check, separated by space.
* **DEPLOY_KEY**: Private key paired with a public key accepted by remote machine, used to authenticate.
* *COMPOSE_FILE*: Name of Docker Compose file with deployment definition. Multiple files are supported, separated by colon (`:`). Default `docker-compose.yml`.
* *DEPLOY_PATH*: Path in remote host where deployment directory (containing temporary files) will be created. Default `~`.
* *DEPLOY_DIR_NAME*: Name of directory containing files needed for deployment. Default `deploy`.
* *DEFAULT_DEPLOY_FILES*: Files needed for deployment, if `${DEPLOY_DIR_NAME}` does not exist. Default `docker-compose*.yml .env`.
* *REGISTRY_URL*: Address of Docker registry where Docker images to deploy are stored. Required for private registries.
* *REGISTRY_USER*: Docker registry username of user with read permissions. Required for private registries.
* *REGISTRY_PASS*: Docker registry user password of user with read permissions. Required for private registries.
* *SERVICES_TO_CHECK*: Names of services to check after deployment, separated by space.
* *STATUS_CHECK_RETRIES*: Default `10`.
* *STATUS_CHECK_INTERVAL*: Default `20`.
* *STATUS_CHECK_DELAY*: Default `120`.
+0 −1
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ COMPOSE_FILE="${COMPOSE_FILE:-docker-compose.yml}"
DEPLOY_PATH="${DEPLOY_PATH:-~}"
DEPLOY_DIR_NAME="${DEPLOY_DIR_NAME:-deploy}"
DEFAULT_DEPLOY_FILES="${DEFAULT_DEPLOY_FILES:-docker-compose*.yml .env}"
REGISTRY_USER="${REGISTRY_USER:-gitlab-ci-token}"

STATUS_CHECK_RETRIES="${STATUS_CHECK_RETRIES:-10}"
STATUS_CHECK_INTERVAL="${STATUS_CHECK_INTERVAL:-20}"
+8 −2
Original line number Diff line number Diff line
@@ -4,12 +4,18 @@ echo -e "\n${INFO_COLOR}Deploying at remote target ${DATA_COLOR}${remoteHost}${I

deployCmd="\
	cd ${DEPLOY_HOME} && \
	docker login -u ${REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY} && \
	if [ ! -z \"${REGISTRY_USER}\" -a ! -z \"${REGISTRY_PASS}\" -a ! -z \"${REGISTRY_URL}\" ] ; \
	then \
		docker login -u ${REGISTRY_USER} -p ${REGISTRY_PASS} ${REGISTRY_URL} ; \
		deployAuthParam=\"--with-registry-auth\" ; \
	else \
		deployAuthParam=\"\" ; \
	fi ; \
	if docker stack ls > /dev/null 2> /dev/null ; \
	then \
		composeFileSplitted=\$(echo ${COMPOSE_FILE} | sed 's/:/ -c /g') && \
		env -i \$(${GREP_BIN} -v '^#\\| ' .env | xargs) \
			docker stack deploy -c \${composeFileSplitted} --with-registry-auth ${STACK:-${SERVICE}} ; \
			docker stack deploy -c \${composeFileSplitted} \${deployAuthParam} ${STACK:-${SERVICE}} ; \
	else \
		docker-compose stop ${SERVICE} && \
		docker-compose rm -f ${SERVICE} && \
+3 −3
Original line number Diff line number Diff line
@@ -10,12 +10,12 @@ fi

remoteHost=$(echo "${SSH_REMOTE}" | cut -f 2 -d '@')

if [ -z "${GITLAB_DEPLOY_KEY}" ]
if [ -z "${DEPLOY_KEY}" ]
then
	echo -e "${FAIL_COLOR}You must define 'GITLAB_DEPLOY_KEY' in environment, with a SSH private key accepted by remote server${NULL_COLOR}"
	echo -e "${FAIL_COLOR}You must define 'DEPLOY_KEY' in environment, with a SSH private key accepted by remote server${NULL_COLOR}"
	exit 1
fi

# Se prepara la identidad para conectar al servidor de despliegue.
eval "$(ssh-agent -s)"
echo "${GITLAB_DEPLOY_KEY}" | tr -d '\r' | ssh-add - > /dev/null
echo "${DEPLOY_KEY}" | tr -d '\r' | ssh-add - > /dev/null
+4 −1
Original line number Diff line number Diff line
@@ -20,7 +20,10 @@ relaunchCmd="\
		echo -e \"${FAIL_COLOR}Service image not found!${NULL_COLOR}\" && \
		exit 1 ; \
	fi ; \
	docker login -u ${REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY} && \
	if [ ! -z \"${REGISTRY_USER}\" -a ! -z \"${REGISTRY_PASS}\" -a ! -z \"${REGISTRY_URL}\" ] ; \
	then \
		docker login -u ${REGISTRY_USER} -p ${REGISTRY_PASS} ${REGISTRY_URL} ; \
	fi ; \
	docker pull \${imageNameAndTag} && \
	imageDigest=\$(docker images --digests --format '{{.Digest}}' \${imageName} | head -1) && \
	docker service update -q --force --image \${imageName}@\${imageDigest} ${SERVICE}"