提交 ff7cf025 编写于 作者: 郝钰

add ci files

上级 ab728ff7
stages:
- build
- deploy
maven-build:
image: maven:3-jdk-8
stage: build
script:
- cd complete
- mvn clean package -B -Dmaven.test.skip=true
artifacts:
paths:
- complete/target/*.jar
production:
image: maven:3-jdk-8
stage: deploy
before_script:
- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config'
script:
# 在 web 服务器上创建应用目录
- ssh root@$APP_HOST "[[ -d /usr/local/apps/hello-world ]] || { mkdir -p /usr/local/apps/hello-world; }"
# 将应用部署启动脚本到 web 服务器上
- cat .gitlab-ci/run.sh | ssh root@$APP_HOST "cat > /usr/local/apps/hello-world/run.sh; chmod +x /usr/local/apps/hello-world/run.sh"
# 复制 jar 文件到服务器
- scp complete/target/spring-boot-complete-0.0.1-SNAPSHOT.jar root@$APP_HOST:/usr/local/apps/hello-world/hello-world.jar
# 部署应用为系统服务
- cat .gitlab-ci/hello-world.service | ssh root@$APP_HOST "cat > ./hello-world.service "
- ssh root@$APP_HOST "[[ -f /etc/systemd/system/hello-world.service ]] || { mv ./hello-world.service /etc/systemd/system/hello-world.service && systemctl daemon-reload && systemctl enable hello-world.service; }"
- ssh root@$APP_HOST "[[ -f ./hello-world.service ]] || { rm ./hello-world.service; }"
# 重启应用服务
- ssh root@$APP_HOST "systemctl restart hello-world"
# 查看服务状态
- ssh root@$APP_HOST "systemctl status hello-world"
only:
- main
#!/bin/bash
ssh root@web1 "[[ -d /usr/local/apps/hello-world ]] || { mkdir -p /usr/local/apps/hello-world; }"
cat .gitlab-ci/run.sh | ssh root@web1 "cat > /usr/local/apps/hello-world/run.sh; chmod +x /usr/local/apps/hello-world/run.sh"
scp complete/target/spring-boot-complete-0.0.1.jar root@web1:/usr/local/apps/hello-world/hello-world.jar
cat .gitlab-ci/hello-world.service | ssh root@web1 "cat > ./hello-world.service "
ssh root@web1 "[[ -f /etc/systemd/system/hello-world.service ]] || { mv ./hello-world.service /etc/systemd/system/hello-world.service && systemctl daemon-reload && systemctl enable hello-world.service && systemctl restart hello-world.service; }"
ssh root@web1 "[[ -f ./hello-world.service ]] && { rm ./hello-world.service; }"
[Unit]
Description=hello-world
After=syslog.target
[Service]
User=root
ExecStart=/usr/local/apps/hello-world/run.sh
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
\ No newline at end of file
#!/bin/bash
cd /usr/local/apps/hello-world
java -jar hello-world.jar
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册