stages: - build - deploy variables: IMAGE_DEV: $CI_REGISTRY_IMAGE/rabbitmq_dev IMAGE_STAGE: $CI_REGISTRY_IMAGE/rabbitmq_stage IMAGE_PROD: $CI_REGISTRY_IMAGE/rabbitmq_prod VERSION: 1.0.$CI_PIPELINE_ID DOCKER_HOST: ssh://cicd@${DEPLOY_HOST} # Set default docker context in docker:cli container before_script: - eval $(ssh-agent -s) - cat $SSH_PRIVATE_KEY | tr -d '\r' | ssh-add - - sed -i '/StrictHostKeyChecking /c StrictHostKeyChecking no' /etc/ssh/ssh_config || true - echo "$CI_REGISTRY_PASSWORD" |docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY # Task templates .build: stage: build tags: - shell variables: IMAGE_NAME: "" DEPLOY_HOST: "" RABBITMQ_USER: "" RABBITMQ_PASS: "" script: - RABBITMQ_PASS_HASH=`docker run --rm --name rabbit-hash-gen rabbitmq:management-alpine rabbitmqctl hash_password $RABBITMQ_PASS |grep -v $RABBITMQ_PASS` - sed -i "s|RABBITMQ_USER|$RABBITMQ_USER|g" definitions.json - sed -i "s|RABBITMQ_PASS_HASH|$RABBITMQ_PASS_HASH|" definitions.json - docker build -t $IMAGE_NAME:$VERSION . - docker push $IMAGE_NAME:$VERSION - docker tag $IMAGE_NAME:$VERSION $IMAGE_NAME:latest - docker push $IMAGE_NAME:latest .deploy: stage: deploy tags: - docker image: docker:cli variables: STACK: "" IMAGE_NAME: "" NETWORK: "" DEPLOY_HOST: "" RABBITMQ_STORAGE: "" RABBITMQ_PORT: "" RABBITMQ_ADMIN_PORT: "" script: - docker pull $IMAGE_NAME:$VERSION - docker stack rm $STACK - docker network create --driver=overlay --scope=swarm $NETWORK || true - docker stack deploy -c ./docker-compose.yml $STACK # Build build_dev: extends: .build rules: - if: $CI_COMMIT_BRANCH == "dev" - if: $BUILD == "yes" variables: IMAGE_NAME: $IMAGE_DEV DEPLOY_HOST: 10.1.0.138 RABBITMQ_USER: $RABBITMQ_USER_DEV RABBITMQ_PASS: $RABBITMQ_PASS_DEV # build_stage: # build_prod: # Deploy deploy_dev: extends: .deploy rules: - if: $CI_COMMIT_BRANCH == "dev" - if: $BUILD == "yes" - if: $DEPLOY == "dev" variables: VERSION: latest variables: STACK: rabbitmq_dev IMAGE_NAME: $IMAGE_DEV NETWORK: ${STACK} DEPLOY_HOST: 10.1.0.138 RABBITMQ_STORAGE: /docker/rabbitmq/dev/rabbitmq_storage RABBITMQ_PORT: 8088 RABBITMQ_ADMIN_PORT: 8089 environment: name: dev url: https://${DEPLOY_HOST}:${RABBITMQ_ADMIN_PORT} # deploy_stage: # deploy_prod: