Skip to content

Instantly share code, notes, and snippets.

@ThiagoBarradas
Created March 16, 2018 19:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ThiagoBarradas/ccf3531f9bf5567876e1da6deedfa014 to your computer and use it in GitHub Desktop.
Save ThiagoBarradas/ccf3531f9bf5567876e1da6deedfa014 to your computer and use it in GitHub Desktop.
Auto rollback with circleci 2.0
## 1) GENERATE AN API TOKEN (https://circleci.com/gh/<gh-user>/<gh-repo-name>/edit#api)
## 2) ADD ENV VAR WITH NAME "CIRCLE_API_TOKEN" WITH PREVIOUS GENERATED TOKEN
version: 2
jobs:
build:
machine: true
steps:
- run:
name: Build App
command: |
URL=http://pruu.herokuapp.com/dump/test-circleci
BODY='{
"step" : "build"
}'
curl -X POST ${URL} -H 'Content-Type: application/json' -d "${BODY}"
unit_test:
machine: true
steps:
- run:
name: Unit Tests
command: |
URL=http://pruu.herokuapp.com/dump/test-circleci
BODY='{
"step" : "unit_test"
}'
curl -X POST ${URL} -H 'Content-Type: application/json' -d "${BODY}"
deploy_production:
machine: true
steps:
- run:
name: Unit Tests
command: |
URL=http://pruu.herokuapp.com/dump/test-circleci
BODY='{
"step" : "deploy_production"
}'
curl -X POST ${URL} -H 'Content-Type: application/json' -d "${BODY}"
integration_test:
machine: true
steps:
- run:
name: Integration Test Command
command: |
URL=WRONG_URL_FOR_SIMULATE_ERROR
BODY='{
"step" : "integration_test"
}'
curl -X POST ${URL} -H 'Content-Type: application/json' -d "${BODY}"
- run:
name: Auto Rollback
command: |
curl --user ${CIRCLE_API_TOKEN}: \
--data build_parameters[CIRCLE_JOB]=deploy_rollback \
--data revision=$CIRCLE_SHA1 \
https://circleci.com/api/v1.1/project/github/${CIRCLE_USERNAME}/$CIRCLE_PROJECT_REPONAME/tree/$CIRCLE_BRANCH
when: on_fail
deploy_commit:
machine: true
steps:
- run:
name: Commit/Finish Deploy
command: |
URL=http://pruu.herokuapp.com/dump/test-circleci
BODY='{
"step" : "deploy_commit"
}'
curl -X POST ${URL} -H 'Content-Type: application/json' -d "${BODY}"
deploy_rollback:
machine: true
steps:
- run:
name: Rollback Deploy
command: |
URL=http://pruu.herokuapp.com/dump/test-circleci
BODY='{
"step" : "deploy_rollback"
}'
curl -X POST ${URL} -H 'Content-Type: application/json' -d "${BODY}"
workflows:
version: 2
build_publish_deploy:
jobs:
- build
- unit_test:
requires:
- build
- hold_deploy:
type: approval
requires:
- unit_test
- deploy_production:
requires:
- hold_deploy
- integration_test:
requires:
- deploy_production
- hold_commit:
type: approval
requires:
- integration_test
- hold_rollback:
type: approval
requires:
- integration_test
- deploy_commit:
requires:
- hold_commit
- deploy_rollback:
requires:
- hold_rollback
@jaswinder97
Copy link

@ThiagoBarradas could you please add some comments to elaborate each step

@zallesov
Copy link

zallesov commented Sep 27, 2019

Here the condition to execute deploy_commit and deploy_rollback are efectively the same. hold_rollback == hold_commit

      - hold_commit:
          type: approval
          requires:
            - integration_test
      - hold_rollback:
          type: approval
          requires:
            - integration_test
      - deploy_commit:
          requires:
            - hold_commit
      - deploy_rollback:
          requires:
            - hold_rollback

@ThiagoBarradas
Copy link
Author

no, because this steps are a "approval button", even depending from same step, each step (hold_commit and hold_rollback) need a different action to start 😊 you can try to confirmate this behavior

@joethrive
Copy link

Thanks for this! Been migrating from Jenkins to circleci and was trying to figure out a good deploy/rollback mechanism. This looks like what I had in mind. How is this setup working out for you in practice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment