-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init orb and scripts * empty * fix: typo * feat: echo workflow api result * fix: use mapfile to pass lint
- Loading branch information
1 parent
8e9fdbf
commit 439614c
Showing
10 changed files
with
106 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
version: 2.1 | ||
|
||
description: > | ||
Sample orb description | ||
# What will your orb allow users to accomplish? | ||
# Descriptions should be short, simple, and informative. | ||
description: Cancel redundant build for every workflow on default branch for CircleCI. | ||
|
||
# This information will be displayed in the orb registry and is not mandatory. | ||
display: | ||
home_url: "https://www.example.com/docs" | ||
source_url: "https://github.com/<organization>/<project-name>" | ||
|
||
# If your orb requires other orbs, you can import them like this. Otherwise remove the "orbs" stanza. | ||
# orbs: | ||
# hello: circleci/hello-build@0.0.5 | ||
home_url: https://corp.moneyforward.com/ | ||
source_url: https://github.com/moneyforward/circleci-cancel-redundant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
description: > | ||
This command cancels duplicate workflow on the default branch using the CircleCI API. | ||
parameters: | ||
circle_api_key: | ||
type: string | ||
default_branch: | ||
default: main | ||
type: string | ||
pipeline_id: | ||
type: string | ||
steps: | ||
- run: | ||
name: "Cancel duplicate workflow on default branch" | ||
environment: | ||
- CIRCLE_API_KEY: << parameters.circle_api_key >> | ||
- DEFAULT_BRANCH: << parameters.default_branch >> | ||
- PIPELINE_ID: << parameters.pipeline_id >> | ||
command: <<include(scripts/cancel.sh)>> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
description: > | ||
This is a sample executor using Docker and Node. If you want to provide a custom environment in your orb, insert your image here. | ||
If you do not require an executor, you can simply delete this directory. | ||
CircleCI base executor. | ||
docker: | ||
- image: 'cimg/node:<<parameters.tag>>' | ||
- image: "cimg/base:<<parameters.tag>>" | ||
parameters: | ||
tag: | ||
default: lts | ||
default: stable | ||
description: > | ||
Pick a specific cimg/node image variant: | ||
https://hub.docker.com/r/cimg/node/tags | ||
Pick a specific cimg/base image variant: | ||
https://hub.docker.com/r/cimg/base/tags | ||
type: string |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/bash | ||
|
||
if [ "$CIRCLE_BRANCH" != "$DEFAULT_BRANCH" ]; then | ||
echo 'Not on default branch, no need to manually cancel duplicate workflow' | ||
exit | ||
fi | ||
|
||
CIRCLE_API_KEY=$(eval echo "$CIRCLE_API_KEY") | ||
|
||
# Get the name of the workflows in the pipeline | ||
WF_NAMES=$(curl --header "Circle-Token: $CIRCLE_API_KEY" --request GET \ | ||
"https://circleci.com/api/v2/pipeline/${PIPELINE_ID}/workflow" | jq -r '.items[].name') | ||
echo "Workflow(s) in pipeline:" | ||
echo "$WF_NAMES" | ||
|
||
## Get the IDs of pipelines created with the the current CIRCLE_SHA1 | ||
PIPE_IDS=$(curl --header "Circle-Token: $CIRCLE_API_KEY" --request GET \ | ||
"https://circleci.com/api/v2/project/gh/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pipeline?branch=$CIRCLE_BRANCH" \ | ||
| jq -r --arg CIRCLE_SHA1 "${CIRCLE_SHA1}" \ | ||
' .items[]|select(.state == "created" and .vcs.revision == $CIRCLE_SHA1).id') | ||
|
||
## Get the IDs of currently running/on_hold workflows with the same name except the current workflow ID | ||
if [ -n "$PIPE_IDS" ]; then | ||
mapfile -t PIPE_LIST <<< "$PIPE_IDS" # Convert space separated values into array | ||
for PIPE_ID in "${PIPE_LIST[@]:1}" # Skip the first PIPE_ID, since it's the latest one | ||
do | ||
result=$(curl --header "Circle-Token: $CIRCLE_API_KEY" --request GET "https://circleci.com/api/v2/pipeline/${PIPE_ID}/workflow") | ||
# Cancel every workflow | ||
for WF_NAME in $WF_NAMES | ||
do | ||
echo "$result" \ | ||
| jq -r \ | ||
--arg PIPELINE_ID "${PIPELINE_ID}" \ | ||
--arg WF_NAME "${WF_NAME}" \ | ||
'.items[]|select(.status == "on_hold" or .status == "running")|select(.name == $WF_NAME)|select(.pipeline_id != $PIPELINE_ID)|.id' \ | ||
>> WF_to_cancel.txt | ||
done | ||
done | ||
fi | ||
|
||
## Cancel any currently running/on_hold workflow with the same name | ||
if [ -s WF_to_cancel.txt ]; then | ||
echo "Canceling the following workflow(s):" | ||
cat WF_to_cancel.txt | ||
while read -r WF_ID; | ||
do | ||
curl --header "Circle-Token: $CIRCLE_API_KEY" --request POST https://circleci.com/api/v2/workflow/"$WF_ID"/cancel | ||
done < WF_to_cancel.txt | ||
## Allowing some time to complete the cancellation | ||
sleep 2 | ||
else | ||
echo "Nothing to cancel" | ||
fi | ||
|
||
rm -f WF_to_cancel.txt |
This file was deleted.
Oops, something went wrong.