Last active
November 3, 2018 13:50
-
-
Save nwatth/6aa0451b02a162908994d7790c748c61 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
| docker service create \ | |
| --name traefik \ | |
| --constraint 'node.role==manager' \ | |
| --publish 80:80 \ | |
| --publish 8081:8080 \ | |
| --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \ | |
| --network traefik-net \ | |
| traefik \ | |
| --docker \ | |
| --docker.swarmmode \ | |
| --docker.domain=mystaging.com \ | |
| --docker.watch \ | |
| --logLevel=DEBUG \ | |
| --web |
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
| FROM ruby:2.5.1-alpine3.7 | |
| RUN apk update \ | |
| && apk upgrade \ | |
| && apk add --no-cache bash \ | |
| git \ | |
| openssh \ | |
| build-base \ | |
| linux-headers \ | |
| postgresql-dev \ | |
| gnupg \ | |
| imagemagick \ | |
| nodejs \ | |
| tzdata | |
| RUN apk update \ | |
| && apk add curl binutils tar \ | |
| && rm -rf /var/cache/apk/* \ | |
| && /bin/bash \ | |
| && touch ~/.bashrc \ | |
| && curl -o- -L https://yarnpkg.com/install.sh | bash \ | |
| && apk del curl tar binutils | |
| ENV PATH /root/.yarn/bin:$PATH | |
| ENV APP_PATH /usr/src/app | |
| WORKDIR $APP_PATH | |
| ADD Gemfile $APP_PATH | |
| Add Gemfile.lock $APP_PATH | |
| RUN bundle install --jobs `expr $(cat /proc/cpuinfo | grep -c "cpu cores") - 1` --retry 3 | |
| ADD package.json $APP_PATH | |
| ADD yarn.lock $APP_PATH | |
| RUN yarn install | |
| COPY . $APP_PATH | |
| EXPOSE 3000 | |
| ENTRYPOINT ["bundle", "exec"] | |
| CMD ["rails", "server"] |
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 | |
| import groovy.json.JsonOutput | |
| 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>" | |
| SLACK_URL = "<SLACK INCOMING WEBHOOK URL>" | |
| } | |
| 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) | |
| notifySlack([ | |
| [ | |
| color: "#36a64f", | |
| title: "<${env.BUILD_URL}|Success ${env.BUILD_DISPLAY_NAME}>", | |
| fields: [ | |
| [ | |
| title: "Project", | |
| value: "${env.IMAGE_NAME}", | |
| short: true | |
| ], | |
| [ | |
| title: "Ticket", | |
| value: "${env.TAG_NAME}", | |
| short: true | |
| ], | |
| [ | |
| title: "Staging URL", | |
| value: "${env.STAGING_URL}", | |
| short: true | |
| ], | |
| ] | |
| ] | |
| ]) | |
| } | |
| } | |
| } | |
| } | |
| def notifySlack(attachments) { | |
| def payload = JsonOutput.toJson([attachments: attachments]) | |
| sh "curl -X POST --data-urlencode \'payload=${payload}\' ${env.SLACK_URL}" | |
| } | |
| 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