Skip to content

Commit

Permalink
Release Updates (#8)
Browse files Browse the repository at this point in the history
* feat: try to replace user_auth with token for simplicity

* feat: update orb version

* rebuild

* feat: more updates

* fix: example formatting
  • Loading branch information
promiseofcake authored Jan 5, 2023
1 parent 7f585d5 commit bbf2495
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
4 changes: 1 addition & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1
setup: true
orbs:
orb-tools: circleci/orb-tools@11.1
orb-tools: circleci/orb-tools@11.6
shellcheck: circleci/[email protected]

filters: &filters
Expand All @@ -25,10 +25,8 @@ workflows:
vcs-type: << pipeline.project.type >>
requires:
[orb-tools/lint, orb-tools/review, orb-tools/pack, shellcheck/check]
# Use a context to hold your publishing token.
context: orb-publishing
filters: *filters
# Triggers the next workflow in the Orb Development Kit.
- orb-tools/continue:
pipeline-number: << pipeline.number >>
vcs-type: << pipeline.project.type >>
Expand Down
2 changes: 1 addition & 1 deletion .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1
orbs:
workflow-queue: promiseofcake/workflow-queue@dev:<<pipeline.git.revision>>
orb-tools: circleci/orb-tools@11.1
orb-tools: circleci/orb-tools@11.6

filters: &filters
tags:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

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

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.
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.

## Configuration Requirements

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)
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)

---

Expand Down
15 changes: 8 additions & 7 deletions src/examples/example.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
description: >
Sample example description.
# Provide a use-case based example for using this orb.
# Everything in the `usage` section will be displayed in the orb registry.
# Comments are not retained.
Use when you want to block a workflow progression until there is only one running.
usage:
version: 2.1
orbs:
<orb-name>: <namespace>/<orb-name>@1.2.3
workflow-queue: promiseofcake/workflow-queue@1
workflows:
use-my-orb:
example:
jobs:
- <orb-name>/<job-name>
- workflow-queue/queue:
name: workflow-queue
context: <context-key>
circleci-api-key: <CIRCLECI_API_TOKEN>
only-on-branch: main
38 changes: 24 additions & 14 deletions src/scripts/queue.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/bin/bash

tmp="/tmp"
pipeline_file="${tmp}/pipeline_status.json"
workflows_file="${tmp}/workflow_status.json"

# logger command for debugging
debug() {
if [ "${CONFIG_DEBUG_ENABLED}" == "1" ]; then
echo "DEBUG: ${*}"
fi
}

tmp="/tmp"
pipeline_file="${tmp}/pipeline_status.json"
workflows_file="${tmp}/workflow_status.json"

# ensure we have the required variables present to execute
load_variables(){
# just confirm our required variables are present
: "${CIRCLE_WORKFLOW_ID:?"Required Env Variable not found!"}"
Expand All @@ -18,30 +20,33 @@ load_variables(){
: "${CIRCLE_REPOSITORY_URL:?"Required Env Variable not found!"}"
: "${CIRCLE_JOB:?"Required Env Variable not found!"}"
# Only needed for private projects
if [ -z "${CIRCLECI_USER_AUTH}" ]; then
echo "CIRCLECI_USER_AUTH not set. Private projects will be inaccessible."
if [ -z "${CIRCLECI_TOKEN}" ]; then
echo "CIRCLECI_TOKEN not set. Private projects will be inaccessible."
else
fetch "https://circleci.com/api/v2/me" "/tmp/me.cci"
me=$(jq -e '.id' /tmp/me.cci)
echo "Using API key for user: ${me}"
fi
}

# helper function to perform HTTP requests via curl
fetch(){
debug "Making API Call to ${1}"
url=$1
target=$2
debug "API CALL ${url}"
http_response=$(curl -s -X GET -H "Authorization: Basic ${CIRCLECI_USER_AUTH}" -H "Content-Type: application/json" -o "${target}" -w "%{http_code}" "${url}")
method=${3:-GET}
debug "Performing API ${method} Call to ${url} to ${target}"

http_response=$(curl -s -X "${method}" -H "Circle-Token: ${CIRCLECI_TOKEN}" -H "Content-Type: application/json" -o "${target}" -w "%{http_code}" "${url}")
if [ "${http_response}" != "200" ]; then
echo "ERROR: Server returned error code: $http_response"
echo "ERROR: Server returned error code: ${http_response}"
debug "${target}"
exit 1
else
debug "API Success"
fi
}

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

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

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

# load all the data necessary to compare build executions
update_comparables(){
fetch_pipelines

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

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


# main execution
# set values that wont change while we wait
if [ "${CONFIG_ONLY_ON_BRANCH}" = "*" ] || [ "${CONFIG_ONLY_ON_BRANCH}" = "${CIRCLE_BRANCH}" ]; then
echo "${CIRCLE_BRANCH} queueable"
else
echo "Queueing only happens on ${CONFIG_ONLY_ON_BRANCH} branch, skipping queue"
exit 0
fi

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

### Queue Loop
# queue loop
confidence=0
while true; do
update_comparables
Expand Down

0 comments on commit bbf2495

Please sign in to comment.