Skip to content

Instantly share code, notes, and snippets.

@josephmancuso
Last active August 9, 2021 17:16
Show Gist options
  • Save josephmancuso/c5e1ff7458a511529d208a5182d19de6 to your computer and use it in GitHub Desktop.
Save josephmancuso/c5e1ff7458a511529d208a5182d19de6 to your computer and use it in GitHub Desktop.
Python script to execute cicle CI jobs
version: 2
jobs:
build:
docker:
- image: circleci/python:3.6 # primary container for the build job
steps:
- checkout
- run: make
- run: make ci
python3.4:
docker:
- image: circleci/python:3.4
steps:
- checkout
- run: make
- run: make ci
python3.5:
docker:
- image: circleci/python:3.5
steps:
- checkout
- run: make
- run: make ci
python3.7:
docker:
- image: circleci/python:3.7
steps:
- checkout
- run: make
- run: make ci
Masonite Validation:
docker:
- image: circleci/python:3.6
steps:
- checkout
- run: python trigger_build.py --repo masoniteframework/validation --branch circle-ci --build MASONITE_DEPENDENT_BRANCH=$CIRCLE_BRANCH
workflows:
version: 2
build_and_test:
jobs:
- build
- python3.4
- python3.5
- python3.7
- Masonite Validation:
requires:
- build
"""
This is a script to easily execute circle CI jobs from other repository builds.
Useful if you have a dependent build that you need to run to ensure the latest code is up to date with the parent build.
This does not work for PR forks because a Circle Access token is required which is insecure for forks.
This script will:
- Fire a build in another circle CI repository
- Loop and call the build_url to continuously get the status
- Wait until the job finishes
- If the job fails this script will exit 1 which will fail the job.
- Else it will exit 0 which will pass the job.
**You will need to download this script and put it in the base of your repository**
Usage:
- python trigger_build.py --repo user/repo --branch branch_name --token CIRCLE_TOKEN --build ENV_VARIABLE=value --build ENV_VARIABLE2=value2
Example:
- python trigger_build.py --repo masoniteframework/validation --branch circle-ci --token $CIRCLE_TOKEN --build MASONITE_BRANCH=$CIRCLE_BRANCH
NOTE: You can get your CircleCI access token via your dashboard. If no token is specified it will use the CIRCLE_TOKEN in your job dashboard.
So if you don't want to pass in the token then put it in the environment variable in your job's dashboard.
Example config:
version: 2
jobs:
Masonite Validation:
docker:
- image: circleci/python:3.6
steps:
- checkout
- run: python trigger_build.py --repo masoniteframework/validation --branch circle-ci --build MASONITE_DEPENDENT_BRANCH=$CIRCLE_BRANCH
"""
import requests
import time
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--repo", help="Repository name")
parser.add_argument("-b", "--branch", help="Branch name")
parser.add_argument("-t", "--token", help="Circle CI Token")
parser.add_argument('-a', '--build', action='append', help='Set Build Arguments')
parser.add_argument('-p', '--poll', help='How long the script should sleep before checking status')
args = parser.parse_args()
repo = args.repo
branch = args.branch or 'master'
token = args.token or os.getenv('CIRCLE_TOKEN')
current_repo = os.getenv('CIRCLE_PROJECT_REPONAME')
current_user = os.getenv('CIRCLE_PROJECT_USERNAME')
poll = args.poll or 5
if os.getenv('CIRCLE_PR_NUMBER'):
print('Cannot Build On PR Forks.')
exit(0)
if not token:
print('No token found.')
exit(1)
parameters = {}
for argument in args.build or []:
if not '=' in argument:
print("ERROR: '--build' argument must contain a '=' sign. Got '{}'".format(argument))
exit(1)
key = argument.split('=')[0]
value = argument.split('=')[1]
parameters.update({key: value})
build_parameters = {'build_parameters': parameters}
r = requests.post('https://circleci.com/api/v1/project/{}/tree/{}?circle-token={}'.format(repo, branch, token), json=build_parameters)
if 'build_num' not in r.json():
print('ERROR: Could not find repository {} or with the branch {}'.format(repo, branch))
print(r.json())
exit(1)
print('Building: ', r.json()['build_num'])
status = requests.get('https://circleci.com/api/v1.1/project/github/{}/{}?circle-token={}'.format(repo, r.json()['build_num'], token))
print('Build Status: ', status.json()['lifecycle'])
while status.json()['lifecycle'] != 'finished':
time.sleep(poll)
print('Build Status: ', status.json()['lifecycle'])
status = requests.get('https://circleci.com/api/v1.1/project/github/{}/{}?circle-token={}'.format(repo, r.json()['build_num'], token))
print('Finished. Failed? ', status.json()['failed'])
if status.json()['failed']:
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment