Skip to content

Instantly share code, notes, and snippets.

@Minasokoni
Forked from ThiagoBarradas/config.yml
Created February 28, 2019 15:54
Show Gist options
  • Save Minasokoni/0e7f1e3848b96bb1fa23a738b62306b3 to your computer and use it in GitHub Desktop.
Save Minasokoni/0e7f1e3848b96bb1fa23a738b62306b3 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment