diff --git a/.gitignore b/.gitignore index de3af067f..c43e89088 100644 --- a/.gitignore +++ b/.gitignore @@ -8,10 +8,12 @@ **/*.xproj **/*.xproj.user **/.vs -**/.vscode **/*.error **/*.json.pretty .idea/ +.vscode +!.vscode/launch.json +!.vscode/tasks.json # output node_modules diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..c4efc63ce --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,57 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Run [build]", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build runner layout", + "program": "${workspaceFolder}/_layout/bin/Runner.Listener", + "args": [ + "run" + ], + "cwd": "${workspaceFolder}/src", + "console": "integratedTerminal", + "requireExactSource": false, + }, + { + "name": "Run", + "type": "coreclr", + "request": "launch", + "program": "${workspaceFolder}/_layout/bin/Runner.Listener", + "args": [ + "run" + ], + "cwd": "${workspaceFolder}/src", + "console": "integratedTerminal", + "requireExactSource": false, + }, + { + "name": "Configure", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "create runner layout", + "program": "${workspaceFolder}/_layout/bin/Runner.Listener", + "args": [ + "configure" + ], + "cwd": "${workspaceFolder}/src", + "console": "integratedTerminal", + "requireExactSource": false, + }, + { + "name": "Debug Worker", + "type": "coreclr", + "request": "attach", + "processName": "Runner.Worker", + "requireExactSource": false, + }, + { + "name": "Attach Debugger", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}", + "requireExactSource": false, + }, + ], +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 000000000..9cdcf7905 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,33 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "create runner layout", + "detail": "Build and Copy all projects, scripts and external dependencies to _layout from src (run this the first time or after deleting _layout)", + "command": "./dev.sh", + "windows": { + "command": "dev.cmd" + }, + "args": [ + "layout" + ], + "options": { + "cwd": "${workspaceFolder}/src" + }, + }, + { + "label": "build runner layout", + "detail": "Build and Copy all projects to _layout from src (run this on code change)", + "command": "./dev.sh", + "windows": { + "command": "dev.cmd" + }, + "args": [ + "build" + ], + "options": { + "cwd": "${workspaceFolder}/src" + }, + } + ], +} \ No newline at end of file diff --git a/docs/contribute.md b/docs/contribute.md index 3c5cad854..e5039f1f6 100644 --- a/docs/contribute.md +++ b/docs/contribute.md @@ -19,12 +19,35 @@ We ask that before significant effort is put into code changes, that we have agr An ADR is an Architectural Decision Record. This allows consensus on the direction forward and also serves as a record of the change and motivation. [Read more here](adrs/README.md) -## Development Life Cycle - -### Required Dev Dependencies +## Required Dev Dependencies ![Win](res/win_sm.png) ![*nix](res/linux_sm.png) Git for Windows and Linux [Install Here](https://git-scm.com/downloads) (needed for dev sh script) +## Quickstart: Run a job from a real repository + +If you just want to get from building the sourcecode to using it to execute an action, you will need: + +- The url of your repository +- A runner registration token. You can find it at `https://github.com/{your-repo}/settings/actions/runners/new` + + +```bash +git clone https://github.com/actions/runner +cd runner/src +./dev.(sh/cmd) layout # the runner that built from source is in {root}/_layout +cd ../_layout +./config.(sh/cmd) --url https://github.com/{your-repo} --token ABCABCABCABCABCABCABCABCABCAB # accept default name, labels and work folder +./run.(sh/cmd) +``` + +If you trigger a job now, you can see the runner execute it. + +Tip: Make sure your job can run on this runner. The easiest way is to set `runs-on: self-hosted` in the workflow file. + + +## Development Life Cycle +If you're using VS Code, you can follow [these](contribute/vscode.md) steps instead. + ### To Build, Test, Layout Navigate to the `src` directory and run the following command: @@ -39,7 +62,7 @@ Navigate to the `src` directory and run the following command: * `build` (`b`): Build everything and update runner layout folder * `test` (`t`): Build runner binaries and run unit tests -**Sample developer flow:** +### Sample developer flow: ```bash git clone https://github.com/actions/runner @@ -51,40 +74,81 @@ cd ./src ./dev.(sh/cmd) test # run all unit tests before git commit/push ``` -**Configure Runner:** +Let's break that down. + +### Clone repository: + ```bash -cd runner/_layout -./config.sh # configure your custom runner +git clone https://github.com/actions/runner +cd runner +``` +If you want to push your changes to a remote, it is recommended you fork the repository and use that fork as your origin instead of `https://github.com/actions/runner`. + + +### Build Layout: + +This command will build all projects, then copies them and other dependencies into a folder called `_layout`. The binaries in this folder are then used for running, debugging the runner. + +```bash +cd ./src # execute the script from this folder +./dev.(sh/cmd) layout # the runner that built from source is in {root}/_layout ``` -You will need your the name of your repository and a runner registration token. -You can find both at `https://github.com/{your-repo}/settings/actions/runners/new` +If you make code changes after this point, use the argument `build` to build your code in the `src` folder to keep your `_layout` folder up to date. + +```bash +cd ./src +./dev.(sh/cmd) build # {root}/_layout will get updated +``` +### Test Layout: + +This command runs the suite of unit tests in the project + +```bash +cd ./src +./dev.(sh/cmd) test # run all unit tests before git commit/push +``` + +### Configure Runner: + +If you want to manually test your runner and run actions from a real repository, you'll have to configure it before running it. + +```bash +cd runner/_layout +./config.(sh/cmd) # configure your custom runner +``` + +You will need your the name of your repository and a runner registration token. +Check [Quickstart](##Quickstart:-Run-a-job-from-a-real-repository) if you don't know how to get this token. These can also be passed down as arguments to `config.(sh/cmd)`: ```bash cd runner/_layout -./config.sh --url https://github.com/{your-repo} --token ABCABCABCABCABCABCABCABCABCAB +./config.(sh/cmd) --url https://github.com/{your-repo} --token ABCABCABCABCABCABCABCABCABCAB ``` -**Run Runner (Configure first!):** +### Run Runner + +All that's left to do is to start the runner: ```bash cd runner/_layout -./run.sh # run your custom runner +./run.(sh/cmd) # run your custom runner ``` -**View logs:** +### View logs: + ```bash cd runner/_layout/_diag ls cat (Runner/Worker)_TIMESTAMP.log # view your log file ``` -### Editors +## Editors [Using Visual Studio Code](https://code.visualstudio.com/) [Using Visual Studio](https://code.visualstudio.com/docs) -### Styling +## Styling We use the .NET Foundation and CoreCLR style guidelines [located here]( https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md) diff --git a/docs/contribute/vscode.md b/docs/contribute/vscode.md new file mode 100644 index 000000000..9b89aa9e2 --- /dev/null +++ b/docs/contribute/vscode.md @@ -0,0 +1,52 @@ +# Development Life Cycle using VS Code: + +These examples use VS Code, but the idea should be similar across all IDEs as long as you attach to the same processes in the right folder. +## Configure + +To successfully start the runner, you need to register it using a repository and a runner registration token. +Run `Configure` first to build the source code and set up the runner in `_layout`. +Once it's done creating `_layout`, it asks for the url of your repository and your token in the terminal. + +Check [Quickstart](../contribute.md#quickstart-run-a-job-from-a-real-repository) if you don't know how to get this token. + +## Debugging + +Debugging the full lifecycle of a job can be tricky, because there are multiple processes involved. +All the configs below can be found in `.vscode/launch.json`. + +## Debug the Listener + +```json +{ + "name": "Run [build]", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build runner layout", // use the config called "Run" to launch without rebuild + "program": "${workspaceFolder}/_layout/bin/Runner.Listener", + "args": [ + "run" // run without args to print usage + ], + "cwd": "${workspaceFolder}/src", + "console": "integratedTerminal", + "requireExactSource": false, +} +``` + +If you launch `Run` or `Run [build]`, it starts a process called `Runner.Listener`. +This process will receive any job queued on this repository if the job runs on matching labels (e.g `runs-on: self-hosted`). +Once a job is received, a `Runner.Listener` starts a new process of `Runner.Worker`. +Since this is a diferent process, you can't use the same debugger session debug it. +Instead, a parallel debugging session has to be started, using a different launch config. +Luckily, VS Code supports multiple parallel debugging sessions. + +## Debug the Worker + +Because the worker process is usually started by the listener instead of an IDE, debugging it from start to finish can be tricky. +For this reason, `Runner.Worker` can be configured to wait for a debugger to be attached before it begins any actual work. + +Set the environment variable `GITHUB_ACTIONS_RUNNER_ATTACH_DEBUGGER` to `true` or `1` to enable this wait. +All worker processes now will wait 20 seconds before they start working on their task. + +This gives enough time to attach a debugger by running `Debug Worker`. +If for some reason you have multiple workers running, run the launch config `Attach` instead. +Select `Runner.Worker` from the running processes when VS Code prompts for it. diff --git a/src/Runner.Listener/Configuration/ConfigurationManager.cs b/src/Runner.Listener/Configuration/ConfigurationManager.cs index 8d08b063e..4254bbdff 100644 --- a/src/Runner.Listener/Configuration/ConfigurationManager.cs +++ b/src/Runner.Listener/Configuration/ConfigurationManager.cs @@ -53,7 +53,7 @@ namespace GitHub.Runner.Listener.Configuration Trace.Info(nameof(LoadSettings)); if (!IsConfigured()) { - throw new InvalidOperationException("Not configured"); + throw new InvalidOperationException("Not configured. Run config.(sh/cmd) to configure the runner."); } RunnerSettings settings = _store.GetSettings();