66 lines
2.0 KiB
YAML
66 lines
2.0 KiB
YAML
name: Build, Test and Push Docker Image
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
|
|
jobs:
|
|
e2e-test:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Build Docker image
|
|
run: docker build -t ssh-server-test .
|
|
|
|
- name: Run SSH server
|
|
run: |
|
|
docker rm -f ssh-server-test || true
|
|
docker network rm test-net || true
|
|
docker network create test-net
|
|
docker run -d --network test-net --name ssh-server-test -e COMMAND="/bin/sh" ssh-server-test
|
|
sleep 2
|
|
if ! docker ps | grep -q ssh-server-test; then
|
|
echo "Container failed to start"
|
|
docker logs ssh-server-test
|
|
exit 1
|
|
fi
|
|
|
|
- name: Test SSH connection
|
|
run: |
|
|
ip=$(docker inspect ssh-server-test | jq -r '.[0].NetworkSettings.Networks."test-net".IPAddress')
|
|
echo "Server IP: $ip"
|
|
output=$( { docker run --rm --network test-net alpine sh -c "apk add --no-cache openssh-client && ssh -p 22 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 root@$ip 'echo test559920' 2>/dev/null" ; } 2>&1 ; true )
|
|
echo "Captured output: '$output'"
|
|
if echo "$output" | grep -q "test559920"; then
|
|
echo "SSH connection test passed"
|
|
else
|
|
echo "SSH connection test failed: $output"
|
|
exit 1
|
|
fi
|
|
docker stop ssh-server-test && docker rm ssh-server-test && docker network rm test-net
|
|
|
|
build-and-push:
|
|
runs-on: ubuntu-latest
|
|
needs: e2e-test
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Log in to Docker Hub
|
|
uses: docker/login-action@v2
|
|
with:
|
|
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
|
password: ${{ secrets.DOCKER_HUB_TOKEN }}
|
|
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v4
|
|
with:
|
|
context: .
|
|
push: true
|
|
tags: dcorral3/go-ssh-server-command:latest
|