Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix; files that didn't backed up is not removed in "post" phase #256

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/reusable-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,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 }}
Expand All @@ -181,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 }}
Expand All @@ -200,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
Expand All @@ -224,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 }}
Expand All @@ -243,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
Expand All @@ -268,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 }}
Expand Down
42 changes: 21 additions & 21 deletions dist/main.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/main.js.map

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions dist/post.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/post.js.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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
Expand All @@ -31,6 +32,24 @@ 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
Expand Down
26 changes: 20 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export function main(): void {
// create ".ssh" directory
const sshDirName = common.getSshDirectory();
const backupSuffix = common.createBackupSuffix(sshDirName);
fs.mkdirSync(sshDirName, {
recursive: true,
mode: 0o700,
});
if (backupSuffix === "") {
createDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
}

// files to be created
const files: FileInfo[] = [
Expand Down Expand Up @@ -89,6 +89,7 @@ export function main(): void {
}

// create files
const createdFileNames: string[] = [];
const backedUpFileNames: string[] = [];
for (const file of files) {
const fileName = path.join(sshDirName, file.name);
Expand All @@ -97,14 +98,27 @@ export function main(): void {
}

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 are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(", ")}`);
console.log(`Following files have been backed up in suffix "${backupSuffix}" successfully; ${backedUpFileNames.join(", ")}`);
}
}

/**
* create directory
* @param dirName directory name to remove
*/
function createDirectory(dirName: string): void {
fs.mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
}

/**
* back up file
* @param fileName file to back up
Expand Down
41 changes: 30 additions & 11 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,55 @@ try {
* cleanup function
*/
export function post(): void {
const sshDirName = common.getSshDirectory();
const backupSuffix = common.getBackupSuffix();
if (backupSuffix === "") {
// remove ".ssh" directory if suffix is not set
removeSshDirectory();
removeDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been removed successfully.`);
} else {
// restore files from backup suffix
restore(backupSuffix);
// 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 ".ssh" directory
* remove directory
* @param dirName directory name to remove
*/
function removeSshDirectory(): void {
const dirName = common.getSshDirectory();
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);

console.log(`SSH key in ${dirName} has been removed successfully.`);
fs.rmSync(pathName);
}
return createdFileNames;
}

/**
* restore files from backups
* @param dirName directory name
* @param backupSuffix suffix of backup directory
* @returns restored file names
*/
function restore(backupSuffix: string): void {
const dirName = common.getSshDirectory();
function restoreFiles(dirName: string, backupSuffix: string): string[] {
const restoredFileNames: string[] = [];
const entries = fs.readdirSync(dirName)
.filter((entry) => {
Expand All @@ -58,9 +78,8 @@ function restore(backupSuffix: string): void {
const pathNameOrg = path.join(dirName, entryOrg);
const pathNameBak = path.join(dirName, entry);

fs.rmSync(pathNameOrg);
fs.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(entryOrg);
}
console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`);
return restoredFileNames;
}
Loading