Skip to content
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

Allow running marge-bot in CI pipelines or as a single CLI job #289

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ optional arguments:
[env var: MARGE_SOURCE_BRANCH_REGEXP] (default: .*)
--debug Debug logging (includes all HTTP requests etc).
[env var: MARGE_DEBUG] (default: False)
--cli Run marge-bot as a single CLI command, not as a long-running service.
This may be used to run marge-bot in scheduled CI pipelines or cronjobs.
[env var: MARGE_CLI] (default: False)
```
Here is a config file example
```yaml
Expand Down Expand Up @@ -299,6 +302,43 @@ configs:
name: marge_bot_config
```

### Running marge-bot in CI

You can also run marge-bot directly in your existing CI via scheduled pipelines
if you'd like to avoid setting up any additional infrastructure.

This way, you can inject secrets for marge-bot's credentials at runtime
inside the ephemeral container for each run by adding them to protected CI/CD
variables in a dedicated marge-bot runner project, as well as store execution
logs as artifacts for evidence.

You can also configure multiple setups in different CI schedules by supplying
`MARGE_*` environment variables per-schedule, such as running a different set
of projects or settings at different times.

Note that in this case, marge-bot will be slower than when run as a service,
depending on the frequency of your pipeline schedules.

Create a marge-bot runner project, and add the variables `MARGE_AUTH_TOKEN`
(of type Variable) and `MARGE_SSH_KEY_FILE` (of type File) in your CI/CD
Variables settings.

Then add a scheduled pipeline run to your project with the following minimal
`.gitlab-ci.yml` config:

```yaml
run:
image:
name: smarkets/marge-bot:latest
entrypoint: [""]
only:
- schedules
variables:
MARGE_CLI: "true"
MARGE_GITLAB_URL: "$CI_SERVER_URL"
script: marge.app
```

### Running marge-bot as a plain python app

#### Installing marge-bot with nix
Expand Down
6 changes: 6 additions & 0 deletions marge/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ def regexp(str_regex):
action='store_true',
help='Skip CI when updating individual MRs when using batches'
)
parser.add_argument(
'--cli',
action='store_true',
help='Run marge-bot as a single CLI command, not a service'
)
config = parser.parse_args(args)

if config.use_merge_strategy and config.batch:
Expand Down Expand Up @@ -327,6 +332,7 @@ def main(args=None):
skip_ci_batches=options.skip_ci_batches,
),
batch=options.batch,
cli=options.cli,
)

marge_bot = bot.Bot(api=api, config=config)
Expand Down
5 changes: 4 additions & 1 deletion marge/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def _run(self, repo_manager):
time_to_sleep_between_projects_in_secs,
projects,
)
if self._config.cli:
return

big_sleep = max(0,
min_time_to_sleep_after_iterating_all_projects_in_secs -
time_to_sleep_between_projects_in_secs * len(projects))
Expand Down Expand Up @@ -187,7 +190,7 @@ def _get_single_job(self, project, merge_request, repo, options):

class BotConfig(namedtuple('BotConfig',
'user ssh_key_file project_regexp merge_order merge_opts git_timeout ' +
'git_reference_repo branch_regexp source_branch_regexp batch')):
'git_reference_repo branch_regexp source_branch_regexp batch cli')):
pass


Expand Down