-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.gitlab-ci.yml
58 lines (55 loc) · 2.02 KB
/
.gitlab-ci.yml
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
stages:
- build
- deploy
# 编译
build:
stage: build
image: 10.0.0.20/ponycool/php-docker-8.2:latest
# 使用docker in docker
# services:
# - docker:dind
variables:
DOCKER_IMAGE_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
before_script:
- docker login "$CI_REGISTRY_SERVER" -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"
- composer install
# 手动启动 DinD 容器,仅services 配置来启动 Docker-in-Docker无法执行时,使用手动启动DinD 容器
- CONTAINER_NAME=dind-service
# 尝试删除容器,如果不存在则忽略错误
- docker container rm -f $CONTAINER_NAME || true
- docker run --name $CONTAINER_NAME -d --privileged -p 127.0.0.1:2375:2375 -v /var/run/docker.sock:/var/run/docker.sock docker:dind
script:
# docker in docker跨平台编译遇到无法解决的错误,暂时使用手动编译
- docker build -t "$DOCKER_IMAGE_NAME" .
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
docker tag "$DOCKER_IMAGE_NAME" "$CI_REGISTRY_IMAGE:latest"
docker push "$CI_REGISTRY_IMAGE:latest"
fi
- |
if echo "$CI_COMMIT_TAG" | grep -Eq '^v[0-9]+\.[0-9]+'; then
echo "This is a release"
fi
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
exists:
- Dockerfile
# 部署
deploy:
stage: deploy
image: alpine
before_script:
- sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
- apk update && apk upgrade
- 'which ssh-agent || ( apk update && apk add openssh-client git )'
- eval $(ssh-agent -s)
- echo "$DEMO_SERVER_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $DEMO_SERVER_HOST >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- ssh $DEMO_SERVER_USER@$DEMO_SERVER_HOST
"cd /opt/ponycool/docker-compose && sudo docker compose pull && sudo docker compose down && sudo docker compose up -d"
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH