Skip to content

Instantly share code, notes, and snippets.

@sebelga
Forked from balintsera/.gitlab-ci.yml
Created November 3, 2020 08:49
Show Gist options
  • Save sebelga/76e92c1467bea3a5d2a967e59d96d91d to your computer and use it in GitHub Desktop.
Save sebelga/76e92c1467bea3a5d2a967e59d96d91d to your computer and use it in GitHub Desktop.
Gitlab config for docker based CICD pipeline (node.js)
image: customregistry/docker-builder:latest
variables:
dockerTag: '$CI_BUILD_REF'
DOCKER_REPO: customregistry
IMAGE_BASE_NAME: redis-faas
IMAGE: $EVISTA_DOCKER_REPO/$IMAGE_BASE_NAME:$CI_BUILD_REF
CONTAINER_NAME: 'fotok-redis-pipeline'
TARGET_DIR_STAGE: /srv/docker/staging/redis-faas
TARGET_DIR_PROD: /srv/docker/prod/redis-faas
before_script:
- echo $dockerTag
- echo $DOCKER_CERT > cert.ca
- eval $(ssh-agent -s)
- ssh-add <(echo "$ID_RSA")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
build:
type: build
tags:
- docker-in-docker-pure-builder
script:
# build test images
- docker build -t $IMAGE .
only:
- master
- production
test:
type: test
variables:
ERROR_PATH: /tmp/example_pipeline_testresults
tags:
- docker-in-docker-builder
script:
# created directory for errored pages
- mkdir -p ${ERROR_PATH} || true
# start the previously built container
- docker run -d --add-host 'localhost localhost.localdomain localtest:127.0.0.1' -v ${ERROR_PATH}:/app/tests/_output --name ${CONTAINER_NAME} ${IMAGE}
# actually run tests
- docker exec ${CONTAINER_NAME} /app/runtests.sh
only:
- master
- production
deploy_stage:
type: deploy
variables:
RELEASE_IMAGE: $DOCKER_REPO/$IMAGE_BASE_NAME:latest
DOCKER_COMPOSE_CMD: cd $TARGET_DIR_STAGE && docker-compose
tags:
- docker-in-docker-builder
script:
# tag the final image (the previous test image is built upon the test image file)
- docker tag $IMAGE $IMAGE
- docker --tlscacert cert.ca push $IMAGE
# tag the full image as lastest and push it
- docker tag $IMAGE $RELEASE_IMAGE
- docker --tlscacert cert.ca push $RELEASE_IMAGE
# deploy the images
- ssh [email protected] -p2224 "docker pull $RELEASE_IMAGE"
- ssh [email protected] -p2224 "${DOCKER_COMPOSE_CMD} stop"
- ssh [email protected] -p2224 "${DOCKER_COMPOSE_CMD} up -d"
only:
- master
deploy_production:
type: deploy
variables:
RELEASE_IMAGE: $DOCKER_REPO/$IMAGE_BASE_NAME:stable
DOCKER_COMPOSE_CMD: cd $TARGET_DIR_PROD && docker-compose
tags:
- docker-in-docker-builder
script:
# tag the final image (the previous test image is built upon the test image file)
- docker tag $IMAGE $IMAGE
- docker --tlscacert cert.ca push $IMAGE
# tag the full image as lastest and push it
- docker tag $IMAGE $RELEASE_IMAGE
- docker --tlscacert cert.ca push $RELEASE_IMAGE
# deploy the images
- ssh [email protected] -p2224 "docker pull $RELEASE_IMAGE"
- ssh [email protected] -p2224 "${DOCKER_COMPOSE_CMD} stop"
- ssh [email protected] -p2224 "${DOCKER_COMPOSE_CMD} up -d"
only:
- production
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment