Skip to content

Commit f0e5a23

Browse files
committed
ci: add environment variable handling in GitHub Actions
- Add a new job `testing-with-env` to the GitHub Actions workflow - Add steps to set up environment variables for public and private SSH keys - Add a step to create a new SSH server using Docker - Add a step to test the SSH connection using the `id_ed25519` key - Add a step to pass a single environment variable to the SSH action - Add a step to pass multiple environment variables to the SSH action - Add a step to use a custom format for environment variables - Add a step to pass all environment variables to the SSH action Signed-off-by: appleboy <[email protected]>
1 parent fc1c1fc commit f0e5a23

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

.github/workflows/ssh-server.yml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,126 @@ jobs:
337337
script: |
338338
whoami
339339
ls -al
340+
341+
testing-with-env:
342+
runs-on: ubuntu-latest
343+
steps:
344+
- name: Checkout code
345+
uses: actions/checkout@v4
346+
347+
- name: add public key to env
348+
run: |
349+
echo "PUBLIC_KEY<<EOF" >> $GITHUB_ENV
350+
cat testdata/.ssh/id_ed25519.pub >> $GITHUB_ENV
351+
echo "EOF" >> $GITHUB_ENV
352+
echo "======= public key ========="
353+
cat testdata/.ssh/id_ed25519.pub
354+
echo "============================"
355+
echo "PRIVATE_KEY<<EOF" >> $GITHUB_ENV
356+
cat testdata/.ssh/id_ed25519 >> $GITHUB_ENV
357+
echo "EOF" >> $GITHUB_ENV
358+
echo "======= private key ========="
359+
cat testdata/.ssh/id_ed25519
360+
echo "============================"
361+
362+
- name: create new ssh server
363+
run: |
364+
docker run -d \
365+
--name=openssh-server \
366+
--hostname=openssh-server \
367+
-p 2222:2222 \
368+
-e PUBLIC_KEY="${{ env.PUBLIC_KEY }}" \
369+
-e SUDO_ACCESS=false \
370+
-e PASSWORD_ACCESS=true \
371+
-e USER_PASSWORD=password \
372+
-e USER_NAME=linuxserver.io \
373+
--restart unless-stopped \
374+
lscr.io/linuxserver/openssh-server:latest
375+
docker exec openssh-server sh -c "hostname -i" > ip.txt
376+
echo "REMOTE_HOST<<EOF" >> $GITHUB_ENV
377+
cat ip.txt >> $GITHUB_ENV
378+
echo "EOF" >> $GITHUB_ENV
379+
echo "======= container ip address ========="
380+
cat ip.txt
381+
echo "======================================"
382+
sleep 2
383+
384+
- name: testing id_ed25519 key
385+
uses: appleboy/[email protected]
386+
with:
387+
host: ${{ env.REMOTE_HOST }}
388+
username: linuxserver.io
389+
key: ${{ env.PRIVATE_KEY }}
390+
port: 2222
391+
script: |
392+
whoami
393+
ls -al
394+
395+
- name: pass environment
396+
uses: appleboy/[email protected]
397+
env:
398+
FOO: "BAR"
399+
with:
400+
host: ${{ env.REMOTE_HOST }}
401+
username: linuxserver.io
402+
key: ${{ env.PRIVATE_KEY }}
403+
port: 2222
404+
envs: FOO
405+
script: |
406+
echo "I am $FOO, thanks"
407+
echo "I am $BAR, thanks"
408+
409+
- name: pass multiple environment
410+
uses: appleboy/[email protected]
411+
env:
412+
FOO: "BAR"
413+
BAR: "FOO"
414+
SHA: ${{ github.sha }}
415+
PORT: ${{ secrets.PORT }}
416+
with:
417+
host: ${{ env.REMOTE_HOST }}
418+
username: linuxserver.io
419+
key: ${{ env.PRIVATE_KEY }}
420+
port: 2222
421+
envs: FOO,BAR,SHA,PORT
422+
script: |
423+
echo "I am $FOO, thanks"
424+
echo "I am $BAR, thanks"
425+
echo "sha: $SHA"
426+
echo "port: $PORT"
427+
sh test.sh
428+
429+
- name: custom envs format
430+
uses: appleboy/[email protected]
431+
env:
432+
FOO: "BAR"
433+
AAA: "BBB"
434+
with:
435+
host: ${{ env.REMOTE_HOST }}
436+
username: linuxserver.io
437+
key: ${{ env.PRIVATE_KEY }}
438+
port: 2222
439+
envs: FOO,BAR,AAA
440+
envs_format: export TEST_{NAME}={VALUE}
441+
script: |
442+
echo "I am $TEST_FOO, thanks"
443+
echo "I am $TEST_BAR, thanks"
444+
echo "I am $BAR, thanks"
445+
echo "I am $TEST_AAA, thanks"
446+
447+
- name: pass all ENV variables to script
448+
uses: appleboy/[email protected]
449+
env:
450+
INPUT_FOO: "BAR"
451+
INPUT_AAA: "BBB"
452+
with:
453+
host: ${{ env.REMOTE_HOST }}
454+
username: linuxserver.io
455+
key: ${{ env.PRIVATE_KEY }}
456+
port: 2222
457+
allenvs: true
458+
script: |
459+
echo "I am $INPUT_FOO, thanks"
460+
echo "I am $INPUT_AAA, thanks"
461+
echo "$GITHUB_BASE_REF"
462+
echo "$GITHUB_REF"

0 commit comments

Comments
 (0)