1
0
Fork 0
mirror of https://github.com/shimataro/ssh-key-action.git synced 2025-06-19 22:52:10 +10:00

Merge pull request #258 from shimataro/develop

version 2.6.0
This commit is contained in:
shimataro 2023-10-12 04:11:09 +09:00 committed by GitHub
commit d05e1bdf7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 3722 additions and 5012 deletions

View file

@ -12,14 +12,12 @@ jobs:
strategy:
matrix:
os:
- Windows-2019
- Windows-2022
- macOS-10.15
- macOS-11
- macOS-12
- Ubuntu-18.04
- Ubuntu-20.04
- Ubuntu-22.04
- windows-2019
- windows-2022
- macos-11
- macos-12
- ubuntu-20.04
- ubuntu-22.04
nodejs:
- 16
fail-fast: false
@ -36,5 +34,7 @@ jobs:
run: npm ci
- name: Build
run: npm run build
- name: Exit if differ (forgot to commit dist dir?)
run: git diff --exit-code --quiet
- name: Verify
run: npm run verify

View file

@ -63,7 +63,9 @@ jobs:
with:
key: ${{ secrets.SSH_KEY_PEM }}
known_hosts: |
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQeJzhupRu0u0cdegZIa8e86EG2qOCsIsD1Xw0xSeiPDlCr7kq97NLmMbpKTX6Esc30NuoqEEHCuc7yWtwp8dI76EEEB1VqY9QJq6vk+aySyboD5QF61I/1WeTwu+deCbgKMGbUijeXhtfbxSxm6JwGrXrhBdofTsbKRUsrN1WoNgUa8uqN1Vx6WAJw1JHPhglEGGHea6QICwJOAr/6mrui/oB7pkaWKHj3z7d1IC4KWLtY47elvjbaTlkN04Kc/5LFEirorGYVbt15kAUlqGM65pk6ZBxtaO3+30LVlORZkxOh+LKL/BvbZ/iRNhItLqNyieoQj/uh/7Iv4uyH/cV/0b4WDSd3DptigWq84lJubb9t/DnZlrJazxyDCulTmKdOR7vs9gMTo+uoIrPSb8ScTtvw65+odKAlBj59dhnVp9zd7QUojOpXlL62Aw56U4oO+FALuevvMjiWeavKhJqlR7i5n9srYcrNV7ttmDw7kf/97P5zauIhxcjX+xHv4M=
bitbucket.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPIQmuzMBuKdWeF4+a2sjSSpBK0iqitSQ+5BM9KhpexuGt20JpTVM7u5BDZngncgrqDMbWdxMWWOGtZ9UgbqgZE=
bitbucket.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIazEu89wgQZ4bqs3d63QSMzYVa0MuJ2e2gKTKqu+UUO
- name: git clone through SSH
run: git clone git@bitbucket.org:shimataro999/ssh-test.git tmp
@ -100,7 +102,9 @@ jobs:
with:
key: ${{ secrets.SSH_KEY_PKCS8 }}
known_hosts: |
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQeJzhupRu0u0cdegZIa8e86EG2qOCsIsD1Xw0xSeiPDlCr7kq97NLmMbpKTX6Esc30NuoqEEHCuc7yWtwp8dI76EEEB1VqY9QJq6vk+aySyboD5QF61I/1WeTwu+deCbgKMGbUijeXhtfbxSxm6JwGrXrhBdofTsbKRUsrN1WoNgUa8uqN1Vx6WAJw1JHPhglEGGHea6QICwJOAr/6mrui/oB7pkaWKHj3z7d1IC4KWLtY47elvjbaTlkN04Kc/5LFEirorGYVbt15kAUlqGM65pk6ZBxtaO3+30LVlORZkxOh+LKL/BvbZ/iRNhItLqNyieoQj/uh/7Iv4uyH/cV/0b4WDSd3DptigWq84lJubb9t/DnZlrJazxyDCulTmKdOR7vs9gMTo+uoIrPSb8ScTtvw65+odKAlBj59dhnVp9zd7QUojOpXlL62Aw56U4oO+FALuevvMjiWeavKhJqlR7i5n9srYcrNV7ttmDw7kf/97P5zauIhxcjX+xHv4M=
bitbucket.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPIQmuzMBuKdWeF4+a2sjSSpBK0iqitSQ+5BM9KhpexuGt20JpTVM7u5BDZngncgrqDMbWdxMWWOGtZ9UgbqgZE=
bitbucket.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIazEu89wgQZ4bqs3d63QSMzYVa0MuJ2e2gKTKqu+UUO
- name: git clone through SSH
run: git clone git@bitbucket.org:shimataro999/ssh-test.git tmp
@ -137,7 +141,9 @@ jobs:
with:
key: ${{ secrets.SSH_KEY_RFC4716 }}
known_hosts: |
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==
bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQeJzhupRu0u0cdegZIa8e86EG2qOCsIsD1Xw0xSeiPDlCr7kq97NLmMbpKTX6Esc30NuoqEEHCuc7yWtwp8dI76EEEB1VqY9QJq6vk+aySyboD5QF61I/1WeTwu+deCbgKMGbUijeXhtfbxSxm6JwGrXrhBdofTsbKRUsrN1WoNgUa8uqN1Vx6WAJw1JHPhglEGGHea6QICwJOAr/6mrui/oB7pkaWKHj3z7d1IC4KWLtY47elvjbaTlkN04Kc/5LFEirorGYVbt15kAUlqGM65pk6ZBxtaO3+30LVlORZkxOh+LKL/BvbZ/iRNhItLqNyieoQj/uh/7Iv4uyH/cV/0b4WDSd3DptigWq84lJubb9t/DnZlrJazxyDCulTmKdOR7vs9gMTo+uoIrPSb8ScTtvw65+odKAlBj59dhnVp9zd7QUojOpXlL62Aw56U4oO+FALuevvMjiWeavKhJqlR7i5n9srYcrNV7ttmDw7kf/97P5zauIhxcjX+xHv4M=
bitbucket.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPIQmuzMBuKdWeF4+a2sjSSpBK0iqitSQ+5BM9KhpexuGt20JpTVM7u5BDZngncgrqDMbWdxMWWOGtZ9UgbqgZE=
bitbucket.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIazEu89wgQZ4bqs3d63QSMzYVa0MuJ2e2gKTKqu+UUO
- name: git clone through SSH
run: git clone git@bitbucket.org:shimataro999/ssh-test.git tmp
@ -156,7 +162,7 @@ jobs:
with:
key: "dummy" # replaced
known_hosts: unnecessary
- name: Install SSH key (replace)
- name: Install SSH key (replaces existing key)
uses: ./.
with:
key: ${{ secrets.SSH_KEY_PEM }}
@ -175,7 +181,7 @@ jobs:
if: ${{ inputs.package_installation_command != '' }}
- name: Checkout source codes
uses: actions/checkout@v3
- name: Install SSH key (replace)
- name: Install SSH key
uses: ./.
with:
key: ${{ secrets.SSH_KEY_PEM }}
@ -194,12 +200,12 @@ jobs:
if: ${{ inputs.package_installation_command != '' }}
- name: Checkout source codes
uses: actions/checkout@v3
- name: Install SSH key (dummy)
- name: Install SSH key
uses: ./.
with:
key: ${{ secrets.SSH_KEY_PEM }}
known_hosts: unnecessary
- name: Install SSH key (replace)
- name: Install SSH key (does nothing)
uses: ./.
with:
key: "dummy" # ignored
@ -218,7 +224,7 @@ jobs:
if: ${{ inputs.package_installation_command != '' }}
- name: Checkout source codes
uses: actions/checkout@v3
- name: Install SSH key (replace)
- name: Install SSH key
uses: ./.
with:
key: ${{ secrets.SSH_KEY_PEM }}
@ -237,15 +243,15 @@ jobs:
if: ${{ inputs.package_installation_command != '' }}
- name: Checkout source codes
uses: actions/checkout@v3
- name: Install SSH key (dummy)
- name: Install SSH key
uses: ./.
with:
key: ${{ secrets.SSH_KEY_PEM }}
known_hosts: unnecessary
- name: Install SSH key (replace)
- name: Install SSH key (fails)
uses: ./.
with:
key: "dummy" # ignored
key: "dummy" # fails
known_hosts: unnecessary
if_key_exists: fail
continue-on-error: true
@ -262,7 +268,7 @@ jobs:
if: ${{ inputs.package_installation_command != '' }}
- name: Checkout source codes
uses: actions/checkout@v3
- name: Install SSH key (replace)
- name: Install SSH key
uses: ./.
with:
key: ${{ secrets.SSH_KEY_PEM }}

View file

@ -11,7 +11,6 @@ jobs:
fail-fast: false
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
- ubuntu-22.04
docker_image:

View file

@ -11,7 +11,6 @@ jobs:
fail-fast: false
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
- ubuntu-22.04
docker_image:

View file

@ -11,7 +11,6 @@ jobs:
fail-fast: false
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
- ubuntu-22.04
docker_image:

View file

@ -11,7 +11,6 @@ jobs:
fail-fast: false
matrix:
os:
- macos-10.15
- macos-11
- macos-12
uses: "./.github/workflows/reusable-verify.yml"

View file

@ -11,7 +11,6 @@ jobs:
fail-fast: false
matrix:
os:
- ubuntu-18.04
- ubuntu-20.04
- ubuntu-22.04
uses: "./.github/workflows/reusable-verify.yml"

View file

@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
## [2.6.0] - 2023-10-11
### Others
* back up and restore files when exist (thanks [@bambeusz](https://github.com/bambeusz))
* remove `macos-10.15` and `ubuntu-18.04` virtual environment
## [2.5.1] - 2023-03-25
### Hotfix
@ -186,7 +193,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* First release.
[Unreleased]: https://github.com/shimataro/ssh-key-action/compare/v2.5.1...HEAD
[Unreleased]: https://github.com/shimataro/ssh-key-action/compare/v2.6.0...HEAD
[2.6.0]: https://github.com/shimataro/ssh-key-action/compare/v2.5.1...v2.6.0
[2.5.1]: https://github.com/shimataro/ssh-key-action/compare/v2.5.0...v2.5.1
[2.5.0]: https://github.com/shimataro/ssh-key-action/compare/v2.4.0...v2.5.0
[2.4.0]: https://github.com/shimataro/ssh-key-action/compare/v2.3.1...v2.4.0

View file

@ -17,7 +17,7 @@ Useful for SCP, SFTP, and `rsync` over SSH in deployment script.
tested on:
* [all available virtual machines](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources) (Windows Server 2022/2019, macOS Monterey/Big Sur/Catalina, and Ubuntu 22.04/20.04/18.04)
* [all available virtual machines](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources) (Windows Server 2022/2019, macOS Monterey/Big Sur, and Ubuntu 22.04/20.04)
* [Docker container (Ubuntu)](https://hub.docker.com/_/ubuntu) / requires `openssh-client` package; `apt install -y openssh-client`
* [Docker container (CentOS)](https://quay.io/repository/centos/centos) / requires `openssh-clients` package; `yum install -y openssh-clients`
* [Docker container (Alpine Linux)](https://hub.docker.com/_/alpine) / requires `openssh-client` package; `apk add openssh-client`

View file

@ -27,5 +27,5 @@ inputs:
default: "fail"
runs:
using: "node16"
main: "lib/index.js"
post: "lib/index.js"
main: "./dist/main.js"
post: "./dist/post.js"

72
dist/main.js vendored Normal file

File diff suppressed because one or more lines are too long

7
dist/main.js.map vendored Normal file

File diff suppressed because one or more lines are too long

67
dist/post.js vendored Normal file

File diff suppressed because one or more lines are too long

7
dist/post.js.map vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

5075
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,18 +1,19 @@
{
"name": "install-ssh-key",
"version": "2.5.1",
"version": "2.6.0",
"private": true,
"description": "Install SSH key in .ssh",
"main": "lib/index.js",
"main": "./dist/main.js",
"engines": {
"node": ">=8.0.0",
"npm": ">=5.7.0"
},
"scripts": {
"build": "ncc build src/main.ts -o lib",
"build": "esbuild ./src/main.ts ./src/post.ts --bundle --platform=node --minify --sourcemap --outdir=./dist",
"check-updates": "ncu",
"lint": "run-p lint:*",
"lint:ts": "eslint ./src --ext .ts",
"lint:es": "eslint ./src --ext .ts",
"lint:ts": "tsc --noEmit ./src/main.ts ./src/post.ts",
"lint:md": "markdownlint . --ignore node_modules --ignore examples",
"lint:yaml": "yamllint **/{,.}*.{yml,yaml} --ignore=node_modules/**",
"verify": "run-p lint"
@ -31,16 +32,16 @@
"author": "shimataro",
"license": "MIT",
"devDependencies": {
"@actions/core": "1.10.0",
"@types/node": "18.11.17",
"@typescript-eslint/eslint-plugin": "5.47.0",
"@typescript-eslint/parser": "5.47.0",
"@vercel/ncc": "0.36.0",
"eslint": "8.30.0",
"markdownlint-cli": "0.32.2",
"npm-check-updates": "16.6.2",
"@actions/core": "1.10.1",
"@types/node": "20.8.4",
"@typescript-eslint/eslint-plugin": "6.7.5",
"@typescript-eslint/parser": "6.7.5",
"esbuild": "0.19.4",
"eslint": "8.51.0",
"markdownlint-cli": "0.37.0",
"npm-check-updates": "16.14.5",
"npm-run-all": "4.1.5",
"typescript": "4.9.4",
"typescript": "5.2.2",
"yaml-lint": "1.7.0"
}
}

View file

@ -13,16 +13,16 @@ URL_REPOSITORY="${URL_PRODUCT}.git"
URL_COMPARE="${URL_PRODUCT}/compare"
URL_RELEASE="${URL_PRODUCT}/releases/new"
COLOR_ERROR="\e[1;41m"
COLOR_SECTION="\e[1;34m"
COLOR_COMMAND_NAME="\e[1;34m"
COLOR_OPTION="\e[4;36m"
COLOR_COMMAND="\e[4m"
COLOR_FILE="\e[1;34m"
COLOR_BRANCH="\e[1;31m"
COLOR_INPUT="\e[1;31m"
COLOR_SELECT="\e[1;32m"
COLOR_RESET="\e[m"
COLOR_ERROR="\033[1;41m"
COLOR_SECTION="\033[1;34m"
COLOR_COMMAND_NAME="\033[1;34m"
COLOR_OPTION="\033[4;36m"
COLOR_COMMAND="\033[4m"
COLOR_FILE="\033[1;34m"
COLOR_BRANCH="\033[1;31m"
COLOR_INPUT="\033[1;31m"
COLOR_SELECT="\033[1;32m"
COLOR_RESET="\033[m"
function main() {
cd $(dirname ${0})/..
@ -103,7 +103,7 @@ function update_changelog() {
local KEYWORD="Unreleased"
sed -i".bak" -r \
-e "s/^((##\s+)\[${KEYWORD}\])$/\1\n\n\2[${VERSION}] - ${DATE}/" \
-e "s/^((##[[:space:]]+)\[${KEYWORD}\])$/\1\n\n\2[${VERSION}] - ${DATE}/" \
-e "s/^(\[${KEYWORD}\](.*))(v.*)\.\.\.HEAD$/\1v${VERSION}...HEAD\n[${VERSION}]\2\3...v${VERSION}/" \
CHANGELOG.md
}

View file

@ -7,4 +7,4 @@ npm run verify
rm -rf node_modules
npm ci --only=production
git add lib
git add dist

View file

@ -25,7 +25,7 @@ npm run build
npm run verify
# commit
git add package.json package-lock.json lib
git add package.json package-lock.json dist
git commit -m "update dependencies"
# finished!

73
src/common.ts Normal file
View file

@ -0,0 +1,73 @@
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as core from "@actions/core";
/** state name of backup suffix */
const STATE_BACKUP_SUFFIX = "backup-suffix";
const STATE_CREATED_FILES = "created-files";
/**
* create backup suffix name
* @param dirName directory to back up
* @returns backup suffix
*/
export function createBackupSuffix(dirName: string): string {
if (!fs.existsSync(dirName)) {
// do nothing if directory does not exist
return "";
}
const backupSuffix = `.bak-${Date.now()}`;
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
return backupSuffix;
}
/**
* get backup suffix name
* @returns backup suffix (if not, empty string)
*/
export function getBackupSuffix(): string {
return core.getState(STATE_BACKUP_SUFFIX);
}
/**
* save created file names
* @param fileNames array of file names
*/
export function saveCreatedFileNames(fileNames: string[]): void {
const json = JSON.stringify(fileNames);
core.saveState(STATE_CREATED_FILES, json);
}
/**
* save created file names
* @returns saved array of file names
*/
export function loadCreatedFileNames(): string[] {
const json = core.getState(STATE_CREATED_FILES);
return JSON.parse(json) as string[];
}
/**
* get SSH directory
* @returns SSH directory name
*/
export function getSshDirectory(): string {
return path.resolve(getHomeDirectory(), ".ssh");
}
/**
* get home directory
* @returns home directory name
*/
function getHomeDirectory(): string {
const homedir = os.homedir();
if (homedir === "/github/home") {
// Docker container
return "/root";
}
return homedir;
}

View file

@ -1,14 +1,23 @@
import fs from "fs";
import path from "path";
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import * as common from "./common";
/** file creation info */
interface FileInfo {
/** file name */
name: string;
/** file contents */
contents: string;
/** creation options */
options: fs.WriteFileOptions;
/** file must not exist when creating */
mustNotExist: boolean;
}
/** default known_hosts */
const KNOWN_HOSTS = [
"github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=",
];
@ -24,34 +33,7 @@ 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 {
export function main(): void {
// parameters
const key = core.getInput("key", {
required: true,
@ -64,7 +46,12 @@ function setup(): void {
const ifKeyExists = core.getInput("if_key_exists");
// create ".ssh" directory
const sshDirName = createSshDirectory();
const sshDirName = common.getSshDirectory();
const backupSuffix = common.createBackupSuffix(sshDirName);
if (backupSuffix === "") {
createDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
}
// files to be created
const files: FileInfo[] = [
@ -75,6 +62,7 @@ function setup(): void {
mode: 0o644,
flag: "a",
},
mustNotExist: false,
},
];
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
@ -85,6 +73,7 @@ function setup(): void {
mode: 0o400,
flag: "wx",
},
mustNotExist: true,
});
}
if (config !== "") {
@ -95,93 +84,64 @@ function setup(): void {
mode: 0o644,
flag: "a",
},
mustNotExist: false,
});
}
// create files
const createdFileNames: string[] = [];
const backedUpFileNames: string[] = [];
for (const file of files) {
const fileName = path.join(sshDirName, file.name);
if (backup(fileName, backupSuffix, file.mustNotExist)) {
backedUpFileNames.push(file.name);
}
fs.writeFileSync(fileName, file.contents, file.options);
createdFileNames.push(file.name);
}
common.saveCreatedFileNames(createdFileNames);
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
console.log(`✅Following files have been created in "${sshDirName}" successfully; ${createdFileNames.join(", ")}`);
if (backedUpFileNames.length > 0) {
console.log(`✅Following files have been backed up in suffix "${backupSuffix}" successfully; ${backedUpFileNames.join(", ")}`);
}
}
/**
* cleanup function
* create directory
* @param dirName directory name to remove
*/
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 dirName = getSshDirectory();
function createDirectory(dirName: string): void {
fs.mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
return dirName;
}
/**
* remove ".ssh" directory
* @returns removed directory name
* back up file
* @param fileName file to back up
* @param backupSuffix suffix
* @param removeOrig remove original file
* @returns is file backed up?
*/
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
*/
function getHomeDirectory(): string {
const homeEnv = getHomeEnv();
const home = process.env[homeEnv];
if (home === undefined) {
throw Error(`${homeEnv} is not defined`);
function backup(fileName: string, backupSuffix: string, removeOrig: boolean): boolean {
if (backupSuffix === "") {
return false;
}
if (!fs.existsSync(fileName)) {
return false;
}
if (home === "/github/home") {
// Docker container
return "/root";
// move -> copy (in order to keep permissions when restore)
const fileNameBak = `${fileName}${backupSuffix}`;
fs.renameSync(fileName, fileNameBak);
if (!removeOrig) {
fs.copyFileSync(fileNameBak, fileName);
}
return home;
}
/**
* get HOME environment name
* @returns HOME environment name
*/
function getHomeEnv(): string {
if (process.platform === "win32") {
// Windows
return "USERPROFILE";
}
// macOS / Linux
return "HOME";
return true;
}
/**
@ -222,8 +182,7 @@ function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean
switch (ifKeyExists) {
case "replace":
// remove file and should create if replace
fs.unlinkSync(keyFilePath);
// should create if replace (existing file will be backed up when creating)
return true;
case "ignore":

85
src/post.ts Normal file
View file

@ -0,0 +1,85 @@
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import * as common from "./common";
try {
post();
} catch (err) {
if (err instanceof Error) {
core.setFailed(err);
}
}
/**
* cleanup function
*/
export function post(): void {
const sshDirName = common.getSshDirectory();
const backupSuffix = common.getBackupSuffix();
if (backupSuffix === "") {
// remove ".ssh" directory if suffix is not set
removeDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been removed successfully.`);
} else {
// remove created files and restore from backup
const removedFileNames = removeCreatedFiles(sshDirName);
console.log(`✅Following files have been removed successfully; ${removedFileNames.join(", ")}`);
const restoredFileNames = restoreFiles(sshDirName, backupSuffix);
console.log(`✅Following files in suffix "${backupSuffix}" have been restored successfully; ${restoredFileNames.join(", ")}`);
}
}
/**
* remove directory
* @param dirName directory name to remove
*/
function removeDirectory(dirName: string): void {
fs.rmSync(dirName, {
recursive: true,
force: true,
});
}
/**
* remove created files in main phase
* @param dirName directory name
* @returns removed file names
*/
function removeCreatedFiles(dirName: string): string[] {
const createdFileNames = common.loadCreatedFileNames();
for (const fileName of createdFileNames) {
const pathName = path.join(dirName, fileName);
fs.rmSync(pathName);
}
return createdFileNames;
}
/**
* restore files from backups
* @param dirName directory name
* @param backupSuffix suffix of backup directory
* @returns restored file names
*/
function restoreFiles(dirName: string, backupSuffix: string): string[] {
const restoredFileNames: string[] = [];
const entries = fs.readdirSync(dirName)
.filter((entry) => {
// skip if not a backed-up file
return entry.endsWith(backupSuffix);
});
for (const entry of entries) {
const entryOrg = entry.substring(0, entry.length - backupSuffix.length);
const pathNameOrg = path.join(dirName, entryOrg);
const pathNameBak = path.join(dirName, entry);
fs.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(entryOrg);
}
return restoredFileNames;
}

View file

@ -7,7 +7,7 @@
"es6"
],
"sourceMap": true,
"outDir": "./lib",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,