From 19a49ed72830f0810df81e42252ee9da19c8e4d8 Mon Sep 17 00:00:00 2001 From: Tachibana waita Date: Tue, 5 Oct 2021 20:13:28 +0900 Subject: [PATCH] feat: support user own tmate server (#93) Co-authored-by: Johannes Schindelin Co-authored-by: Max Schmitt --- README.md | 21 +++++++++++++++++++++ action.yml | 18 +++++++++++++++++- lib/index.js | 23 ++++++++++++++++++++++- src/helpers.js | 13 +++++++++++++ src/index.js | 13 ++++++++++--- 5 files changed, 83 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2b4b8702..c69343a6 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,27 @@ jobs: If the registered public SSH key is not your default private SSH key, you will need to specify the path manually, like so: `ssh -i `. +## Use your own tmate servers + +By default the tmate session uses `ssh.tmate.io`. You can use your own tmate servers. [tmate-ssh-server](https://github.com/tmate-io/tmate-ssh-server) is the server side part of tmate. + +```yaml +name: CI +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 + with: + tmate-server-host: ssh.tmate.io + tmate-server-port: 22 + tmate-server-rsa-fingerprint: SHA256:Hthk2T/M/Ivqfk1YYUn5ijC2Att3+UPzD7Rn72P5VWs + tmate-server-ed25519-fingerprint: SHA256:jfttvoypkHiQYUqUCwKeqd9d1fJj/ZiQlFOHVl6E9sI +``` + ## Continue a workflow If you want to continue a workflow and you are inside a tmate session, just create a empty file with the name `continue` either in the root directory or in the project directory by running `touch continue` or `sudo touch /continue`. diff --git a/action.yml b/action.yml index 35651c59..39c14301 100644 --- a/action.yml +++ b/action.yml @@ -12,4 +12,20 @@ inputs: limit-access-to-actor: description: 'If only the public SSH keys of the user triggering the workflow should be authorized' required: false - default: 'false' \ No newline at end of file + default: 'false' + tmate-server-host: + description: 'The hostname for your tmate server' + required: false + default: '' + tmate-server-port: + description: 'The port for your tmate server' + required: false + default: '' + tmate-server-rsa-fingerprint: + description: 'The RSA fingerprint for your tmate server' + required: false + default: '' + tmate-server-ed25519-fingerprint: + description: 'The ed25519 fingerprint for your tmate server' + required: false + default: '' diff --git a/lib/index.js b/lib/index.js index 0e1f3007..22524e47 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10242,6 +10242,7 @@ var external_child_process_ = __nccwpck_require__(3129); // @ts-check + /** * @param {string} cmd * @returns {Promise} @@ -10277,6 +10278,18 @@ const execShellCommand = (cmd) => { }); } +/** + * @param {string} key + * @return {string} + */ +const getValidatedInput = (key) => { + const value = core.getInput(key); + if (/^[-.+A-Za-z0-9]*$/.test(value)) { + throw new Error(`Invalid value for '${key}': '${value}'`); + } + return value; +} + ;// CONCATENATED MODULE: ./src/index.js // @ts-check @@ -10366,7 +10379,14 @@ async function run() { // Work around potential `set -e` commands in `~/.profile` (looking at you, `setup-miniconda`!) await execShellCommand(`echo 'set +e' >/tmp/tmate.bashrc`); - const setDefaultCommand = `set-option -g default-command "bash --rcfile /tmp/tmate.bashrc" \\;`; + let setDefaultCommand = `set-option -g default-command "bash --rcfile /tmp/tmate.bashrc" \\;`; + + if (core.getInput("tmate-server-host") !== "") { + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-host "${getValidatedInput("tmate-server-host")}" \\;`; + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-port "${getValidatedInput("tmate-server-port")}" \\;`; + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-rsa-fingerprint "${getValidatedInput("tmate-server-rsa-fingerprint")}" \\;`; + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-ed25519-fingerprint "${getValidatedInput("tmate-server-ed25519-fingerprint")}" \\;`; + } core.debug("Creating new session") await execShellCommand(`${tmate} ${newSessionExtra} ${setDefaultCommand} new-session -d`); @@ -10411,6 +10431,7 @@ function continueFileExists() { const continuePath = process.platform === "win32" ? "C:/msys64/continue" : "/continue" return external_fs_default().existsSync(continuePath) || external_fs_default().existsSync(external_path_default().join(process.env.GITHUB_WORKSPACE, "continue")) } + ;// CONCATENATED MODULE: ./src/main.js diff --git a/src/helpers.js b/src/helpers.js index b4e02e44..e49768ea 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,5 +1,6 @@ // @ts-check import { spawn } from 'child_process' +import * as core from "@actions/core" /** * @param {string} cmd @@ -35,3 +36,15 @@ export const execShellCommand = (cmd) => { }); }); } + +/** + * @param {string} key + * @return {string} + */ +export const getValidatedInput = (key) => { + const value = core.getInput(key); + if (/^[-.+A-Za-z0-9]*$/.test(value)) { + throw new Error(`Invalid value for '${key}': '${value}'`); + } + return value; +} diff --git a/src/index.js b/src/index.js index 4f9577f2..8947f187 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ import * as github from "@actions/github" import * as tc from "@actions/tool-cache" import { Octokit } from "@octokit/rest" -import { execShellCommand } from "./helpers" +import { execShellCommand, getValidatedInput } from "./helpers" const TMATE_LINUX_VERSION = "2.4.0" @@ -86,7 +86,14 @@ export async function run() { // Work around potential `set -e` commands in `~/.profile` (looking at you, `setup-miniconda`!) await execShellCommand(`echo 'set +e' >/tmp/tmate.bashrc`); - const setDefaultCommand = `set-option -g default-command "bash --rcfile /tmp/tmate.bashrc" \\;`; + let setDefaultCommand = `set-option -g default-command "bash --rcfile /tmp/tmate.bashrc" \\;`; + + if (core.getInput("tmate-server-host") !== "") { + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-host "${getValidatedInput("tmate-server-host")}" \\;`; + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-port "${getValidatedInput("tmate-server-port")}" \\;`; + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-rsa-fingerprint "${getValidatedInput("tmate-server-rsa-fingerprint")}" \\;`; + setDefaultCommand = `${setDefaultCommand} set-option -g tmate-server-ed25519-fingerprint "${getValidatedInput("tmate-server-ed25519-fingerprint")}" \\;`; + } core.debug("Creating new session") await execShellCommand(`${tmate} ${newSessionExtra} ${setDefaultCommand} new-session -d`); @@ -130,4 +137,4 @@ function didTmateQuit() { function continueFileExists() { const continuePath = process.platform === "win32" ? "C:/msys64/continue" : "/continue" return fs.existsSync(continuePath) || fs.existsSync(path.join(process.env.GITHUB_WORKSPACE, "continue")) -} \ No newline at end of file +}