1、拉取代码并上传至gitlab中
[root@docker java-demo]# git clone https://codeup.aliyun.com/613aded82bf07942fbc24954/gani/dev.git
[root@docker java-demo]# cd gitlab-ci-k8s-demo/
[root@docker gitlab-ci-k8s-demo]# git init
[root@docker dev]# git remote add gitlab http://192.168.8.31/root/java-demo.git
[root@docker gitlab-ci-k8s-demo]# git push gitlab master
创建DockerFiles文件
FROM openjdk:8
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
WORKDIR /data/app
COPY ./target/hello.jar .
EXPOSE 80
ENTRYPOINT ["java","-jar","hello.jar"]
修改config.toml文件[因为前面部署挂载到 /data/app/gitlab-runner/config/]
vim /data/app/gitlab-runner/config/config.toml
创建.gitlab-ci.yml文件
image: docker # Pipeline中各个步骤阶段的构建镜像没有指定时, 默认使用docker:stable镜像
stages:
- package # 源码打包阶段
- docker_build # 镜像构建和打包推送阶段
- deploy_k8s # 应用部署阶段
variables:
KUBECONFIG: /etc/deploy/config # 定义全局变量KUBECONFIG
MAVEN_OPTS: "-Dmaven.repo.local=/opt/cache/.m2/repository"
MAVEN_CLI_OPTS: "-s settings.xml --batch-mode" #设置maven settings
mvn_build_job: # job名称
image: maven:3.6.2-jdk-14 # 本阶段构建使用的构建镜像
stage: package # 关联的阶段名称
tags: # GitLab Runner的tag
- docker
script:
- mvn package -B -DskipTests # 执行构建脚本
- pwd
- sleep 3600
- cp target/*.jar /opt/cache # 构建物保存至缓存区
docker_build_job:
image: docker
stage: docker_build
tags:
- docker
script:
- mkdir target
- pwd
- cp /opt/cache/hello.jar target/hello.jar
- docker build -t registry.cn-hangzhou.aliyuncs.com/fs-gani/hello:$CI_PIPELINE_ID .
- docker images |grep hello
- docker login --username=$hubor_name --password=$hubor_password registry.cn-hangzhou.aliyuncs.com
- docker push registry.cn-hangzhou.aliyuncs.com/fs-gani/hello:$CI_PIPELINE_ID
deploy_k8s_job: # job名称
image: rancher/cli:latest
stage: deploy_k8s # 关联的阶段名称
tags: # GitLab Runner的tag
- docker
script:
- pwd
- echo yes | rancher login https://192.168.8.21/k8s/clusters/c-psk44 --token token-m8j5q:svv2rtlkkwv2f6rlhj5k7mj75r2b6z8mslmmgjhzbhq9cn5fn7l94l --context c-psk44:p-hr86z
# # - mkdir -p /etc/deploy
# # - echo $kube_config |base64 -d > $KUBECONFIG # 配置连接Kubernetes集群的config文件
# # - sed -i "s/IMAGE_TAG/$CI_PIPELINE_ID/g" deployment.yaml # 动态替换部署文件中的镜像tag
- sed -i "s/IMAGE_TAG/21/g" deployment.yaml # 动态替换部署文件中的镜像tag
# - rancher kubectl --namespace=hello-java apply -f ./deployment.yaml
- rancher kubectl apply -f ./deployment.yaml
# # - cat deployment.yaml
# # - kubectl apply -f deployment.yaml
- sleep 3600
# - kubectl version
----------settings文件
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>maven</id>
<username>maven</username>
<password>Hi@user123</password>
</server>
<server>
<id>releases</id>
<username>maven</username>
<password>Hi@user123</password>
</server>
<server>
<id>snapshots</id>
<username>maven</username>
<password>Hi@user123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven</id>
<mirrorOf>*,!maven_nexus_201</mirrorOf>
<url>http://10.0.0.221:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>maven</id>
<repositories>
<repository>
<id>maven</id>
<name>maven repository</name>
<url>http://10.0.0.221:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven</id>
<name>maven plugin repository</name>
<url>http://10.0.0.221:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jdk18</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>maven</activeProfile>
</activeProfiles>
</settings>
<mirrors> | <profiles> | <activeProfiles> | 私服 | |
情况一 | 存在 | 存在 | 存在 | ok |
情况二 | 存在 | 存在 | 不存在 | ok |
情况三 | 存在 | 不存在 | 不存在 | ok |
情况四 | 不存在 | 存在 | 存在 | no |
maven设置settings的方式还可以通过Dockerfile生成一个image。
FROM maven:3.6.2-jdk-14
COPY settings.xml /usr/share/maven/ref/
定义变量
最后一步:执行
备注:rancher方式链接K8S方案参考
思考:这个pipeline中使用cp复制jar包。能否通过artifacts贯穿build和deploy两个步骤呢?