ci: add tests #65
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deployement Tests | |
on: | |
push: | |
branches: | |
- "*" | |
pull_request: | |
types: [opened, synchronize] | |
branches: | |
- "*" | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
env: | |
CONTAINER_NAME: "docker_throw_ssh" | |
CONTAINER_SSH_PORT: "22" | |
SSH_KEY_TYPE: "ed25519" | |
SSH_KEY_PATH: "$HOME/.ssh/id_ed25519" | |
REMOTE_SSH_HOST: "127.0.0.1" | |
REMOTE_SSH_PORT: "2222" | |
REMOTE_SSH_USER: "president" | |
REMOTE_SSH_PASSWORD: "P@ssword!" | |
services: | |
docker_throw_ssh: | |
image: ghcr.io/tristiisch/docker_throw_ssh:latest | |
ports: | |
- 2222:22 | |
options: > | |
--privileged | |
--tty | |
--cap-add=NET_ADMIN | |
--cap-add=NET_RAW | |
--tmpfs /run | |
--tmpfs /run/lock | |
--tmpfs /tmp | |
volumes: | |
- /sys/fs/cgroup:/sys/fs/cgroup | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 1 | |
- name: Wait for SSH server | |
run: | | |
docker ps -a | |
if [ -n ACT ]; then | |
docker ps -aq -f name=${{ env.CONTAINER_NAME }} | xargs -r docker rm -f | |
sudo apt update &> /dev/null | |
sudo apt install -y netcat &> /dev/null | |
fi | |
echo "CONTAINER_NAME=$(docker ps --format '{{.Names}}' | head -n 1)" >> $GITHUB_ENV | |
echo "Waiting for SSH server at ${{ env.REMOTE_SSH_HOST }}:${{ env.REMOTE_SSH_PORT }}..." | |
timeout 30 bash -c 'while ! nc -zv ${{ env.REMOTE_SSH_HOST }} ${{ env.REMOTE_SSH_PORT }}; do sleep 1; done' | |
echo "SSH server port is open." | |
ssh-keyscan -v -t ${{ env.SSH_KEY_TYPE }} -p ${{ env.REMOTE_SSH_PORT }} ${{ env.REMOTE_SSH_HOST }} || true | |
echo "SSH server is available." | |
- name: Retrieve SSH Public Key | |
run: | | |
PUBLIC_KEY=$(ssh-keyscan -t ${{ env.SSH_KEY_TYPE }} -p ${{ env.REMOTE_SSH_PORT }} ${{ env.REMOTE_SSH_HOST }}) | |
echo "SSH_KEY_PUBLIC_VALUE=${PUBLIC_KEY}" >> $GITHUB_ENV | |
- name: Generate an ssh-key & copy ssh-key in container | |
run: | | |
if [ -n ACT ]; then | |
sudo apt update | |
sudo apt install -y sshpass | |
fi | |
ssh-keygen -t "${{ env.SSH_KEY_TYPE }}" -f "${{ env.SSH_KEY_PATH }}" -N "" -o -a 100 | |
echo "SSH_KEY_PUBLIC=$(cat ${{ env.SSH_KEY_PATH }}.pub)" >> $GITHUB_ENV | |
sshpass -p "${{ env.REMOTE_SSH_PASSWORD }}" \ | |
ssh-copy-id -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \ | |
-i "${{ env.SSH_KEY_PATH }}.pub" \ | |
-p "${{ env.REMOTE_SSH_PORT }}" \ | |
"${{ env.REMOTE_SSH_USER }}@${{ env.REMOTE_SSH_HOST }}" | |
- name: Print logs on error | |
if: failure() | |
run: | | |
echo "Printing container logs" | |
docker logs ${{ env.CONTAINER_NAME }} || (echo "No container ${{ env.CONTAINER_NAME }} found" && exit 1) | |
echo "Printing service status" | |
docker exec ${{ env.CONTAINER_NAME }} service sshd status || (echo "No sshd service found" && exit 1) | |
echo "Printing SSH logs from /var/log/auth.log" | |
docker exec ${{ env.CONTAINER_NAME }} cat /var/log/auth.log | grep sshd || (echo "No sshd logs found" && exit 1) | |
- name: Start Deployment 1 | |
uses: ./ | |
with: | |
deployment_mode: docker-swarm | |
remote_docker_host: "${{ env.REMOTE_SSH_HOST }}:${{ env.REMOTE_SSH_PORT }}" | |
remote_docker_username: ${{ env.REMOTE_SSH_USER }} | |
ssh_private_key: ${{ env.SSH_KEY_PUBLIC }} | |
ssh_public_key: ${{ env.SSH_KEY_PUBLIC_VALUE }} | |
stack_file_path: ./tests/docker-compose.yml | |
stack_name: nginx_1 |