Skip to content

Instantly share code, notes, and snippets.

@sajjadG
Created April 11, 2022 06:53
Show Gist options
  • Save sajjadG/bd58e08e77e1bf590576e4f246e1b6b3 to your computer and use it in GitHub Desktop.
Save sajjadG/bd58e08e77e1bf590576e4f246e1b6b3 to your computer and use it in GitHub Desktop.
.gitlab-ci.yml example2
image: docker:19.03.12
# services:
# - docker:19.03.12-dind
variables:
# DOCKER_DRIVER: overlay2
# DOCKER_TLS_CERTDIR: ""
PROJECT_NAME: my-project
.ssh_preparation: &ssh_preparation
before_script:
- eval $(ssh-agent -s)
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > /etc/ssh/ssh_config'
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
.docker_build: &docker_build
script:
- now=$(date '+%Y-%m-%d-%H-%M')
- echo "$DOCKER_REGISTRY_PASS" | docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin
- docker build --build-arg ENV_FILE=$ENV_FILE --tag $IMAGE_NAME:$now -t $IMAGE_NAME:latest .
stages:
- build
- deploy
#########################################################################
build_staging:
stage: build
variables:
IMAGE_NAME: "sajjadg/${PROJECT_NAME}_snapshot"
ENV_FILE: ".env.staging"
<<: *docker_build
only:
- test
tags:
- staging
deploy_staging:
stage: deploy
<<: *ssh_preparation
script:
- ssh -p $STAGING_SERVER_PORT $STAGING_SERVER_IP -l $STAGING_SERVER_USER "cd /srv/$PROJECT_NAME;/usr/bin/docker-compose up -d;/usr/bin/docker-compose ps"
only:
- test
tags:
- staging
#########################################################################
build_prod:
stage: build
variables:
IMAGE_NAME: "sajjadg/${PROJECT_NAME}"
ENV_FILE: ".env.production"
<<: *docker_build
only:
- master
# tags:
# - builder01
deploy_prod:
stage: deploy
script:
- cd /srv/$PROJECT_NAME;/usr/bin/docker-compose up -d;/usr/bin/docker-compose ps
only:
- master
tags:
- production01
- production02
- production03
when: manual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment