til: github-actions_debug-tmate.md
This data as json
| path | topic | title | url | body | html | shot | created | created_utc | updated | updated_utc | shot_hash | slug |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| github-actions_debug-tmate.md | github-actions | Open a debugging shell in GitHub Actions with tmate | https://github.com/simonw/til/blob/main/github-actions/debug-tmate.md | > :warning: **17 Feb 2022: There have been reports of running tmate causing account suspensions**. See [this issue](https://github.com/mxschmitt/action-tmate/issues/104) for details. Continue with caution. Thanks to [this Twitter conversation](https://twitter.com/harrymarr/status/1304820879268950021) I found out about [mxschmitt/action-tmate](https://github.com/mxschmitt/action-tmate), which uses https://tmate.io/ to open an interactive shell session running inside the GitHub Actions environment. I created a `.github/workflows/tmate.yml` file in my repo containing the following: ```yaml name: tmate session on: workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Setup tmate session uses: mxschmitt/action-tmate@v3 ``` Clicking the "Run workflow" button in the GitHub Actions interface then gave me the following in the GitHub Actions log output: ``` WebURL: https://tmate.io/t/JA69KaB2avRPRZSkRb8JPa9Gd SSH: ssh JA69KaB2avRPRZSkRb8JPa9Gd@nyc1.tmate.io ``` I ran `ssh JA69KaB2avRPRZSkRb8JPa9Gd@nyc1.tmate.io` and got a direction connection to the Action, with my project files all available thanks to the `- uses: actions/checkout@v2` step. Once I'd finish testing things out in that environment, I typed `touch continue` and the session ended itself. ## Starting a shell just for test failures on manual runs I had a tricky test failure that I wanted to debug interactively. Here's a recipe for starting a tmate shell ONLY if the previous step failed, and only if the run was triggered manually (using `workflow_dispatch`) - because I don't want an accidental test opening up a shell and burning up my GitHub Actions minutes allowance. ```yaml steps: - name: Run tests run: pytest - name: tmate session if tests fail if: failure() && github.event_name == 'workflow_dispatch' uses: mxschmitt/action-tmate@v3 ``` | <blockquote> <p><g-emoji class="g-emoji" alias="warning" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/26a0.png">⚠️</g-emoji> <strong>17 Feb 2022: There have been reports of running tmate causing account suspensions</strong>. See <a href="https://github.com/mxschmitt/action-tmate/issues/104">this issue</a> for details. Continue with caution.</p> </blockquote> <p>Thanks to <a href="https://twitter.com/harrymarr/status/1304820879268950021" rel="nofollow">this Twitter conversation</a> I found out about <a href="https://github.com/mxschmitt/action-tmate">mxschmitt/action-tmate</a>, which uses <a href="https://tmate.io/" rel="nofollow">https://tmate.io/</a> to open an interactive shell session running inside the GitHub Actions environment.</p> <p>I created a <code>.github/workflows/tmate.yml</code> file in my repo containing the following:</p> <div class="highlight highlight-source-yaml"><pre><span class="pl-ent">name</span>: <span class="pl-s">tmate session</span> <span class="pl-ent">on</span>: <span class="pl-ent">workflow_dispatch</span>: <span class="pl-ent">jobs</span>: <span class="pl-ent">build</span>: <span class="pl-ent">runs-on</span>: <span class="pl-s">ubuntu-latest</span> <span class="pl-ent">steps</span>: - <span class="pl-ent">uses</span>: <span class="pl-s">actions/checkout@v2</span> - <span class="pl-ent">name</span>: <span class="pl-s">Setup tmate session</span> <span class="pl-ent">uses</span>: <span class="pl-s">mxschmitt/action-tmate@v3</span></pre></div> <p>Clicking the "Run workflow" button in the GitHub Actions interface then gave me the following in the GitHub Actions log output:</p> <pre><code>WebURL: https://tmate.io/t/JA69KaB2avRPRZSkRb8JPa9Gd SSH: ssh JA69KaB2avRPRZSkRb8JPa9Gd@nyc1.tmate.io </code></pre> <p>I ran <code>ssh JA69KaB2avRPRZSkRb8JPa9Gd@nyc1.tmate.io</code> and got a direction connection to the Action, with my project files all available thanks to the <code>- uses: actions/checkout@v2</code> step.</p> <p>Once I'd finish testing things out in that environment, I typed <code>touch continue</code> and the session ended itself.</p> <h2> <a id="user-content-starting-a-shell-just-for-test-failures-on-manual-runs" class="anchor" href="#starting-a-shell-just-for-test-failures-on-manual-runs" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Starting a shell just for test failures on manual runs</h2> <p>I had a tricky test failure that I wanted to debug interactively. Here's a recipe for starting a tmate shell ONLY if the previous step failed, and only if the run was triggered manually (using <code>workflow_dispatch</code>) - because I don't want an accidental test opening up a shell and burning up my GitHub Actions minutes allowance.</p> <div class="highlight highlight-source-yaml"><pre> <span class="pl-ent">steps</span>: - <span class="pl-ent">name</span>: <span class="pl-s">Run tests</span> <span class="pl-ent">run</span>: <span class="pl-s">pytest</span> - <span class="pl-ent">name</span>: <span class="pl-s">tmate session if tests fail</span> <span class="pl-ent">if</span>: <span class="pl-s">failure() && github.event_name == 'workflow_dispatch'</span> <span class="pl-ent">uses</span>: <span class="pl-s">mxschmitt/action-tmate@v3</span></pre></div> | <Binary: 58,103 bytes> | 2020-09-14T15:25:36-07:00 | 2020-09-14T22:25:36+00:00 | 2022-02-17T15:30:51-08:00 | 2022-02-17T23:30:51+00:00 | 64a5eae4fd60080f0d219ecc7b9ccd05 | debug-tmate |