Skip to content

Commit 731538b

Browse files
authored
Merge pull request #3 from ustclug/testdrive
Add Testdrive CI
2 parents 530ade9 + e16f4bf commit 731538b

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

.github/workflows/docker-image.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ name: Docker image automatic build (ghcr)
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches:
6+
- '**'
67
pull_request:
7-
branches: [ master ]
8+
branches:
9+
- master
810

911
env:
1012
REGISTRY: ghcr.io
@@ -15,9 +17,17 @@ jobs:
1517
runs-on: ubuntu-latest
1618
steps:
1719
- uses: actions/checkout@v2
18-
- name: Build the Docker image (PR only)
19-
if: github.ref != 'refs/heads/master'
20-
run: docker build . --file Dockerfile --tag ${{ env.IMAGE_NAME }}:$(date +%s)
20+
- name: Set tag for testing
21+
run: |
22+
echo "TAG=$(date +%s)" >> $GITHUB_ENV
23+
- name: Build the Docker image (for testing)
24+
run: |
25+
docker build . --file Dockerfile --tag ${{ env.IMAGE_NAME }}:$TAG
26+
- name: Testdrive
27+
run: |
28+
TAG=$TAG ./testdrive.sh
29+
env:
30+
IMAGE_NAME: ${{ env.IMAGE_NAME }}
2131
- name: Login to ghcr
2232
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
2333
if: github.ref == 'refs/heads/master'

testdrive.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash -ex
2+
3+
SUFFIX="$RANDOM"
4+
5+
cleanup() {
6+
docker stop "gitlab-${SUFFIX}" || kill -TERM "$(jobs -pr)" || true
7+
docker stop "gitlab-redis-${SUFFIX}" || true
8+
docker stop "gitlab-postgresql-${SUFFIX}" || true
9+
}
10+
11+
trap cleanup EXIT
12+
13+
docker run --rm --name "gitlab-postgresql-${SUFFIX}" -d \
14+
--env 'DB_NAME=gitlabhq_production' \
15+
--env 'DB_USER=gitlab' --env 'DB_PASS=password' \
16+
--env 'DB_EXTENSION=pg_trgm,btree_gist' \
17+
sameersbn/postgresql:14-20230628
18+
docker run --rm --name "gitlab-redis-${SUFFIX}" -d \
19+
--volume /srv/docker/gitlab/redis:/data \
20+
redis:6.2
21+
docker run --rm --name "gitlab-${SUFFIX}" \
22+
--link "gitlab-postgresql-${SUFFIX}:postgresql" --link "gitlab-redis-${SUFFIX}:redisio" \
23+
--publish 10022:22 --publish 10080:80 \
24+
--env 'GITLAB_PORT=10080' --env 'GITLAB_SSH_PORT=10022' \
25+
--env 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
26+
--env 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
27+
--env 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
28+
--env OAUTH2_GENERIC_USTC_APP_ID=1234 \
29+
--env OAUTH2_GENERIC_USTC_APP_SECRET=example \
30+
--env 'OAUTH2_GENERIC_USTC_LABEL=example oauth' \
31+
--env OAUTH2_GENERIC_USTC_CLIENT_SITE=https://example.com \
32+
--env OAUTH2_GENERIC_USTC_CLIENT_USER_INFO_URL=/userinfo \
33+
--env OAUTH2_GENERIC_USTC_CLIENT_AUTHORIZE_URL=/authorize \
34+
--env OAUTH2_GENERIC_USTC_CLIENT_TOKEN_URL=/token \
35+
--env OAUTH2_GENERIC_USTC_ID_PATH=gid \
36+
"$IMAGE_NAME":"$TAG" &
37+
38+
check() {
39+
local url="http://localhost:10080"
40+
status_code=$(curl --write-out '%{http_code}' --silent --output /dev/null "$url")
41+
# Check if the status code is not in the success range (200-399)
42+
if [[ $status_code -lt 200 || $status_code -gt 399 ]]; then
43+
echo "Error: Failed to access $url (status code: $status_code)"
44+
return 1
45+
fi
46+
ret=$(docker exec "gitlab-${SUFFIX}" cat /home/git/gitlab/config/gitlab.yml | grep -c 'example oauth')
47+
if [[ $ret -ne 1 ]]; then
48+
echo "Error: Failed to find 'example oauth' in gitlab.yml"
49+
return 1
50+
fi
51+
return 0
52+
}
53+
54+
RETRIES="48"
55+
RETRIED=0
56+
WAIT_TIME="5s"
57+
58+
until check || { [[ "$((RETRIED++))" == "${RETRIES}" ]] && exit 1; } ; do
59+
sleep "${WAIT_TIME}"
60+
done

0 commit comments

Comments
 (0)