Skip to content

Commit 9dc8c46

Browse files
jyanniBotPeaches
andauthored
Create random sock file if none passed (#42)
* SIP-388 feat: make socket-path optional with rand temp file as default * SIP-388 fix: include default in yml * SIP-388 refactor: use "-u" to unlink temp file prior to ssh-agent * SIP-388 docs: adjust readme to hint at optional property * SIP-388 build: test itself during ci pipeline * SIP-388 fix: end action on error * SIP-388 build: adjust ci for a new specific socket file * SIP-388 fix: error handling for file path generation * SIP-388 refactor: move console log after socketPath set * SIP-388 fix: properly set socketPath * SIP-388 fix: trim newlines away --------- Co-authored-by: Connor Tumbleson <connor@sourcetoad.com>
1 parent f333ba8 commit 9dc8c46

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,24 @@ jobs:
1818
- run: yarn install
1919

2020
- run: yarn build
21+
22+
- name: Test Itself (Random Socket)
23+
id: ssh-socket-action-random
24+
uses: ./
25+
with:
26+
host: github.com
27+
key: ${{ secrets.BASE64_INTENTIONAL_EXAMPLE_PUBLIC_SECRET_KEY }}
28+
29+
- name: Use SSH socket (Random Socket)
30+
run: ls -l "${{ steps.ssh-socket-action-random.outputs.socket-path }}"
31+
32+
- name: Test Itself (Specified Socket)
33+
id: ssh-socket-action-specified
34+
uses: ./
35+
with:
36+
host: github.com
37+
socket-path: '/tmp/ssh_agent.sock'
38+
key: ${{ secrets.BASE64_INTENTIONAL_EXAMPLE_PUBLIC_SECRET_KEY }}
39+
40+
- name: Use SSH socket (Specified Socket)
41+
run: ls -l "${{ steps.ssh-socket-action-specified.outputs.socket-path }}"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Following inputs can be used as `step.with` keys
1010
|---------------|----------|--------|------------------------------------|
1111
| `host` | Yes | String | Remote hostname. |
1212
| `port` | No | Number | SSH Port (default: `22`). |
13-
| `socket-path` | Yes | String | Path at which to create socket. |
13+
| `socket-path` | No | String | Path at which to create socket. |
1414
| `key` | Yes | String | base64 private key |
1515
| `lifetime` | No | Number | Seconds to keep key (default: 600) |
1616

@@ -32,11 +32,11 @@ Store that in GitHub Secrets to securely pass to the action.
3232
```yaml
3333
- name: SSH Socket Setup
3434
id: ssh-socket-action
35-
uses: sourcetoad/ssh-socket-action@v1.0.0
35+
uses: sourcetoad/ssh-socket-action@v1
3636
with:
3737
host: github.com
3838
port: 22 # optional
39-
socket-path: /tmp/ssh_agent.sock
39+
socket-path: /tmp/ssh_agent.sock # optional
4040
key: ${{ secrets.BASE64_SECRET_KEY }}
4141

4242
- name: Use SSH socket

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ inputs:
1414
required: false
1515
socket-path:
1616
description: 'path at which to create socket'
17-
required: true
17+
required: false
18+
default: ''
1819
key:
1920
description: 'ssh private key'
2021
required: true

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ const { execSync } = require('child_process');
44

55
const host = core.getInput('host');
66
const port = core.getInput('port');
7-
const socketPath = core.getInput('socket-path');
87
const key = core.getInput('key');
98
const lifetimeInSeconds = core.getInput('lifetime');
9+
let socketPath = core.getInput('socket-path');
10+
11+
// Create random socket path, if none passed.
12+
if (!socketPath) {
13+
try {
14+
socketPath = execSync('mktemp -u', {encoding: 'utf-8'}).trim();
15+
} catch (e) {
16+
core.setFailed(e.message);
17+
process.exit(1);
18+
}
19+
}
1020

1121
console.log(`Attempting to create ${socketPath}...`);
1222

@@ -24,6 +34,7 @@ try {
2434
core.info('Agent already exists on sock. Skipping creation.');
2535
} else {
2636
core.setFailed(e.message);
37+
process.exit(1);
2738
}
2839
}
2940

0 commit comments

Comments
 (0)