Last active
November 3, 2018 13:51
-
-
Save nwatth/721561c7fc7a6f71a0866094d179343e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env groovy | |
| pipeline { | |
| agent any | |
| environment { | |
| HOST_NAME = "<HOST NAME>" | |
| IMAGE_NAME = "<IMAGE NAME>" | |
| TAG_NAME = BRANCH_NAME.toLowerCase() | |
| STAGING_NAME = "${IMAGE_NAME}-${TAG_NAME}" | |
| STAGING_URL = "http://${STAGING_NAME}.${HOST_NAME}" | |
| SECRET_KEY = "<RAILS SECRET KEY>" | |
| } | |
| stages { | |
| stage('Build') { | |
| steps { | |
| sh "docker build -t ${env.IMAGE_NAME}:${env.TAG_NAME} ." | |
| } | |
| } | |
| stage('Prepare') { | |
| steps { | |
| parallel( | |
| 'postgres': { preparePostgres(env.STAGING_NAME) }, | |
| 'redis': { prepareRedis(env.STAGING_NAME) }, | |
| ) | |
| } | |
| } | |
| stage('Deploy') { | |
| steps { | |
| createEnvironment(env.STAGING_NAME, env.SECRET_KEY) | |
| } | |
| } | |
| } | |
| } | |
| def preparePostgres(name) { | |
| sh "docker service rm ${name}-pg || :" | |
| sh script: """\ | |
| docker service create \ | |
| --name ${name}-pg \ | |
| --network traefik-net \ | |
| postgres \ | |
| """ | |
| } | |
| def prepareRedis(name) { | |
| sh "docker service rm ${name}-redis || :" | |
| sh script: """\ | |
| docker service create \ | |
| --name ${name}-redis \ | |
| --network traefik-net \ | |
| redis \ | |
| """ | |
| } | |
| def createEnvironment(name, secret_key) { | |
| sh "docker service rm ${name} || :" | |
| sh script: """\ | |
| docker service create \ | |
| --name ${name} \ | |
| -e REDIS_URL='redis://${name}-redis:6379' \ | |
| -e DATABASE_URL='postgresql://postgres@${name}-pg' \ | |
| -e RAILS_ENV='production' \ | |
| -e SECRET_KEY_BASE='${secret_key}' \ | |
| -e RAILS_SERVE_STATIC_FILES=true \ | |
| --label 'traefik.port=3000' \ | |
| --network traefik-net \ | |
| ${env.IMAGE_NAME}:${env.TAG_NAME} \ | |
| """ | |
| sh script: """\ | |
| docker run \ | |
| --rm \ | |
| -e RAILS_ENV=production \ | |
| -e SECRET_KEY_BASE='${secret_key}' \ | |
| -e DATABASE_URL=postgresql://postgres@${name}-pg \ | |
| --network traefik-net \ | |
| ${env.IMAGE_NAME}:${env.TAG_NAME} \ | |
| rails db:create db:migrate assets:precompile \ | |
| """ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment