Skip to content

Commit

Permalink
[carwow] Set pipeline number as integer
Browse files Browse the repository at this point in the history
Fork of eddiewebb/queue v3 pre-release.

The pipeline values are not passing thru the orb, so they need to be
declared explicitly. Only the pipeline number is really neeed, but it
needs to be of type integer as CircleCI can't cast pipeline values.

https://discuss.circleci.com/t/pipeline-variables-not-valid-in-orbs/37221
https://discuss.circleci.com/t/unable-to-force-pipeline-number-to-be-type-string/40201

Also, set it up so we can publish a forked version within carwow namespace.
  • Loading branch information
PChambino committed Feb 12, 2024
1 parent 4f39420 commit 2a94879
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 229 deletions.
189 changes: 30 additions & 159 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,172 +1,43 @@

version: 2.1

parameters:
orbname:
type: string
default: "queue"
description: Change this to whatever name is correct for your orb.
orbs:
orb-tools: circleci/orb-tools@12

workflows:
build-deploy:
version: 2
build-and-publish:
jobs:
- validate:
- orb-tools/pack:
filters:
branches:
ignore:
- master
- test:
context: [orbs]
requires:
- validate
filters:
branches:
only:
- staging
- trying
- publish:
context: [orbs]
requires:
- test
filters:
branches:
only: staging
only: carwow
tags:
only: /^v[0-9]+\.[0-9]+\.[0-9]+$/

jobs:
validate:
docker:
- image: cimg/base:2019.08
working_directory: ~/repo
steps:
- checkout
- install-circleci
- pack-and-validate
test:
docker:
- image: cimg/base:2019.08
working_directory: ~/repo
steps:
- checkout
- install-circleci
- pack-and-validate
- pr-info
- run:
name: Publish Dev
command: |
PUBLISH_MESSAGE=`circleci orb publish packed.yml eddiewebb/<<pipeline.parameters.orbname>>@dev:<<pipeline.number>> --token ${CIRCLECI_API_KEY}`
ORB_VERSION=$(echo $PUBLISH_MESSAGE | sed -n 's/Orb `\(.*\)` was published.*/\1/p')
echo "export ORB_VERSION=\"${ORB_VERSION}\"" >> $BASH_ENV
echo $ORB_VERSION
echo "export PR_MESSAGE=\"BotComment: *Development* version of orb available for manual validation - \\\`${ORB_VERSION}\\\`\"" >> $BASH_ENV
- install-bats
- run:
name: Import Tests using BATS
command: |
export BATS_IMPORT_DEV_ORB="eddiewebb/<<pipeline.parameters.orbname>>@dev:<<pipeline.number>>"
bats --jobs 8 test
- pr-comment
- run:
name: Check Semver
command: |
if [ "$PR_NUMBER" == "" ];then
echo "No pr found, do nothing"
exit 0
fi
TITLE=`curl -u eddiewebb:${GHI_TOKEN} "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${PR_NUMBER}" | jq '.title' `
SEMVER_INCREMENT=`echo $TITLE | sed -En 's/.*\[semver:(major|minor|patch|skip)\].*/\1/p'`
if [ -z ${SEMVER_INCREMENT} ];then
echo "Merge commit did not indicate which SemVer increment to make. Please ammend commit with [semver:FOO] where FOO is major, minor, or patch"
exit 1
fi
- orb-tools/publish:
name: orb-tools/publish-dev
context: org-global
orb_name: carwow/queue
vcs_type: <<pipeline.project.type>>

publish:
docker:
- image: cimg/base:2024.01
working_directory: ~/repo
steps:
- checkout
- install-circleci
- pr-info
- when:
condition:
or:
- equal: [ trying, << pipeline.git.branch >> ]
- equal: [ staging, << pipeline.git.branch >> ]
steps:
- run:
name: Promote to prod
command: |
SEMVER_INCREMENT=`git log -1 --pretty=%B | sed -En 's/.*\[semver:(major|minor|patch|skip)\].*/\1/p'`
if [ -z ${SEMVER_INCREMENT} ];then
echo "Merge commit did not indicate which SemVer increment to make. Please ammend commit with [semver:FOO] where FOO is major, minor, or patch"
exit 1
elif [ "$SEMVER_INCREMENT" == "skip" ];then
echo "SEMVER in commit indicated to skip orb release"
echo "export PR_MESSAGE=\"Orb publish was skipped due to [semver:skip] in commit message.\"" >> $BASH_ENV
exit 0
else
PUBLISH_MESSAGE=`circleci orb publish promote eddiewebb/<<pipeline.parameters.orbname>>@dev:<<pipeline.number>> ${SEMVER_INCREMENT} --token ${CIRCLECI_API_KEY}`
echo $PUBLISH_MESSAGE
ORB_VERSION=$(echo $PUBLISH_MESSAGE | sed -n 's/Orb .* was promoted to `\(.*\)`.*/\1/p')
echo "export PR_MESSAGE=\"BotComment: *Production* version of orb available for use - \\\`${ORB_VERSION}\\\`\"" >> $BASH_ENV
fi
- pr-comment
requires:
- orb-tools/pack

filters:
branches:
only: carwow

- orb-tools/publish:
context: org-global
orb_name: carwow/queue
vcs_type: <<pipeline.project.type>>
pub_type: production

requires:
- orb-tools/pack

commands:
install-bats:
description: installs the BATS bash testing tool
steps:
- run:
name: Install BATS (bash testing)
command: |
cd /tmp && git clone https://github.com/bats-core/bats-core.git && cd bats-core
./install.sh /usr/local
- run:
name: Install YQ
command: |
curl -L https://github.com/mikefarah/yq/releases/download/v4.14.1/yq_linux_amd64 -o yq
chmod a+x yq
mv yq /usr/local/bin/
install-circleci:
description: installs the new CIrcleCI CLI with orb support
steps:
- run:
name: Install CircleCI CLI (the new one)
command: |
#curl https://raw.githubusercontent.com/CircleCI-Public/circleci-cli/master/install.sh --fail --show-error | bash
circleci update
circleci version
echo "Run circleci help"
circleci help
echo -e "token: ${CIRCLECI_API_KEY}\nverbose: false" > ~/.circleci/cli.yml
pack-and-validate:
description: pack directory to single file and validate
steps:
- run:
name: Pack and Validate
command: |
circleci orb pack src > packed.yml
circleci orb validate packed.yml
pr-comment:
description: add message to pr this build originated from
steps:
- run:
name: Publish Version to PR
command: |
if [ "$PR_NUMBER" == "" ];then
echo "No pr found, do nothing"
exit 0
fi
curl -X POST -u eddiewebb:${GHI_TOKEN} "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${PR_NUMBER}/comments" -d "{\"body\":\"${PR_MESSAGE}\"}"
pr-info:
description: get PR number this change originated from
steps:
- run:
name: Get PR Info
command: |
PR_NUMBER=`git log -1 --pretty=%s | sed -En 's/(Try|Merge) #([0-9]*).*/\2/p'`
echo "PR_NUMBER is ${PR_NUMBER}"
echo "export PR_NUMBER=${PR_NUMBER}" >> $BASH_ENV
filters:
branches:
ignore: /.*/
tags:
only: /^v[0-9]+\.[0-9]+\.[0-9]+$/
74 changes: 22 additions & 52 deletions src/@orb.yml
Original file line number Diff line number Diff line change
@@ -1,70 +1,40 @@
version: 2.1
description: |
Allows jobs or entire workflows to be queued to ensure they run in serial.
Allows jobs or entire workflows to be queued to ensure they run in serial.
This is ideal for deployments or other activities that must not run concurrently.
May optionaly consider branch-level isolation if unique branches should run concurrently.
This orb requires the project to have an **Personal** API key in order to query build states.
It requires a single environment variable CIRCLECI_API_KEY which can be created in account settings - https://circleci.com/account/api.
3.0.0: [BREAKING CHANGE] Use pipeline.number as authoritative comparison. Breaks many parameters.
2.2.2: Docs clarity on token needs (@davidjb)
2.2.1: fixes release version bug
2.2.0: Adds 'filter-by-workflow' (@soniqua)
2.0.0: Breaking change fixes dyanamic config, but may break Bitbucket users.
1.12.0: Adds Server Support (@nanophate)
1.9.0: Doc update
1.8.4: Adds urlencode for branch names. (@andrew-barnett)
1.8.1: Adds content-type header to API calls (@kevinr-electric) and prints message on error (@AlexMeuer)
1.8.0: minor fix same as version 1.8.0 (missing docs)
1.7.1: patch fix same as version 1.8.1 to catch folsk who dont update
1.7.0: adds regexp for job names, collab with @jonesie100
1.6.5: docs contribution by @pwilczynskiclearcode
1.6.4: support slashes in Tags, thanks @dunial
1.6.3: addresses API changes that broke branch-job queueing, adds more API checks
1.6.1: fixes issue in tag matching , thanks @calvin-summer
1.6.0: Support Tags, thanks @nikolaik, @dunial
1.5.0: API variables name as parameter , thanks @philnielson
1.4.4: Docs improvements, thanks @jordan-brough
1.4.3: more confident confidence thanks @GreshamDanielStephens
1.4.2: Doc improvements, thanks @olleolleolle
1.4.1: fixes bug in block-workflow as job. thanks @mu-bro
1.4.0: Adds confidence checks to avoid race condition
1.3.0: use small resource class in job
Fork of eddiewebb/queue v3 pre-release.
display:
home_url: https://eddiewebb.github.io/circleci-queue/
source_url: https://github.com/eddiewebb/circleci-queue
source_url: https://github.com/carwow/circleci-queue

examples:
queue_workflow:
description: Used typically as first job and will queue until no previous workflows are running
usage:
version: 2.1
orbs:
queue: eddiewebb/queue@volatile
queue: carwow/queue@1

workflows:
build_deploy:
jobs:
- queue/block_workflow:
max-wait-time: "10" # max wait, in minutes (default 10)
limit-branch-name: main # restrict queueing to a specific branch (default *)
circleci-api-key: CIRCLE_TOKEN
my-pipeline: <<pipeline.number>>
- some_other_job:
requires:
- queue/block_workflow

single_concurrency_job:
description: |
Used to ensure that a only single job (deploy) is not run concurrently.
By default will only queue if the same job from previous worfklows is running on the same branch.
Used to ensure that a only single job (deploy) is not run concurrently.
By default will only queue if the same job from previous worfklows is running on the same branch.
This allows safe jobs like build/test to overlap, minimizing overall queue times.
usage:
version: 2.1
orbs:
queue: eddiewebb/queue@volatile
queue: carwow/queue@1

workflows:
build_deploy:
Expand All @@ -76,25 +46,25 @@ examples:
jobs:
build:
docker:
- image: circleci/node:10
- image: circleci/ruby
steps:
- run: echo "This job can overlap"

deploy:
docker:
- image: circleci/node:10
- image: circleci/ruby
steps:
- queue/until_front_of_line:
max-wait-time: "10" # max wait, in minutes (default 10)
limit-branch-name: main # restrict queueing to a specific branch (default *)
circleci-api-key: CIRCLE_TOKEN
my-pipeline: <<pipeline.number>>
- run: echo "This job will not overlap"

multiple_job_names_regexp:
description: Use regexp-jobname when you have multiple jobs to block order of.
usage:
version: 2.1
orbs:
queue: eddiewebb/queue@volatile
queue: carwow/queue@1

workflows:
build_deploy:
Expand All @@ -109,23 +79,23 @@ examples:
jobs:
build:
docker:
- image: circleci/node:10
- image: circleci/ruby
steps:
- run: echo "This job can overlap"

DeployStep1:
docker:
- image: circleci/node:10
- image: circleci/ruby
steps:
- queue/until_front_of_line:
max-wait-time: "10" # max wait, in minutes (default 10)
limit-branch-name: main # restrict queueing to a specific branch (default *)
job-regex: "^DeployStep[0-9]$" #use extendex regexp pattern
circleci-api-key: CIRCLE_TOKEN
my-pipeline: <<pipeline.number>>
job-regex: "^DeployStep[0-9]$"
- run: echo "This job will not overlap with itself or next similar nameds job"

DeployStep2:
docker:
- image: circleci/node:10
- image: circleci/ruby
steps:
- run: echo "This job will block step1 on any further workflows"

21 changes: 5 additions & 16 deletions src/commands/until_front_of_line.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,18 @@ parameters:
default: "circleci.com"
description: "For server user to specifiy custom hostname for their server"

#
# These a;l pass through from pipeline, but orb validator wont let us use them inline :(
#
my-pipeline:
type: string
default: "<<pipeline.number>>"
trigger-source:
type: string
default: "<<pipeline.trigger_source>>"
vcs-type:
type: string
default: "<<pipeline.project.type>>"
my-branch:
type: string
default: "<<pipeline.git.branch>>"
default: github
my-pipeline:
type: integer
description: "Set as: <<pipeline.number>>"

steps:
- run:
name: Queue Until Front of Line
command: <<include(../scripts/loop.bash)>>
environment:
environment:
ONLY_ON_BRANCH: <<parameters.limit-branch-name>>
BLOCK_WORKFLOW: <<parameters.block-workflow>>
MAX_TIME: '<<parameters.max-wait-time>>'
Expand All @@ -84,6 +75,4 @@ steps:
JOB_REGEXP: <<parameters.job-regex>>
CIRCLECI_BASE_URL: https://<<parameters.circleci-hostname>>
MY_PIPELINE_NUMBER: <<parameters.my-pipeline>>
TRIGGER_SOURCE: <<parameters.trigger-source>>
VCS_TYPE: <<parameters.vcs-type>>
MY_BRANCH: <<parameters.my-branch>>
Loading

0 comments on commit 2a94879

Please sign in to comment.