Skip to content

Commit

Permalink
Added a simple option to prevent the SSH from being cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
WesleyHilhorst committed Jun 30, 2023
1 parent f5671c0 commit 74e05ce
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ steps:
known_hosts: ${{ secrets.KNOWN_HOSTS }}
config: ${{ secrets.CONFIG }} # ssh_config; optional
if_key_exists: fail # replace / ignore / fail; optional (defaults to fail)
cleanup: false # Should remove .ssh directory? set false to keep .ssh directory
- name: rsync over SSH
run: rsync -r ./foo/ user@remote:bar/
```
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://help.github.com/en/articles/metadata-syntax-for-github-actions
name: "Install SSH Key"
description: "Install SSH key in ~/.ssh"
author: "shimataro"
author: "Pinch"
branding:
icon: "terminal"
color: "gray-dark"
Expand All @@ -25,6 +25,10 @@ inputs:
description: "replace / ignore / fail"
required: false
default: "fail"
cleanup:
description: "Should remove .ssh directory?"
required: false
default: false
runs:
using: "node16"
main: "lib/index.js"
Expand Down
23 changes: 16 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2822,19 +2822,25 @@ function setup() {
});
}
// create files
const fileNames = [];
for (const file of files) {
const fileName = path_1.default.join(sshDirName, file.name);
fs_1.default.writeFileSync(fileName, file.contents, file.options);
fileNames.push(fileName);
}
core.saveState("files", fileNames);
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
}
/**
* cleanup function
*/
function cleanup() {
// remove ".ssh" directory
const sshDirName = removeSshDirectory();
console.log(`SSH key in ${sshDirName} has been removed successfully.`);
const shouldCleanup = core.getBooleanInput("cleanup");
if (shouldCleanup) {
// remove ".ssh" directory
const sshDirName = removeSshDirectory();
console.log(`SSH key in ${sshDirName} has been removed successfully.`);
}
}
/**
* create ".ssh" directory
Expand All @@ -2854,10 +2860,13 @@ function createSshDirectory() {
*/
function removeSshDirectory() {
const dirName = getSshDirectory();
fs_1.default.rmSync(dirName, {
recursive: true,
force: true,
});
const fileNames = JSON.parse(core.getState("files"));
for (const filePath of fileNames) {
fs_1.default.rmSync(filePath, {
force: true,
});
console.log(`File ${filePath} has been removed.`);
}
return dirName;
}
/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/shimataro/ssh-key-action.git"
"url": "git+https://github.com/pinchbv/ssh-key-action-ga.git"
},
"keywords": [
"actions",
Expand All @@ -28,7 +28,7 @@
"ssh",
"ssh key"
],
"author": "shimataro",
"author": "Pinch",
"license": "MIT",
"devDependencies": {
"@actions/core": "1.10.0",
Expand Down
26 changes: 19 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,29 @@ function setup(): void {
}

// create files
const fileNames = [];
for (const file of files) {
const fileName = path.join(sshDirName, file.name);
fs.writeFileSync(fileName, file.contents, file.options);
fileNames.push(fileName);
}

core.saveState("files", fileNames);

console.log(`SSH key has been stored to ${sshDirName} successfully.`);
}

/**
* cleanup function
*/
function cleanup(): void {
// remove ".ssh" directory
const sshDirName = removeSshDirectory();
const shouldCleanup = core.getBooleanInput("cleanup");
if (shouldCleanup) {
// remove ".ssh" directory
const sshDirName = removeSshDirectory();

console.log(`SSH key in ${sshDirName} has been removed successfully.`);
console.log(`SSH key in ${sshDirName} has been removed successfully.`);
}
}

/**
Expand All @@ -136,10 +143,15 @@ function createSshDirectory(): string {
*/
function removeSshDirectory(): string {
const dirName = getSshDirectory();
fs.rmSync(dirName, {
recursive: true,
force: true,
});
const fileNames = JSON.parse(core.getState("files"));

for (const filePath of fileNames) {
fs.rmSync(filePath, {
force: true,
});
console.log(`File ${filePath} has been removed.`);
}

return dirName;
}

Expand Down

0 comments on commit 74e05ce

Please sign in to comment.