-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Build: skip build based on commands' exit codes #9649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
abc767e
cd91bf5
7c110db
f67209c
ced9655
b0c7e6f
fc2dfa8
f9eecb4
0ca2fc6
428f45d
6ba8aae
111ee35
a41cbf2
dc79701
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ There are some caveats to knowing when using user-defined jobs: | |
* Environment variables are expanded in the commands (see :doc:`environment-variables`) | ||
* Each command is executed in a new shell process, so modifications done to the shell environment do not persist between commands | ||
* Any command returning non-zero exit code will cause the build to fail immediately | ||
(note there is a special exit code to `cancel the build <cancel-build-based-on-a-condition>`_) | ||
* ``build.os`` and ``build.tools`` are required when using ``build.jobs`` | ||
|
||
|
||
|
@@ -104,6 +105,70 @@ To avoid this, it's possible to unshallow the clone done by Read the Docs: | |
- git fetch --unshallow | ||
|
||
|
||
Cancel build based on a condition | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
When a command exits with code ``183``, | ||
Read the Docs will cancel the build immediately. | ||
You can use this approach to cancel builds that you don't want to complete based on some conditional logic. | ||
|
||
.. note:: Why 183 was chosen for the exit code? | ||
|
||
It's the word "skip" encoded in ASCII. | ||
Then it's taken the 256 modulo of it because | ||
`the Unix implementation does this automatically <https://tldp.org/LDP/abs/html/exitcodes.html>`_ | ||
for exit codes greater than 255. | ||
|
||
.. code-block:: python | ||
|
||
>>> sum(list('skip'.encode('ascii'))) | ||
439 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Belated comment: Do you still need this explanation now that you no longer use 439 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It explains where the number 183 comes from. Isn't it clear with this note? I'm happy to update it |
||
>>> 439 % 256 | ||
183 | ||
|
||
|
||
Here is an example that cancels builds from pull requests when there are no changes to the ``docs/`` folder compared to the ``origin/main`` branch: | ||
|
||
.. code-block:: yaml | ||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:caption: .readthedocs.yaml | ||
|
||
version: 2 | ||
build: | ||
os: "ubuntu-22.04" | ||
tools: | ||
python: "3.11" | ||
jobs: | ||
post_checkout: | ||
# Cancel building pull requests when there aren't changed in the docs directory. | ||
# `--quiet` exits with a 1 when there **are** changes, | ||
# so we invert the logic with a ! | ||
# | ||
# If there are no changes (exit 0) we force the command to return with 183. | ||
# This is a special exit code on Read the Docs that will cancel the build immediately. | ||
- | | ||
if [ $READTHEDOCS_VERSION_TYPE = "external" ]; | ||
then | ||
! git diff --quiet origin/main -- docs/ && exit 183; | ||
fi | ||
|
||
|
||
This other example shows how to cancel a build if the commit message contains ``skip ci`` on it: | ||
|
||
.. code-block:: yaml | ||
:caption: .readthedocs.yaml | ||
|
||
version: 2 | ||
build: | ||
os: "ubuntu-22.04" | ||
tools: | ||
python: "3.11" | ||
jobs: | ||
post_checkout: | ||
# Use `git log` to check if the latest commit contains "skip ci", | ||
# in that case exit the command with 183 to cancel the build | ||
- (git --no-pager log --pretty="tformat:%s -- %b" -1 | grep -viq "skip ci") || exit 183 | ||
|
||
|
||
Generate documentation from annotated sources with Doxygen | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Doc build constants.""" | ||
|
||
import structlog | ||
import re | ||
|
||
import structlog | ||
from django.conf import settings | ||
|
||
|
||
log = structlog.get_logger(__name__) | ||
|
||
PDF_RE = re.compile('Output written on (.*?)') | ||
|
@@ -30,3 +28,11 @@ | |
DOCKER_OOM_EXIT_CODE = 137 | ||
|
||
DOCKER_HOSTNAME_MAX_LEN = 64 | ||
|
||
# Why 183 exit code? | ||
# | ||
# >>> sum(list('skip'.encode('ascii'))) | ||
# 439 | ||
# >>> 439 % 256 | ||
# 183 | ||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RTD_SKIP_BUILD_EXIT_CODE = 183 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I brought up the point that perhaps we want support for more explicit codes here. If so, perhaps we should choose a less arbitrary value here and allocate a block of exit codes. 183 is in a safe range for posix standards at least though: But, do we want something like:
Not to say we're implementing everything now, but if we will have more than one exit code, I'd suggest starting from a value that doesn't seem so arbitrary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, I checked for this as well and I was happy that "skip" was in that range 😄
This looks interesting, but right now I'm not seeing a clear usage for other useful exit codes. We can keep thinking about this and see if we find some other useful cases. In any case, we can reserve the range 180-185 and start with 183 for this one; there is no need to make them consecutive. |
Uh oh!
There was an error while loading. Please reload this page.