main
Dragan Filipovic 2022-12-31 02:43:17 +01:00
parent fd8e654d7e
commit aeee708bee
6 changed files with 85 additions and 49 deletions

View File

@ -5,8 +5,9 @@ on:
branches: [ 'feature/add-tests' ] branches: [ 'feature/add-tests' ]
env: env:
GLOBAL_WORKFLOW_ENV: __some value for GLOBAL_WORKFLOW_ENV__
TEST_HOST_DOCKER: ./test TEST_HOST_DOCKER: ./test
TEST_USER: kaja TEST_USER: test
jobs: jobs:
e2e: e2e:
@ -16,6 +17,25 @@ jobs:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
# - name: Set ENV var
# run: |
# echo "CUSTOM_VAR=IT WORKS" >> $GITHUB_ENV
# - name: Use ENV var via `env` block
# run: |
# echo $CUSTOM_VAR # OK
# echo ${{ env.CUSTOM_VAR }} # OK
# echo $USE_CUSTOM_VAR # NOT OK, prints ''
# echo ${{ env.USE_CUSTOM_VAR }} # NOT OK, prints ''
# echo $USE_CUSTOM_VAR_2 # NOT OK, prints '$CUSTOM_VAR'
# echo ${{ env.USE_CUSTOM_VAR_2 }} # OK
# echo $USE_CUSTOM_VAR_3 # NOT OK, prints '$GLOBAL_WORKFLOW_ENV'
# echo ${{ env.USE_CUSTOM_VAR_3 }} # OK
# env:
# USE_CUSTOM_VAR: ${{ env.CUSTOM_VAR }}
# USE_CUSTOM_VAR_2: $CUSTOM_VAR
# USE_CUSTOM_VAR_3: $GLOBAL_WORKFLOW_ENV
- name: Clean up old test files - name: Clean up old test files
run: | run: |
docker stop ssh-host-container || true && docker rm ssh-host-container || true docker stop ssh-host-container || true && docker rm ssh-host-container || true
@ -37,9 +57,9 @@ jobs:
run: | run: |
docker build \ docker build \
-t ssh-host-image . \ -t ssh-host-image . \
--build-arg ssh_pub_key="$(cat $HOME/.ssh/id_rsa.pub)" \ --build-arg SSH_PUB_KEY="$(cat $HOME/.ssh/id_rsa.pub)" \
--build-arg ssh_user="${{ env.TEST_USER }}" --build-arg ssh_user="${{ env.TEST_USER }}"
docker run -d --name=ssh-host-container ssh-host-image docker run -d -p 8822:22 --name=ssh-host-container ssh-host-image
docker exec ssh-host-container sh -c "hostname --ip-address" > ip.txt docker exec ssh-host-container sh -c "hostname --ip-address" > ip.txt
echo "EXAMPLE_REMOTE_HOST<<EOF" >> $GITHUB_ENV echo "EXAMPLE_REMOTE_HOST<<EOF" >> $GITHUB_ENV
cat ip.txt >> $GITHUB_ENV cat ip.txt >> $GITHUB_ENV
@ -54,35 +74,36 @@ jobs:
date +"%Y-%m-%d %H:%M:%S,%3N" >> index.html date +"%Y-%m-%d %H:%M:%S,%3N" >> index.html
cat index.html cat index.html
- name: e2e Test ssh # - name: e2e Test local ssh-deploy action
run: | # run: |
ssh ${{ env.REMOTE_USER }}@${{ env.REMOTE_HOST }} # echo "${{ env.REMOTE_HOST }} -- $EXAMPLE_REMOTE_HOST -- ${{ env.EXAMPLE_REMOTE_HOST }}"
env: # npm ci
REMOTE_HOST: ${{ env.EXAMPLE_REMOTE_HOST }} # npm run build
REMOTE_USER: ${{ env.TEST_USER }} # node ./src/test.js || true
# env:
# SSH_PRIVATE_KEY: $EXAMPLE_SSH_PRIVATE_KEY
# ARGS: "-rltgoDzvO"
# SOURCE: "test_project/"
# REMOTE_HOST: $EXAMPLE_REMOTE_HOST
# REMOTE_USER: ${{ env.TEST_USER }}
# TARGET: "/var/www/html/"
# EXCLUDE: "/dist/, /node_modules/"
- name: e2e Test local ssh-deploy action # - name: e2e Test ssh
run: | # run: |
npm ci # echo "$SSH_REMOTE_USER@$SSH_REMOTE_HOST"
npm run build # ssh -p 8822 $SSH_REMOTE_USER@$SSH_REMOTE_HOST 'exit'
node ./src/index.js || true # env:
env: # SSH_REMOTE_HOST: ${{ env.EXAMPLE_REMOTE_HOST }}
SSH_PRIVATE_KEY: ${{ env.EXAMPLE_SSH_PRIVATE_KEY }} # SSH_REMOTE_USER: ${{ env.TEST_USER }}
ARGS: "-rltgoDzvO"
SOURCE: "test_project/"
REMOTE_HOST: ${{ env.EXAMPLE_REMOTE_HOST }}
REMOTE_USER: ${{ env.TEST_USER }}
TARGET: "/var/www/html/"
EXCLUDE: "/dist/, /node_modules/"
- name: e2e Test published ssh-deploy action - name: e2e Test published ssh-deploy action
uses: easingthemes/ssh-deploy@main uses: easingthemes/ssh-deploy@main
env: with:
SSH_PRIVATE_KEY: ${{ env.EXAMPLE_SSH_PRIVATE_KEY }} SSH_PRIVATE_KEY: $EXAMPLE_SSH_PRIVATE_KEY
ARGS: "-rltgoDzvO" ARGS: "-rltgoDzvO"
SOURCE: "test_project/" SOURCE: "test_project/"
REMOTE_HOST: ${{ env.EXAMPLE_REMOTE_HOST }} REMOTE_HOST: $EXAMPLE_REMOTE_HOST
REMOTE_USER: ${{ env.TEST_USER }} REMOTE_USER: ${{ env.TEST_USER }}
TARGET: "/var/www/html/" TARGET: "/var/www/html/"
EXCLUDE: "/dist/, /node_modules/" EXCLUDE: "/dist/, /node_modules/"

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,9 @@ const inputs = {
GITHUB_WORKSPACE: process.env.GITHUB_WORKSPACE GITHUB_WORKSPACE: process.env.GITHUB_WORKSPACE
}; };
// Get inputs from ENV or WITH workflow settings // Get inputs from ENV or WITH workflow settings
console.log('EXAMPLE_REMOTE_HOST: ', process.env.EXAMPLE_REMOTE_HOST);
console.log('REMOTE_HOST: ', process.env.REMOTE_HOST);
inputNames.forEach((input) => { inputNames.forEach((input) => {
inputs[input] = process.env[input] || process.env[`INPUT_${input}`]; inputs[input] = process.env[input] || process.env[`INPUT_${input}`];
}); });

View File

@ -1,9 +1,2 @@
const { console.log('EXAMPLE_REMOTE_HOST: ', process.env.EXAMPLE_REMOTE_HOST);
REMOTE_HOST, REMOTE_USER, console.log('REMOTE_HOST: ', process.env.REMOTE_HOST);
REMOTE_PORT, SSH_PRIVATE_KEY, DEPLOY_KEY_NAME,
SOURCE, TARGET, ARGS, EXCLUDE,
GITHUB_WORKSPACE
} = require('./inputs');
// eslint-disable-next-line max-len
console.log(REMOTE_HOST, REMOTE_USER, REMOTE_PORT, SSH_PRIVATE_KEY, DEPLOY_KEY_NAME, SOURCE, TARGET, ARGS, EXCLUDE, GITHUB_WORKSPACE);

View File

@ -1,15 +1,31 @@
FROM nginx FROM nginx
# Set args to get from Gtihub Action
ARG ssh_pub_key ARG SSH_PUB_KEY
ARG ssh_user
# Add a user to the container RUN apt update
RUN adduser --disabled-password $ssh_user
# USER $ssh_user RUN apt install openssh-server sudo -y
# Add the ssh public key to the container
RUN mkdir -p /home/$ssh_user/.ssh RUN useradd -rm -d /home/test -s /bin/bash -g root -G sudo -u 1000 test
RUN echo "$ssh_pub_key" > /home/$ssh_user/.ssh/authorized_keys
RUN chmod 700 /home/$ssh_user/.ssh RUN usermod -aG sudo test
# Start server
EXPOSE 80 RUN echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config.d/pub.conf
STOPSIGNAL SIGTERM RUN echo "AuthorizedKeysFile .ssh/authorized_keys" >> /etc/ssh/sshd_config.d/pub.conf
RUN mkdir -p /home/test/.ssh
RUN echo "$SSH_PUB_KEY" > /home/test/.ssh/authorized_keys
RUN chmod 700 /home/test/.ssh
RUN chown -R test /home/test/.ssh
RUN service ssh start
RUN echo 'test:test' | chpasswd
EXPOSE 22
ADD entrypoint.sh /docker-entrypoint.d/entrypoint.sh
RUN chmod +x /docker-entrypoint.d/entrypoint.sh
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]

3
test/entrypoint.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
/usr/sbin/sshd -D