Skip to content

Instantly share code, notes, and snippets.

@plcosta
Created April 10, 2019 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plcosta/43d9a627c73ae242ef1ffe483e458296 to your computer and use it in GitHub Desktop.
Save plcosta/43d9a627c73ae242ef1ffe483e458296 to your computer and use it in GitHub Desktop.
GitLab CI - Review Apps - Example
image: ruby:2.5.1
cache:
key: "$CI_COMMIT_REF_NAME"
untracked: true
variables:
POSTGRES_DB: dbname
POSTGRES_USER: dbuser
POSTGRES_PASSWORD: dbpass
DATABASE_URL: "postgres://dbuser:dbpass@postgres:5432/dbname"
DOKKU: ssh dokku@$REVIEW_DOKKU_HOST_IP
APP_NAME: $CI_ENVIRONMENT_SLUG
DB_NAME: $CI_ENVIRONMENT_SLUG-database
BUILDPACK_URL: "https://github.com/bundler/heroku-buildpack-bundler2"
stages:
- review
# - test
# - staging
# - production
before_script:
- 'apt-get update -qq && apt-get -o dir::cache::archives="/cache/apt"
install -y -qq sqlite3 libsqlite3-dev nodejs ruby-dev libffi-dev'
- gem install bundler -v '~> 2.0.1' --no-ri --no-rdoc
- bundle install --jobs $(nproc) --path=/cache/bundler
- ln -nfs .test.env .env
.before_ssh: &before_ssh
- 'which ssh-agent || ( apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- ssh-keyscan -H "$REVIEW_DOKKU_HOST_IP" >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
.deploy_review_script: &deploy_review_script
- $DOKKU postgres:unlink $DB_NAME $APP_NAME || echo $?
- $DOKKU postgres:destroy $DB_NAME --force || echo $?
- $DOKKU cleanup || echo $?
- $DOKKU apps:create $APP_NAME || echo $?
- $DOKKU postgres:create $DB_NAME || echo $?
- $DOKKU postgres:link $DB_NAME $APP_NAME || echo $?
- $DOKKU config:set --no-restart $APP_NAME BUILDPACK_URL=$BUILDPACK_URL TZ=America/Sao_Paulo RAILS_SERVE_STATIC_FILES=1
NO_FORCE_SSL=1 RACK_DEV_MARK_ENV=review AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
AWS_BUCKET_NAME=$AWS_BUCKET_NAME AWS_ENDPOINT=$AWS_ENDPOINT AWS_REGION=$AWS_REGION HOST_URL=$CI_ENVIRONMENT_SLUG.$REVIEW_DOKKU_HOST
CORS_ORIGINS=$CORS_ORIGINS DISABLE_DATABASE_ENVIRONMENT_CHECK=1
- git push dokku@$REVIEW_DOKKU_HOST_IP:$APP_NAME HEAD:refs/heads/master
- $DOKKU -tt run $APP_NAME bundle exec rake db:migrate
- $DOKKU -tt run $APP_NAME bundle exec rake db:seed
- $DOKKU ps:scale $APP_NAME web=1
review:
stage: review
before_script: *before_ssh
script: *deploy_review_script
environment:
name: review/$CI_COMMIT_REF_NAME
url: http://$CI_ENVIRONMENT_SLUG.$REVIEW_DOKKU_HOST
on_stop: stop_review
only:
- branches
except:
- master
- stable
when: manual
stop_review:
stage: review
variables:
GIT_STRATEGY: none
before_script: *before_ssh
script:
- $DOKKU apps:destroy $CI_ENVIRONMENT_SLUG --force || echo $?
- $DOKKU postgres:destroy $CI_ENVIRONMENT_SLUG-database --force || echo $?
- $DOKKU cleanup || echo $?
environment:
name: review/$CI_COMMIT_REF_NAME
action: stop
when: manual
only:
- branches
except:
- master
- stable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment