-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker-test.yaml
59 lines (50 loc) · 1.91 KB
/
docker-test.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
name: Docker Build Test
on:
pull_request:
env:
HEALTH_CHECK_TIMEOUT_SECONDS: 60
jobs:
docker-build-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build docker image
run: |
docker build -t test-build .
- name: Inspect Docker Container for Healthcheck
if: ${{ ! vars.DISABLE_HEALTHCHECK }} # set to true to disable HEALTHCHECK tests
run: |
HEALTHCHECK=$(docker inspect --format='{{json .Config.Healthcheck}}' test-build)
if [ "$HEALTHCHECK" = "null" ]; then
echo "::error::Image does not have a healthcheck configured"
exit 1
fi
- name: Test Docker Image
if: ${{ ! vars.DISABLE_HEALTHCHECK }}
run: |
timeout_seconds=${{env.HEALTH_CHECK_TIMEOUT_SECONDS}}
start_time=$(date +%s)
docker run --name test-container -d test-build
# Wait until the container is running or timeout occurs
while true; do
if [ "$(docker inspect -f {{.State.Health.Status}} test-container )" == "healthy" ]; then
echo "Container is Healthy."
break
fi
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ $elapsed_time -ge $timeout_seconds ]; then
echo "::error::Timeout occurred. Container didn't became healthy within $timeout_seconds seconds."
exit 1
fi
sleep 2
done
# Add more tests based on your requirements
- name: Show the Healtcheck and container logs
if: always()
run: |
(docker inspect -f '{{json .State.Health}}' test-container || true ) | jq '.'
docker logs test-container || true