Skip to content

Commit bbf2495

Browse files
Release Updates (#8)
* feat: try to replace user_auth with token for simplicity * feat: update orb version * rebuild * feat: more updates * fix: example formatting
1 parent 7f585d5 commit bbf2495

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

.circleci/config.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2.1
22
setup: true
33
orbs:
4-
orb-tools: circleci/orb-tools@11.1
4+
orb-tools: circleci/orb-tools@11.6
55
shellcheck: circleci/[email protected]
66

77
filters: &filters
@@ -25,10 +25,8 @@ workflows:
2525
vcs-type: << pipeline.project.type >>
2626
requires:
2727
[orb-tools/lint, orb-tools/review, orb-tools/pack, shellcheck/check]
28-
# Use a context to hold your publishing token.
2928
context: orb-publishing
3029
filters: *filters
31-
# Triggers the next workflow in the Orb Development Kit.
3230
- orb-tools/continue:
3331
pipeline-number: << pipeline.number >>
3432
vcs-type: << pipeline.project.type >>

.circleci/test-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2.1
22
orbs:
33
workflow-queue: promiseofcake/workflow-queue@dev:<<pipeline.git.revision>>
4-
orb-tools: circleci/orb-tools@11.1
4+
orb-tools: circleci/orb-tools@11.6
55

66
filters: &filters
77
tags:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
Forked from https://github.com/eddiewebb/circleci-queue and updated to reduce the use-cases, and migrate to the CircleCI V2 API
99

10-
The purpose of this Orb is to add a concept of a queue to specific branch workflow tasks in CircleCi. The main use-cases is to isolate a set of changes to ensure that one set of a thing is running at one time. Think of smoke-tests against a nonproduction environment.
10+
The purpose of this Orb is to add a concept of a queue to specific branch workflow tasks in CircleCi. The main use-case is to isolate a set of changes to ensure that one set of a thing is running at one time. Think of smoke-tests against a nonproduction environment as a promotion gate.
1111

1212
## Configuration Requirements
1313

14-
In order to use this orb you will need to export a CIRCLECI_USER_AUTH secret added to a context of your choosing. The token is generated and stored as a Basic Auth of a Circle Token (see: https://support.circleci.com/hc/en-us/articles/360052405651-Utilizing-Basic-Authorization-in-CircleCI-API-Calls)
14+
In order to use this orb you will need to export a `CIRCLECI_TOKEN` secret added to a context of your choosing. It will authentcation against the CircleCI API to check on workflow status. (see: https://circleci.com/docs/api/v2/index.html#section/Authentication)
1515

1616
---
1717

src/examples/example.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
description: >
2-
Sample example description.
3-
# Provide a use-case based example for using this orb.
4-
# Everything in the `usage` section will be displayed in the orb registry.
5-
# Comments are not retained.
2+
Use when you want to block a workflow progression until there is only one running.
63
usage:
74
version: 2.1
85
orbs:
9-
<orb-name>: <namespace>/<orb-name>@1.2.3
6+
workflow-queue: promiseofcake/workflow-queue@1
107
workflows:
11-
use-my-orb:
8+
example:
129
jobs:
13-
- <orb-name>/<job-name>
10+
- workflow-queue/queue:
11+
name: workflow-queue
12+
context: <context-key>
13+
circleci-api-key: <CIRCLECI_API_TOKEN>
14+
only-on-branch: main

src/scripts/queue.sh

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/bin/bash
22

3+
tmp="/tmp"
4+
pipeline_file="${tmp}/pipeline_status.json"
5+
workflows_file="${tmp}/workflow_status.json"
6+
7+
# logger command for debugging
38
debug() {
49
if [ "${CONFIG_DEBUG_ENABLED}" == "1" ]; then
510
echo "DEBUG: ${*}"
611
fi
712
}
813

9-
tmp="/tmp"
10-
pipeline_file="${tmp}/pipeline_status.json"
11-
workflows_file="${tmp}/workflow_status.json"
12-
14+
# ensure we have the required variables present to execute
1315
load_variables(){
1416
# just confirm our required variables are present
1517
: "${CIRCLE_WORKFLOW_ID:?"Required Env Variable not found!"}"
@@ -18,30 +20,33 @@ load_variables(){
1820
: "${CIRCLE_REPOSITORY_URL:?"Required Env Variable not found!"}"
1921
: "${CIRCLE_JOB:?"Required Env Variable not found!"}"
2022
# Only needed for private projects
21-
if [ -z "${CIRCLECI_USER_AUTH}" ]; then
22-
echo "CIRCLECI_USER_AUTH not set. Private projects will be inaccessible."
23+
if [ -z "${CIRCLECI_TOKEN}" ]; then
24+
echo "CIRCLECI_TOKEN not set. Private projects will be inaccessible."
2325
else
2426
fetch "https://circleci.com/api/v2/me" "/tmp/me.cci"
2527
me=$(jq -e '.id' /tmp/me.cci)
2628
echo "Using API key for user: ${me}"
2729
fi
2830
}
2931

32+
# helper function to perform HTTP requests via curl
3033
fetch(){
31-
debug "Making API Call to ${1}"
3234
url=$1
3335
target=$2
34-
debug "API CALL ${url}"
35-
http_response=$(curl -s -X GET -H "Authorization: Basic ${CIRCLECI_USER_AUTH}" -H "Content-Type: application/json" -o "${target}" -w "%{http_code}" "${url}")
36+
method=${3:-GET}
37+
debug "Performing API ${method} Call to ${url} to ${target}"
38+
39+
http_response=$(curl -s -X "${method}" -H "Circle-Token: ${CIRCLECI_TOKEN}" -H "Content-Type: application/json" -o "${target}" -w "%{http_code}" "${url}")
3640
if [ "${http_response}" != "200" ]; then
37-
echo "ERROR: Server returned error code: $http_response"
41+
echo "ERROR: Server returned error code: ${http_response}"
3842
debug "${target}"
3943
exit 1
4044
else
4145
debug "API Success"
4246
fi
4347
}
4448

49+
# fetch all current pipelines for a given branch
4550
fetch_pipelines(){
4651
: "${CIRCLE_BRANCH:?"Required Env Variable not found!"}"
4752
echo "Only blocking execution if running previous workflows on branch: ${CIRCLE_BRANCH}"
@@ -51,6 +56,7 @@ fetch_pipelines(){
5156
fetch "${pipelines_api_url_template}" "${pipeline_file}"
5257
}
5358

59+
# fetch all running or created workflows for a given pipeline
5460
fetch_pipeline_workflows(){
5561
for pipeline in $(jq -r ".items[] | .id //empty" ${pipeline_file} | uniq)
5662
do
@@ -63,11 +69,13 @@ fetch_pipeline_workflows(){
6369
jq -s '[.[].items[] | select((.status == "running") or (.status == "created"))]' ${tmp}/pipeline-*.json > ${workflows_file}
6470
}
6571

72+
# parse workflows to fetch parmeters about this current running workflow
6673
load_current_workflow_values(){
6774
my_commit_time=$(jq ".[] | select (.id == \"${CIRCLE_WORKFLOW_ID}\").created_at" ${workflows_file})
6875
my_workflow_id=$(jq ".[] | select (.id == \"${CIRCLE_WORKFLOW_ID}\").id" ${workflows_file})
6976
}
7077

78+
# load all the data necessary to compare build executions
7179
update_comparables(){
7280
fetch_pipelines
7381

@@ -86,20 +94,22 @@ update_comparables(){
8694
debug "Oldest workflow: ${oldest_running_workflow_id}"
8795
}
8896

97+
# will perform a cancel request for the workflow in question
8998
cancel_current_workflow(){
9099
echo "Cancelleing workflow ${my_workflow_id}"
91-
cancel_api_url_template="https://circleci.com/api/v2/workflow/${my_workflow_id}"
92-
curl -s -X POST -H "Authorization: Basic ${CIRCLECI_USER_AUTH}" -H "Content-Type: application/json" "${cancel_api_url_template}" > /dev/null
100+
fetch "https://circleci.com/api/v2/workflow/${my_workflow_id}" "${tmp}/cancel-workflow-${my_workflow_id}.out" "POST"
93101
}
94102

103+
104+
# main execution
105+
# set values that wont change while we wait
95106
if [ "${CONFIG_ONLY_ON_BRANCH}" = "*" ] || [ "${CONFIG_ONLY_ON_BRANCH}" = "${CIRCLE_BRANCH}" ]; then
96107
echo "${CIRCLE_BRANCH} queueable"
97108
else
98109
echo "Queueing only happens on ${CONFIG_ONLY_ON_BRANCH} branch, skipping queue"
99110
exit 0
100111
fi
101112

102-
### Set values that wont change while we wait
103113
load_variables
104114
max_time=${CONFIG_TIME}
105115
echo "This build will block until all previous builds complete."
@@ -108,7 +118,7 @@ wait_time=0
108118
loop_time=11
109119
max_time_seconds=$((max_time * 60))
110120

111-
### Queue Loop
121+
# queue loop
112122
confidence=0
113123
while true; do
114124
update_comparables

0 commit comments

Comments
 (0)