Skip to content

Commit

Permalink
Init Orb and Add Script (#1)
Browse files Browse the repository at this point in the history
* feat: init orb and scripts

* empty

* fix: typo

* feat: echo workflow api result

* fix: use mapfile to pass lint
  • Loading branch information
littlemight authored Sep 5, 2022
1 parent 8e9fdbf commit 439614c
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 56 deletions.
5 changes: 3 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ workflows:
- orb-tools/pack:
filters: *filters
- orb-tools/review:
exclude: RC005,RC006,RC007
filters: *filters
- shellcheck/check:
filters: *filters
- orb-tools/publish:
orb-name: <namespace>/<orb-name>
orb-name: moneyforward/circleci-cancel-redundant
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: <publishing-context>
context: orb-publishing
filters: *filters
# Triggers the next workflow in the Orb Development Kit.
- orb-tools/continue:
Expand Down
18 changes: 14 additions & 4 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2.1
orbs:
<orb-name>: <namespace>/<orb-name>@dev:<<pipeline.git.revision>>
circleci-cancel-redundant: moneyforward/circleci-cancel-redundant@dev:<<pipeline.git.revision>>
orb-tools: circleci/orb-tools@11.1

filters: &filters
Expand All @@ -16,7 +16,17 @@ jobs:
steps:
- checkout
# Run your orb's commands to validate them.
- <orb-name>/greet
- circleci-cancel-redundant/cancel:
circle_api_key: $CIRCLE_TOKEN
default_branch: main
pipeline_id: << pipeline.id >>
- run:
name: Sleep and echo
command: |
echo "Waking up..."
sleep 5
echo "Hello world!"
workflows:
test-deploy:
jobs:
Expand All @@ -26,13 +36,13 @@ workflows:
- orb-tools/pack:
filters: *filters
- orb-tools/publish:
orb-name: <namespace>/<orb-name>
orb-name: moneyforward/circleci-cancel-redundant
vcs-type: << pipeline.project.type >>
pub-type: production
requires:
- orb-tools/pack
- command-tests
context: <publishing-context>
context: orb-publishing
filters:
branches:
ignore: /.*/
Expand Down
14 changes: 3 additions & 11 deletions src/@orb.yml
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
19 changes: 19 additions & 0 deletions src/commands/cancel.yml
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)>>
15 changes: 0 additions & 15 deletions src/commands/greet.yml

This file was deleted.

9 changes: 7 additions & 2 deletions src/examples/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ description: >
usage:
version: 2.1
orbs:
<orb-name>: <namespace>/<orb-name>@1.2.3
circleci-cancel-redundant: moneyforward/circleci-cancel-redundant@1.2.3
workflows:
use-my-orb:
jobs:
- <orb-name>/<job-name>
- some-job:
pre-steps:
- circleci-cancel-redundant/cancel:
circle_api_key: "your-api-key"
default_branch: main
pipeline_id: << pipeline.id >>
11 changes: 5 additions & 6 deletions src/executors/default.yml
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
14 changes: 0 additions & 14 deletions src/jobs/hello.yml

This file was deleted.

55 changes: 55 additions & 0 deletions src/scripts/cancel.sh
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
2 changes: 0 additions & 2 deletions src/scripts/greet.sh

This file was deleted.

0 comments on commit 439614c

Please sign in to comment.