Skip to content

Commit 286a349

Browse files
authored
refactor(workflow): enhance SSH setup and cleanup logic (#11)
- Reorganized the workflow to improve clarity and maintainability: - Added a new `noop.sh` script for setup post-check of known hosts. - Moved the `Setup SSH key` step after `Check known hosts file` for better sequence. - Improved the `check.sh` script: - Validates the presence of expected SSH fingerprints in the `known_hosts` file. - Updated the `post_action.sh` script: - Simplified removal of the `known_hosts` file instead of line-by-line cleanup. - Adjusted `post_check.sh` to ensure the `known_hosts` file is completely removed. - Updated `action.yml` to include corrected paths and a reordering of the steps.
1 parent 6a9ff81 commit 286a349

File tree

6 files changed

+34
-27
lines changed

6 files changed

+34
-27
lines changed

.github/workflows/pull_request.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ jobs:
1414
runs-on: ${{ matrix.os }}
1515
steps:
1616
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
17-
- name: Setup SSH key
18-
uses: ./
19-
with:
20-
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
21-
ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
22-
log-public-key: false
23-
- name: Check known hosts file
17+
- name: Setup post check of known hosts file
2418
uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
2519
env:
2620
SSH_KNOWN_HOSTS: ${{ secrets.SSH_KNOWN_HOSTS }}
2721
with:
2822
main: |
29-
sh check.sh
23+
sh noop.sh
3024
post: |
3125
sh post_check.sh
26+
- name: Setup SSH key
27+
uses: ./
28+
with:
29+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
30+
ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }}
31+
log-public-key: false
32+
- name: Check known hosts file
33+
shell: sh
34+
run: |
35+
sh check.sh
3236
- name: Install docker (Missing on MacOS)
3337
if: runner.os == 'macos'
3438
shell: bash

action.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ inputs:
3535
runs:
3636
using: 'composite'
3737
steps:
38-
- uses: webfactory/ssh-agent@dc588b651fe13675774614f8e6a936a468676387 # v0.9.0
39-
with:
40-
ssh-private-key: ${{ inputs.ssh-private-key }}
41-
ssh-auth-sock: ${{ inputs.ssh-auth-sock }}
42-
log-public-key: ${{ inputs.log-public-key }}
43-
ssh-agent-cmd: ${{ inputs.ssh-agent-cmd }}
44-
ssh-add-cmd: ${{ inputs.ssh-add-cmd }}
45-
git-cmd: ${{ inputs.git-cmd }}
4638
- uses: pyTooling/Actions/with-post-step@9ceefdbf5dceae8c441fc393ed82344c7ca8bbdb # v3.1.1
4739
env:
4840
SSH_HOST: ${{ inputs.ssh-host }}
@@ -53,6 +45,14 @@ runs:
5345
sh "${{ github.action_path }}/action.sh"
5446
post: |
5547
sh "${{ github.action_path }}/post_action.sh"
48+
- uses: webfactory/ssh-agent@dc588b651fe13675774614f8e6a936a468676387 # v0.9.0
49+
with:
50+
ssh-private-key: ${{ inputs.ssh-private-key }}
51+
ssh-auth-sock: ${{ inputs.ssh-auth-sock }}
52+
log-public-key: ${{ inputs.log-public-key }}
53+
ssh-agent-cmd: ${{ inputs.ssh-agent-cmd }}
54+
ssh-add-cmd: ${{ inputs.ssh-add-cmd }}
55+
git-cmd: ${{ inputs.git-cmd }}
5656
branding:
5757
icon: loader
5858
color: 'purple'

check.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/bin/env sh
22

3-
if [ ! -s "${HOME}/.ssh/known_hosts" ]; then
3+
SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"
4+
5+
if ! grep -q "${SSH_KNOWN_HOSTS}" "${SSH_KNOWN_HOSTS_FILE}"; then
46
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Assertion Error::\
5-
~/.ssh/known_hosts is missing or empty."
7+
${SSH_KNOWN_HOSTS_FILE} file should contain the ssh fingerprint."
68
exit 1
7-
fi
9+
fi
10+
11+
unset SSH_KNOWN_HOSTS_FILE

noop.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
3+
exit 0

post_action.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
#!/usr/bin/env sh
22

33
SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"
4-
TEMP_FILE="/tmp/718f4157-5493-43b2-837b-3ccb27f78e7b"
54

6-
sed '$ d' "${SSH_KNOWN_HOSTS_FILE}" > "${TEMP_FILE}"
7-
cat "${TEMP_FILE}" > "${SSH_KNOWN_HOSTS_FILE}"
8-
rm -rf "${TEMP_FILE}"
5+
rm -rf "${SSH_KNOWN_HOSTS_FILE}"
96

107
echo "::notice file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Notice::\
11-
${SSH_KNOWN_HOSTS_FILE} has been cleaned."
8+
${SSH_KNOWN_HOSTS_FILE} has been removed."
129

1310
unset SSH_KNOWN_HOSTS_FILE
14-
unset TEMP_FILE

post_check.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
SSH_KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"
44

5-
if ! grep -q "${SSH_KNOWN_HOSTS}" "${SSH_KNOWN_HOSTS_FILE}" ; then
5+
if [ -s "${SSH_KNOWN_HOSTS_FILE}" ] ; then
66
echo "::error file=$(basename "$0"),line=${LINENO},endLine=${LINENO},title=Assertion Error::\
7-
${SSH_KNOWN_HOSTS_FILE} file should not contain the ssh fingerprint after the job."
7+
${SSH_KNOWN_HOSTS_FILE} file should be removed."
88
exit 1
99
fi
1010

0 commit comments

Comments
 (0)