Skip to content

Commit 30bcd6b

Browse files
authored
Merge pull request #1 from NarrativeScience/bump-sleep
Bump sleep timeout, upgrade dependencies
2 parents d7c0219 + 1ae8bd5 commit 30bcd6b

File tree

6 files changed

+70
-627
lines changed

6 files changed

+70
-627
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
orb.yml

LICENSE.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
Copyright (c) 2019, Narrative Science
2+
23
All rights reserved.
34

45
Redistribution and use in source and binary forms, with or without
56
modification, are permitted provided that the following conditions are met:
6-
* Redistributions of source code must retain the above copyright
7-
notice, this list of conditions and the following disclaimer.
8-
* Redistributions in binary form must reproduce the above copyright
9-
notice, this list of conditions and the following disclaimer in the
10-
documentation and/or other materials provided with the distribution.
11-
* Neither the name of the <organization> nor the
12-
names of its contributors may be used to endorse or promote products
13-
derived from this software without specific prior written permission.
7+
8+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
* Neither the name of the <organization> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
1411

1512
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1613
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

README.md

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
# CircleCI Orb: Workflow Manager
1+
# CircleCI Orb: Workflow Manager [![CircleCI Orb Version](https://img.shields.io/badge/endpoint.svg?url=https://badges.circleci.io/orb/narrativescience/workflow-manager)](https://circleci.com/orbs/registry/orb/narrativescience/workflow-manager) [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
22

3-
Manage workflow concurrency and job state using an external store.
3+
[View in CircleCI Orb Registry](https://circleci.com/orbs/registry/orb/narrativescience/workflow-manager)
4+
5+
Manage workflow concurrency and job state using an external store.
6+
7+
This orb allows you to:
8+
9+
- Limit the number of concurrently running workflows. This is useful when you want to only deploy one batch of changes at a time or use AWS CloudFormation and need to wait for the previous deploy to finish.
10+
- Squash commits that are deployed in a workflow when the workflow is allowed to proceed
11+
- Store and retreive data from a key-value store in jobs, even if they're run in parallel
12+
- Track the status of a workflow from the command line
13+
- Send a Slack message when a workflow succeeds after failing
414

515
## Rationale
616

717
Even though we tested it in different jobs, the [queue orb](https://circleci.com/orbs/registry/orb/eddiewebb/queue) did not consistently block the deploy workflow from executing more than one commit at a time. We don't think the issue is necessarily with the queue orb, it's most likely with the CircleCI API.
818

919
Instead of using the CircleCI API to determine if the workflow can continue, we can use a remote key-value store (DynamoDB) that acts as a first-in-first-out (FIFO) queue. This will allow us to process deploys one at a time, in the order the commits were merged.
1020

11-
## Supporting Infrastructure
21+
## Installation
1222

1323
### Requirements
1424

1525
- Install the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)
26+
- Install [jq](https://stedolan.github.io/jq/download/)
1627
- Set your AWS user profile or `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_DEFAULT_REGION` in your shell
1728

18-
### How to deploy
29+
### Deploying the Stack
1930

20-
Deploy the [CloudFormation stack](./cloudformation_template.yml) with infrastructure to support this orb using the AWS CLI:
31+
This orb uses a DynamoDB table to store state. Deploy the [CloudFormation stack](./cloudformation_template.yml) with infrastructure to support this orb using the AWS CLI:
2132

2233
```bash
2334
aws cloudformation deploy \
@@ -27,9 +38,9 @@ aws cloudformation deploy \
2738

2839
This will create a DynamoDB table. You can tweak that template to align with your organization's standards or use the AWS console instead.
2940

30-
### Permissions
41+
### IAM Permissions
3142

32-
You need to allow your CircleCI AWS user to interact with the DynamoDB table. Create a managed or inline policy and attach it to the IAM user:
43+
You need to allow your CircleCI AWS IAM user to interact with the DynamoDB table. Create a managed or inline policy and attach it to the IAM user:
3344

3445
```json
3546
{
@@ -52,11 +63,28 @@ You need to allow your CircleCI AWS user to interact with the DynamoDB table. Cr
5263
}
5364
```
5465

55-
### Deploy Queue
66+
### CircleCI Context
67+
68+
For each workflow that uses this `workflow-manager` orb, create a [CircleCI Context](https://circleci.com/docs/2.0/contexts/) with an environment variable named `WORKFLOW_LOCK_KEY` set to the name of a workflow. Then, set `context: my-new-context` for every job in the workflow.
5669

57-
#### Entering the Queue
70+
## Usage
71+
72+
### Entering the Queue: `wait-in-queue`
73+
74+
We run the [`wait-in-queue` job](src/jobs/wait-in-queue.yml) first in the deploy workflow. For example:
75+
76+
```yaml
77+
jobs:
78+
- workflow-manager/wait-in-queue:
79+
context: my-deploy
80+
filters:
81+
branches:
82+
only: master
83+
check_previous_commit: true
84+
do_not_cancel_workflow_if_tag_in_commit: "[force deploy]"
85+
```
5886
59-
We run the [`wait-in-queue` job](src/jobs/wait-in-queue.yml) first in the deploy workflow. It starts by adding an item to the table with some attributes pertaining to the workflow instance:
87+
It starts by adding an item to the table with some attributes pertaining to the workflow instance:
6088
6189
Field | Type | Description
6290
--- | --- | ---
@@ -72,9 +100,22 @@ status | string | Localized secondary index. One of `QUEUED`, `RUNNING`, `SUCCES
72100

73101
The job then starts polling the table for the oldest item that doesn't have a status attribute of `SUCCESS` or `FAILED`. If that item has the same `workflow_id` as the job, that means the job is at the "front" of the queue and can continue; we set the item's status to `RUNNING` and the workflow transitions to the next job. It will wait in the queue for up to 4 hours before failing. When the workflow is allowed to continue, it can be said to have a "lock" on the deploy.
74102

75-
#### Exiting the Queue
103+
### Exiting the Queue: `exit-queue`
76104

77-
The [`exit-queue` job](src/jobs/exit-queue.yml) updates the table item's status attribute to be `SUCCESS` or `FAILED` depending on the value of the `exit_condition` parameter. By updating the job status, it allows other workflows to continue. This job should be called as the last job of every "branch" in a given workflow. For our deploy workflow this means putting it in 3 or 4 places.
105+
The [`exit-queue` job](src/jobs/exit-queue.yml) updates the table item's status attribute to be `SUCCESS` or `FAILED` depending on the value of the `exit_condition` parameter. By updating the job status, it allows other workflows to continue. This job should be called as the last job of every "branch" in a given workflow. For example:
106+
107+
```yaml
108+
jobs:
109+
# ...
110+
- workflow-manager/exit-queue:
111+
context: my-deploy
112+
requires:
113+
- deploy-stack
114+
filters:
115+
branches:
116+
only: master
117+
send_slack_on_recovery: true
118+
```
78119

79120
Even if we add the `exit-queue` job in all the right places in the workflow, we can still get into a state in which the lock is not released when a job fails. CircleCI does not have support for "run a job if some other job failed" so we have to add boilerplate to do so. This takes the form of adding the following step at the bottom of every job that the deploy workflow uses:
80121

@@ -87,7 +128,7 @@ The `exit-queue` command will then release the lock only if a previous step in t
87128

88129
Instead of passing a lock key parameter down through the job/command parameter stack, we can instead leverage a [CircleCI Context](https://circleci.com/docs/2.0/contexts/) that sets an environment variable called `WORKFLOW_LOCK_KEY`. If all jobs in the workflow include the `context: <context name>` parameter, all commands will have access to that environment variable. The lock-related commands can source that variable to set the lock key.
89130

90-
#### Squashing Commits
131+
### Squashing Commits
91132

92133
The deploy worklow has the capability to "squash commits", which replicates a feature of Jenkins that didn't come for free in CircleCI.
93134

@@ -99,19 +140,10 @@ __What if I don't want my commit squashed?__ If you don't want this behavior for
99140

100141
__Note:__ As of now, if there's a failure in the deploy workflow, the Slack message sent to the channel will only include the author of that commit, i.e. it won't contain the list of authors of commits that were squashed. We can see how it plays out before deciding if this behavior should be added.
101142

102-
## Workflows CLI
143+
### Workflows CLI
103144

104145
The `./workflows-cli.sh` script is a CLI for working with CircleCI workflows. It's mostly a wrapper on top of the `aws` CLI and primarily queries the workflows table in DynamoDB (see: [Deploy Queue](#deploy-queue)).
105146

106-
### Installation
107-
108-
Requirements:
109-
110-
- Install the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)
111-
- Install [jq](https://stedolan.github.io/jq/download/)
112-
113-
### Usage
114-
115147
```bash
116148
Usage:
117149
./workflows-cli.sh <command> [<arg>...] [options]

0 commit comments

Comments
 (0)