|
| 1 | +# Using file mounts with server-side execution |
| 2 | + |
| 3 | +<aside class="experimental"> |
| 4 | +<p> |
| 5 | +<span class="badge badge-experimental">Experimental</span> This feature is experimental and might change or be removed in the future. We've released it as an experimental feature to provide a preview of functionality we're working on. |
| 6 | +</p> |
| 7 | + |
| 8 | +<p><b>We're very much looking for input and feedback on this feature.</b> You can either <a href="https://about.sourcegraph.com/contact">contact us directly</a>, <a href="https://github.com/sourcegraph/sourcegraph">file an issue</a>, or <a href="https://twitter.com/sourcegraph">tweet at us</a>.</p> |
| 9 | + |
| 10 | +<p>This feature is available in Sourcegraph 4.1 with <a href="https://github.com/sourcegraph/src-cli">Sourcegraph CLI</a> 4.0.1 and later.</p> |
| 11 | +</aside> |
| 12 | + |
| 13 | +> NOTE: Running a batch spec [server-side](../explanations/server_side.md) with file mounts is |
| 14 | +> currently only supported with <a href="https://github.com/sourcegraph/src-cli">Sourcegraph CLI</a>. |
| 15 | +
|
| 16 | +File [`mounts`](../references/batch_spec_yaml_reference.md#steps-mount) are a powerful way to run custom files without |
| 17 | +directly embedding the files in your batch spec. |
| 18 | + |
| 19 | +## Writing a batch spec |
| 20 | + |
| 21 | +In the following example, you have a Python script that appends "Hello World" to all `README.md` files. |
| 22 | + |
| 23 | +```python |
| 24 | +#!/usr/bin/env python3 |
| 25 | +import os.path |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + if os.path.exists('README.md'): |
| 30 | + with open('README.md', 'a') as f: |
| 31 | + f.write('\nHello World') |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + main() |
| 36 | +``` |
| 37 | + |
| 38 | +To use the Python script in your batch change, mount the script in a `step` using |
| 39 | +the [`mounts`](../references/batch_spec_yaml_reference.md#steps-mount) field. The following is an example of mounting |
| 40 | +the above Python script in a `step`. |
| 41 | + |
| 42 | +```yaml |
| 43 | +name: hello-world |
| 44 | +description: Add Hello World to READMEs |
| 45 | + |
| 46 | +# Find all repositories that contain a README.md file. |
| 47 | +on: |
| 48 | + - repositoriesMatchingQuery: file:README.md |
| 49 | + |
| 50 | +# In each repository, run this command. Each repository's resulting diff is captured. |
| 51 | +steps: |
| 52 | + - run: python /tmp/hello_appender.py |
| 53 | + container: python:latest |
| 54 | + mount: |
| 55 | + - path: ./hello_appender.py |
| 56 | + mountpoint: /tmp/hello_appender.py |
| 57 | + |
| 58 | +# Describe the changeset (e.g., GitHub pull request) you want for each repository. |
| 59 | +changesetTemplate: |
| 60 | + title: Hello World |
| 61 | + body: My first batch change! |
| 62 | + branch: hello-world # Push the commit to this branch. |
| 63 | + commit: |
| 64 | + message: Append Hello World to all README.md files |
| 65 | + published: false # Do not publish any changes to the code hosts yet |
| 66 | +``` |
| 67 | +
|
| 68 | +In this example, the Python script should live besides the batch spec file, as indicated by the `path`: |
| 69 | + |
| 70 | +```text |
| 71 | +. |
| 72 | +├── batch-spec.yml |
| 73 | +└── hello_appender.py |
| 74 | +``` |
| 75 | + |
| 76 | +Note that a `container` appropriate for the mounted file has also been chosen for this step. |
| 77 | + |
| 78 | +## Running server-side |
| 79 | + |
| 80 | +After writing the batch spec, use the <a href="https://github.com/sourcegraph/src-cli">Sourcegraph CLI (`src`)</a> |
| 81 | +command [`remote`](../../cli/references/batch/remote.md) to execute the batch spec server-side. |
| 82 | + |
| 83 | +```shell |
| 84 | +src batch remote -f batch-spec.yml |
| 85 | +``` |
| 86 | + |
| 87 | +Once successful, `src` provides a URL to the execution of the batch change. |
0 commit comments