Using file mounts with server-side execution

File mounts are a powerful way to run custom files without directly embedding the files in your batch spec.

Writing a batch spec

In the following example, you have a Python script that appends "Hello World" to all README.md files.

#!/usr/bin/env python3
import os.path


def main():
  if os.path.exists('README.md'):
    with open('README.md', 'a') as f:
      f.write('\nHello World')


if __name__ == "__main__":
  main()

To use the Python script in your batch change, mount the script in a step using the mounts field. The following is an example of mounting the above Python script in a step.

name: hello-world
description: Add Hello World to READMEs

# Find all repositories that contain a README.md file.
on:
  - repositoriesMatchingQuery: file:README.md

# In each repository, run this command. Each repository's resulting diff is captured.
steps:
  - run: python /tmp/hello_appender.py
    container: python:latest
    mount:
      - path: ./hello_appender.py
        mountpoint: /tmp/hello_appender.py

# Describe the changeset (e.g., GitHub pull request) you want for each repository.
changesetTemplate:
  title: Hello World
  body: My first batch change!
  branch: hello-world # Push the commit to this branch.
  commit:
    message: Append Hello World to all README.md files
  published: false # Do not publish any changes to the code hosts yet

In this example, the Python script should live besides the batch spec file, as indicated by the path:

.
├── batch-spec.yml
└── hello_appender.py

Note that a container appropriate for the mounted file has also been chosen for this step.

Running server-side

After writing the batch spec, use the Sourcegraph CLI (src) command remote to execute the batch spec server-side.

src batch remote -f batch-spec.yml

Once successful, src provides a URL to the execution of the batch change.