Tutorial¶
demo¶
The demo package shows some examples of how to use circleci.py
copyright: |
|
---|---|
license: | MIT, see LICENSE for more details. |
demo.sdk¶
This module shows some examples of how to use the circleci.py sdk
demo.sdk.build_singleton¶
Demonstrate how to use the circleci.py SDK to make builds run one at a time.
The purpose of this script is to be executed early in a CircleCI job. It will check for running builds, if it finds any it will pause execution and poll at a 15 second interval. Once other jobs have finished it will continue execution. If no jobs are found, it will start execution.
1 2 3 4 5 6 7 8 9 10 11 12 | import os
from circleci.api import Api
from circleci.sdk import SDK
ORG = "levlaz"
REPO = "circleci-sandbox"
circleci = Api(os.environ.get("CIRCLE_TOKEN"))
sdk = SDK(circleci)
if __name__ == "__main__":
sdk.build_singleton(ORG, REPO)
|
- Usage
Make sure a valid
CIRCLE_TOKEN
is set as an environment variable.Replace
ORG
andREPO
with your own values.ORG
can be either a username or an org name.If you are using bitbucket you should add the
vcs_type
argument tobuild_singleton
.sdk.build_singleton(ORG, REPO, vcs_type='bitbucket')
Within a CircleCI job you can do something like this in order to execute this
script. Assuming you have this build_singleton.py
script checked into a
directory called scripts
in your code repository.
1 2 3 4 5 | - run:
name: Build Singleton
command: |
sudo pip install circleci
python scripts/build_singleton.py
|
If builds are running you will see the following output in CircleCI:
found running builds, sleeping for 15 seconds.
['https://circleci.com/gh/levlaz/circleci-sandbox/1148', 'https://circleci.com/gh/levlaz/circleci-sandbox/1147', 'https://circleci.com/gh/levlaz/circleci-sandbox/1146']
found running builds, sleeping for 15 seconds.
['https://circleci.com/gh/levlaz/circleci-sandbox/1146']
found running builds, sleeping for 15 seconds.
['https://circleci.com/gh/levlaz/circleci-sandbox/1146']
Once no more jobs are found, the job will begin to execute and you will see the following output in CircleCI:
no running builds found, beginning execution.