In this documentation it explains that by using [ci skip] or [skip ci] in the commit message that circleci will skip that build.
Can we specify a branch to ignore for example ‘[ci skip staging]’
If that is not possible another user posted some time ago another solution where by checking the commit message for a custom designation to skip the build or stop the job here:
Is this currently possible with the latest version of circleci?
I would like to add a step in my job where by circleci would commit and push to the branch the workflow is running off of at the end of my job. However I don’t want to have circleci build for that branch again for that commit as this would cause a recursion.
I don’t think branch-to-ignore would work as the commit and push would be on a branch that I have set up a filter to specifically trigger a workflow on that branch. In essence it would cause a recursive build process to fire off (merge changes into stage branch -> workflow kickoffs a stage build to testflight and auto update version number -> commit and push version back into stage branch -> workflow kickoffs a stage build…)
Is there a way to stop a workflow (without an error alert) when a commit message contains certain text?
Hi @rankinit! Thanks for clarifying – and yes, I see the conundrum here!
You can certainly parse the commit message, or use the built-in environment variable for “CIRCLE_BRANCH” to stop the step. You can stop the workflow with an exit 1, or since you are trying to avoid that, you can use the API to cancel the currently running workflow .
So for example:
version: 2.1
jobs:
my-test-job:
machine: true
steps:
- checkout
- run: | # this job exits if the branch matches a string
if [ "$CIRCLE_BRANCH" == 'branch-to-ignore' ]; then
exit 1
fi
- run: | # this job cancels the workflow with the API if there is a particular string in the commit message. In this case it looks for [ignore-branch:true]
COMMIT_SUBJECT=`git log -1 --pretty=%s.`
IGNORE_BRANCH=$(echo "${COMMIT_SUBJECT}" | sed -En 's/.*\[ignore-branch:(true)\].*/\1/p')
echo "Commit subject: ${COMMIT_SUBJECT}"
echo "ignore-branch: ${IGNORE_BRANCH}"
if [ "$IGNORE_BRANCH" == 'true' ]; then
curl -X POST "https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}/cancel" \
-H "Accept: application/json" \
-H "Circle-Token: ${CIRCLE_TOKEN}"
fi
another-job:
machine: true
steps:
- run: echo "I should not run"
workflows:
tests:
jobs:
- my-test-job:
context: context
- another-job:
requires:
- my-test-job
Note that in the second step, I passed in my CIRCLE_TOKEN from a context.
There is also, however, an open feature request to cancel a workflow from within a job without using the API: https://ideas.circleci.com/ideas/CCI-I-856. Please do vote for it if this is something that will be useful to you!
I hope that helps! Please ask away if you have additional questions!