Skip to content

Commit

Permalink
add cleanup process (#224)
Browse files Browse the repository at this point in the history
* add cleanup process

* save state

* update README/CHANGELOG

* fix message

* `getSshDirectory()` should not be exported
  • Loading branch information
shimataro committed Dec 19, 2022
1 parent 193316a commit 7cccbd3
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -161,7 +161,7 @@ rules: # https://eslint.org/docs/rules/
- error
- max: 1
no-native-reassign: error
no-negated-condition: error
no-negated-condition: 'off'
no-negated-in-lhs: error
no-nested-ternary: error
no-new: error
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

* reomve SSH directory at the end of workflow

## [2.4.0] - 2022-11-03

### Added
Expand Down
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -45,7 +45,10 @@ steps:

See [Workflow syntax for GitHub Actions](https://help.github.com/en/articles/workflow-syntax-for-github-actions) for details.

**NOTE:** Server key of `github.com` will be always set to `known_hosts`.
**NOTE:**

* Server key of `github.com` will be always set to `known_hosts`.
* SSH keys will be removed at the end of workflow.

### Install multiple keys

Expand Down
1 change: 1 addition & 0 deletions action.yml
Expand Up @@ -28,3 +28,4 @@ inputs:
runs:
using: "node16"
main: "lib/index.js"
post: "lib/index.js"
55 changes: 53 additions & 2 deletions lib/index.js
Expand Up @@ -2738,6 +2738,31 @@ catch (err) {
* main function
*/
function main() {
if (!isPost()) {
setup();
setPost();
}
else {
cleanup();
}
}
/**
* is post process?
* @returns Yes/No
*/
function isPost() {
return Boolean(core.getState("isPost"));
}
/**
* update post state
*/
function setPost() {
core.saveState("isPost", "true");
}
/**
* setup function
*/
function setup() {
// parameters
const key = core.getInput("key", {
required: true,
Expand Down Expand Up @@ -2788,19 +2813,45 @@ function main() {
}
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.`);
}
/**
* create ".ssh" directory
* @returns directory name
*/
function createSshDirectory() {
const home = getHomeDirectory();
const dirName = path_1.default.resolve(home, ".ssh");
const dirName = getSshDirectory();
fs_1.default.mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
return dirName;
}
/**
* remove ".ssh" directory
* @returns removed directory name
*/
function removeSshDirectory() {
const dirName = getSshDirectory();
fs_1.default.rmSync(dirName, {
recursive: true,
force: true,
});
return dirName;
}
/**
* get SSH directory
* @returns SSH directory name
*/
function getSshDirectory() {
return path_1.default.resolve(getHomeDirectory(), ".ssh");
}
/**
* get home directory
* @returns home directory name
Expand Down
61 changes: 59 additions & 2 deletions src/main.ts
Expand Up @@ -25,6 +25,33 @@ try {
* main function
*/
function main(): void {
if (!isPost()) {
setup();
setPost();
} else {
cleanup();
}
}

/**
* is post process?
* @returns Yes/No
*/
function isPost(): boolean {
return Boolean(core.getState("isPost"));
}

/**
* update post state
*/
function setPost(): void {
core.saveState("isPost", "true");
}

/**
* setup function
*/
function setup(): void {
// parameters
const key = core.getInput("key", {
required: true,
Expand Down Expand Up @@ -80,20 +107,50 @@ function main(): void {
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
}

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

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

/**
* create ".ssh" directory
* @returns directory name
*/
function createSshDirectory(): string {
const home = getHomeDirectory();
const dirName = path.resolve(home, ".ssh");
const dirName = getSshDirectory();
fs.mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
return dirName;
}

/**
* remove ".ssh" directory
* @returns removed directory name
*/
function removeSshDirectory(): string {
const dirName = getSshDirectory();
fs.rmSync(dirName, {
recursive: true,
force: true,
});
return dirName;
}

/**
* get SSH directory
* @returns SSH directory name
*/
function getSshDirectory(): string {
return path.resolve(getHomeDirectory(), ".ssh");
}

/**
* get home directory
* @returns home directory name
Expand Down

0 comments on commit 7cccbd3

Please sign in to comment.