安装gitlab-runner
安装gitrunner
# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Give it permissions to execute
sudo chmod +x /usr/local/bin/gitlab-runner
# Create a GitLab CI user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
# Install and run as service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
#上面两步如果提示 cammand not found,需要配置环境变量,也可以忽略上面两步,直接注册
vim /etc/profile
JAVA_HOME=/opt/jdk1.8.0_211
JAVA_BIN=$JAVA_HOME/bin
JRE_HOME=$JAVA_HOME/jre
JRE_BIN=$JRE_HOME/bin
GIT_RUNNER=/usr/local/bin/git-runner
MAVEN_HOME=/opt/maven-3.3.9/bin
PATH=$JAVA_BIN:$JRE_BIN:$PATH:$GIT_RUNNER:$MAVEN_HOME
export JAVA_HOME JRE_HOME PATH
source /etc/profile
注册 gitrunner
sudo gitlab-runner register --url http://192.168.26.134/ --registration-token $REGISTRATION_TOKEN
参数来源于
注册 runner
[root@localhost bin]# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=21527 revision=4e1f20da version=13.4.0
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.56.10/
Please enter the gitlab-ci token for this runner:
PwF1sZPX_zsB-xChSKjH
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: test ci cd desc
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,other-tag
Registering runner... succeeded runner=PwF1sZPX
Please enter the executor: ssh, virtualbox, parallels, shell, docker-ssh, docker+machine, docker-ssh+machine, kubernetes, custom, docker:
docker
Please enter the default Docker image (e.g. ruby:2.6):
maven:3.3.9-jdk-8
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
刷新gitlab页面
安装maven
wget https://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
tar -zxvf apache-maven-3.3.9-bin.tar.gz
mv apache-maven-3.3.9 maven-3.3.9
#参考上面的环境变量添加
快速获取springboot项目
https://start.spring.io/
增加配置
clean-container.sh
#!/bin/bash
docker rm -f $1
exit 0
clean-image.sh
#!/bin/bash
docker images | grep -q "$1"
if [ $? -eq 0 ]; then docker rmi $1:$2 ; fi
exit 0
.gitlab-ci.yml
variables:
GIT_STRATEGY: fetch
project_name: "online-taxi"
service_name: "demo"
port: 9050
stages:
- build
- deploy
############################## HW DEV ENV ##############################
dev-hw:build:
tags:
- dev-hw
stage: build
script:
- echo -e "\n[$CI_JOB_NAME] $(date) ($CI_BUILD_REF_NAME) ($CI_BUILD_STAGE)\n" | tee -a /tmp/test.txt
- mvn clean package
- sh scripts/clean-image.sh $service_name latest
- docker build -f Dockerfile -t $service_name:latest .
when:
manual
dev-hw:deploy:
tags:
- dev-hw
stage: deploy
needs: [ "dev-hw:build" ]
script:
- echo -e "\n[$CI_JOB_NAME] $(date) ($CI_BUILD_REF_NAME) ($CI_BUILD_STAGE)\n" | tee -a /tmp/test.txt
- sh scripts/clean-container.sh $service_name
- docker create -m 1G --name $service_name -p $port:$port -e "SPRING_PROFILES_ACTIVE=dev-hw" -v /var/log/$project_name:/var/log/$project_name $service_name:latest
- docker start $service_name
when:
manual
Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
VOLUME /var/log/online-taxi
ADD target/demo-2.6.2.jar demo.jar
EXPOSE 9050
ENV TZ Asia/Shanghai
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add tzdata ttf-dejavu fontconfig \
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& apk del tzdata
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/demo.jar"]