From 31829c47228441d12138c198c8821561c3491004 Mon Sep 17 00:00:00 2001 From: Alessandro Desantis Date: Sat, 27 Apr 2019 17:12:48 +0200 Subject: [PATCH] Make environments generic This allows people to create trunk-based deployment workflows with more than just a staging and production environment. --- README.md | 19 ++--- src/examples/trunk-based-deployment.yml | 14 +-- src/jobs/deploy-production.yml | 60 ------------- src/jobs/deploy-staging.yml | 79 ----------------- src/jobs/deploy.yml | 109 ++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 156 deletions(-) delete mode 100644 src/jobs/deploy-production.yml delete mode 100644 src/jobs/deploy-staging.yml create mode 100644 src/jobs/deploy.yml diff --git a/README.md b/README.md index 146deec..cd93b5a 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,14 @@ workflows: build-and-deploy: jobs: - build - - opsworks/deploy-staging: + - opsworks/deploy: + name: deploy-staging app_id: your-staging-opsworks-app-id stack_id: your-staging-opsworks-stack-id github_repo: myorg/myrepo slack_hook_url: https://your.slack.hook.url + current_env: staging + next_env: production requires: - build filters: @@ -37,23 +40,17 @@ workflows: only: master - approve-production: requires: - - opsworks/deploy-staging + - deploy-staging type: approval - filters: - branches: - only: master - - opsworks/deploy-production: + - opsworks/deploy: + name: deploy-production app_id: your-staging-opsworks-app-id stack_id: your-staging-opsworks-stack-id github_repo: myorg/myrepo slack_hook_url: https://your.slack.hook.url + current_env: production requires: - approve-production - -jobs: - build: - steps: - - run your tests here ``` ## Publishing diff --git a/src/examples/trunk-based-deployment.yml b/src/examples/trunk-based-deployment.yml index 9b4446a..8cb8ab2 100644 --- a/src/examples/trunk-based-deployment.yml +++ b/src/examples/trunk-based-deployment.yml @@ -12,11 +12,14 @@ usage: build-and-deploy: jobs: - build - - opsworks/deploy-staging: + - opsworks/deploy: + name: deploy-staging app_id: your-staging-opsworks-app-id stack_id: your-staging-opsworks-stack-id github_repo: myorg/myrepo slack_hook_url: https://your.slack.hook.url + current_env: staging + next_env: production requires: - build filters: @@ -24,16 +27,15 @@ usage: only: master - approve-production: requires: - - opsworks/deploy-staging + - deploy-staging type: approval - filters: - branches: - only: master - - opsworks/deploy-production: + - opsworks/deploy: + name: deploy-production app_id: your-staging-opsworks-app-id stack_id: your-staging-opsworks-stack-id github_repo: myorg/myrepo slack_hook_url: https://your.slack.hook.url + current_env: production requires: - approve-production diff --git a/src/jobs/deploy-production.yml b/src/jobs/deploy-production.yml deleted file mode 100644 index 4c47bc3..0000000 --- a/src/jobs/deploy-production.yml +++ /dev/null @@ -1,60 +0,0 @@ -executor: default - -parameters: - app_id: - type: string - description: The ID of the OpsWorks app to deploy. - stack_id: - type: string - description: The ID of the OpsWorks stack to deploy. - github_repo: - type: string - description: The path of your GitHub repo (org/name). - slack_hook_url: - type: string - description: The URL of the Slack hook to notify. - -steps: - - checkout - - run: - name: Install awscli - command: pip install awscli --user - - deploy: - name: Deploy to Production - command: | - ~/.local/bin/aws opsworks update-app --app-id <> --app-source Revision=$CIRCLE_SHA1 - ~/.local/bin/aws opsworks wait deployment-successful --deployment-ids `~/.local/bin/aws opsworks create-deployment --stack-id <> --app-id <> --command "{\"Name\":\"deploy\"}" | awk '{print $2}' | sed 's/"//g'` - - run: - name: Remember Last Deploy Ref - command: echo $CIRCLE_SHA1 > .last_deploy - - save_cache: - key: last-head-v2-{{ epoch }} - paths: .last_deploy - - run: - name: Notify Successful Production Deploy - command: | - cat \<<- EOF > notification.json - { - "attachments": [{ - "title": "Deploy", - "color": "good", - "text": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`> has been deployed to *production*!" - }] - } - EOF - curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> - when: on_success - - run: - name: Notify Failed Production Deploy - command: | - cat \<<- EOF > notification.json - { - "attachments": [{ - "title": "Deploy", - "color": "danger", - "text": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`> failed being deployed to *production*!" - }] - } - EOF - curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> - when: on_fail diff --git a/src/jobs/deploy-staging.yml b/src/jobs/deploy-staging.yml deleted file mode 100644 index ae332b1..0000000 --- a/src/jobs/deploy-staging.yml +++ /dev/null @@ -1,79 +0,0 @@ -executor: default - -parameters: - app_id: - type: string - description: The ID of the OpsWorks app to deploy. - stack_id: - type: string - description: The ID of the OpsWorks stack to deploy. - github_repo: - type: string - description: The path of your GitHub repo (org/name). - slack_hook_url: - type: string - description: The URL of the Slack hook to notify. - -steps: - - checkout - - restore_cache: - key: last-head-v2 - - run: - name: Install awscli - command: pip install awscli --user - - deploy: - name: Deploy Staging - command: | - ~/.local/bin/aws opsworks update-app --app-id <> --app-source Revision=$CIRCLE_SHA1 - ~/.local/bin/aws opsworks wait deployment-successful --deployment-ids `~/.local/bin/aws opsworks create-deployment --stack-id <> --app-id <> --command "{\"Name\":\"deploy\"}" | awk '{print $2}' | sed 's/"//g'` - - run: - name: Notify Staging Deploy - command: | - cat \<<- EOF > notification.json - { - "attachments": [{ - "title": "Deploy", - "color": "good", - "text": "The application has been deployed to *staging*.\nApproving the workflow will deploy to *production*.\nChanges are from last *production* deploy.", - "fields": [{ - "title": "Staging Ref", - "short": true, - "value": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`>" - }, { - "title": "Production Ref", - "short": true, - "value": ">/commit/`cat .last_deploy`|`cat .last_deploy | cut -c1-8`>" - } - ], - "actions": [{ - "type": "button", - "text": "See Changes :octocat:", - "style": "primary", - "url": "https://github.com/<>/compare/`cat .last_deploy`...$CIRCLE_SHA1" - }, { - "type": "button", - "style": "danger", - "text": "Approve Workflow :shipit:", - "url": "https://circleci.com/workflow-run/$CIRCLE_WORKFLOW_ID" - } - ] - }] - } - EOF - curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> - when: on_success - - run: - name: Notify Failed Staging Deploy - command: | - cat \<<- EOF > notification.json - - { - "attachments": [{ - "title": "Deploy", - "color": "danger", - "text": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`> failed being deployed to *staging*!" - }] - } - EOF - curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> - when: on_fail diff --git a/src/jobs/deploy.yml b/src/jobs/deploy.yml new file mode 100644 index 0000000..d44029d --- /dev/null +++ b/src/jobs/deploy.yml @@ -0,0 +1,109 @@ +executor: default + +parameters: + app_id: + type: string + description: The ID of the OpsWorks app to deploy. + stack_id: + type: string + description: The ID of the OpsWorks stack to deploy. + github_repo: + type: string + description: The path of your GitHub repo (org/name). + slack_hook_url: + type: string + description: The URL of the Slack hook to notify. + current_env: + type: string + description: The name of the environment currently being deployed. + next_env: + type: string + description: The next of the next environment to deploy. + default: ~ + +steps: + - checkout + - run: + name: Install awscli + command: pip install awscli --user + - deploy: + name: Deploy to <> + command: | + ~/.local/bin/aws opsworks update-app --app-id <> --app-source Revision=$CIRCLE_SHA1 + ~/.local/bin/aws opsworks wait deployment-successful --deployment-ids `~/.local/bin/aws opsworks create-deployment --stack-id <> --app-id <> --command "{\"Name\":\"deploy\"}" | awk '{print $2}' | sed 's/"//g'` + - run: + name: Remember Last Deploy Ref + command: echo $CIRCLE_SHA1 > .last_deploy_<> + - save_cache: + key: last-head-v2-{{ epoch }} + paths: .last_deploy + - when: + condition: <> + steps: + - run: + name: Notify Successful Deploy + command: | + cat \<<- EOF > notification.json + { + "attachments": [{ + "title": "Deploy", + "color": "good", + "text": "The application has been deployed to *<>*.\nApproving the workflow will deploy to *<>*.\nChanges are from last *<>* deploy.", + "fields": [{ + "title": "<> Ref", + "short": true, + "value": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`>" + }, { + "title": "<> Ref", + "short": true, + "value": ">/commit/`cat .last_deploy_<>`|`cat .last_deploy | cut -c1-8`>" + } + ], + "actions": [{ + "type": "button", + "text": "See Changes :octocat:", + "style": "primary", + "url": "https://github.com/<>/compare/`cat .last_deploy_<>`...$CIRCLE_SHA1" + }, { + "type": "button", + "style": "danger", + "text": "Approve Workflow :shipit:", + "url": "https://circleci.com/workflow-run/$CIRCLE_WORKFLOW_ID" + } + ] + }] + } + EOF + curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> + when: on_success + - unless: + condition: <> + steps: + - run: + name: Notify Successful Deploy + command: | + cat \<<- EOF > notification.json + { + "attachments": [{ + "title": "Deploy", + "color": "good", + "text": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`> has been deployed to *<>*!" + }] + } + EOF + curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> + when: on_success + - run: + name: Notify Failed Deploy + command: | + cat \<<- EOF > notification.json + { + "attachments": [{ + "title": "Deploy", + "color": "danger", + "text": ">/commit/$CIRCLE_SHA1|`echo $CIRCLE_SHA1 | cut -c1-8`> failed being deployed to *<>*!" + }] + } + EOF + curl -X POST -H 'Content-type: application/json' --data '@notification.json' <> + when: on_fail