|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +from os import environ |
| 4 | + |
| 5 | +from atlassian import Bamboo |
| 6 | + |
| 7 | +""" |
| 8 | +To set up variables, use: |
| 9 | +export BAMBOO_URL=https://your-url BAMBOO_PASSWORD=your_pass BAMBOO_USERNAME=your_username |
| 10 | +You also can use .env files, check the awesome python-dotenv module: |
| 11 | +https://github.com/theskumar/python-dotenv |
| 12 | +""" |
| 13 | +bamboo = Bamboo( |
| 14 | + url=environ.get("BAMBOO_URL"), |
| 15 | + username=environ.get("BAMBOO_USERNAME"), |
| 16 | + password=environ.get("BAMBOO_PASSWORD"), |
| 17 | + advanced_mode=True # In this app I use an advanced_mode flag to handle responses. |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def execute_build(build_key, params): |
| 22 | + """ |
| 23 | + build_key: str |
| 24 | + params: dict |
| 25 | + """ |
| 26 | + started_build = bamboo.execute_build(build_key, **params) |
| 27 | + logging.info(f"Build execution status: {started_build.status_code}") |
| 28 | + if started_build.status_code == 200: |
| 29 | + logging.info(f"Build key: {started_build.json().get('buildResultKey')}") |
| 30 | + logging.info(started_build.json().get('link', {}).get("href")) |
| 31 | + else: |
| 32 | + logging.error(f"Execution failed!") |
| 33 | + logging.error(started_build.json().get("message")) |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == '__main__': |
| 37 | + """ |
| 38 | + This part of code only executes if we run this module directly. |
| 39 | + You can still import the execute_build function and use it separately in the different module. |
| 40 | + """ |
| 41 | + # Setting the logging level. INFO|ERROR|DEBUG are the most common. |
| 42 | + logging.basicConfig(level=logging.INFO) |
| 43 | + # Initialize argparse module with some program name and additional information |
| 44 | + parser = argparse.ArgumentParser( |
| 45 | + prog="bamboo_trigger", |
| 46 | + usage="%(prog)s BUILD-KEY --arguments [KEY VALUE]", |
| 47 | + description="Simple execution of the bamboo plan with provided key-value arguments" |
| 48 | + ) |
| 49 | + # Adding the build key as the first argument |
| 50 | + parser.add_argument("build", type=str, help="Build key") |
| 51 | + # Adding key=value parameters after the --arguments key |
| 52 | + parser.add_argument("--arguments", nargs="*") |
| 53 | + # Getting arguments |
| 54 | + args = parser.parse_args() |
| 55 | + # Make a dictionary from the command arguments |
| 56 | + build_arguments = {args.arguments[i]: args.arguments[i + 1] for i in range(0, len(args.arguments or []), 2)} |
| 57 | + # Pass build key and arguments to the function |
| 58 | + execute_build(args.build, build_arguments) |
0 commit comments