Gitlab-CI持续集成(根据不同代码分⽀,maven打包成docker,
推送到远程仓库。。。
基本概念:
持续集成 Continuous Integration
每次向存储库的推送,都会⾃动连续地构建和测试,以确保所引⼊的更改通过我们为应⽤程序建⽴的所有测试,准则和代码合规性标准。 Linux安装插件 GitLab-Runner:
1.下载适⽤于您系统的⼆进制⽂件之⼀
2. 授予其执⾏权限
sudo chmod +x /usr/local/bin/gitlab-runner
3. 创建⼀个GitLab CI⽤户
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
4.安装并且作为服务运⾏
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
在GNU / Linux下注册Runner:
1.运⾏以下命令
sudo gitlab-runner register
2.输⼊您的GitLab实例URL
3.输⼊您获得的令牌(gitlab token和项⽬token)
Please enter the gitlab-ci token for this runner
xxxtoken
4.输⼊Runner的描述,稍后可以在GitLab的UI中进⾏更改
Please enter the gitlab-ci description for this runner
[hostname] my-runner
5.输⼊与Runner关联的标签,稍后可以在GitLab的UI中进⾏更改
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag
6.输⼊Runner执⾏者
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
7.如果您选择Docker作为执⾏程序,则会要求您为未在以下项⽬中定义⼀个的项⽬使⽤默认映像.l
Please enter the Docker image (eg. ruby:2.6):
alpine:latest
8.安装完成
POM⽂件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId&le</groupId>
<artifactId>platform-user</artifactId>
<version>0.1</version>
<name>platform-user</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<!-- 需要存储的仓库域名+⽬录+项⽬名 -->
<docker.image.prefix>gitlab/docker/${project.artifactId}</docker.image.prefix>
<dockerfile-maven-plugin.version>1.4.10</dockerfile-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>./</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-plugin.version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-d=file:/dev/./urandom","-jar","/app.jar"]
.l:
before_script:
- export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)
stages:
- check
- build
- test
-
deploy
- push
java-check:
stage: check
script:
- java -version
before_chomd:
variables: # 全局定义新增变量值
GIT_STRATEGY: none #clone,fetch,和none 设置为none 防⽌git删除打包
stage: check
script:
-
ls -la .
- chmod +x ./mvnw
.sonar-check:
variables:
GIT_STRATEGY: none
GIT_CHECKOUT: "false"
stage: check
script:
- ./mvnw clean package -DskipTests sonar:sonar -Dsonar.host.url=127.0.0.1:9100 -Dsonar.projectName=iCatering-serviceshow
.unit-test:
variables:
GIT_STRATEGY: none
GIT_CHECKOUT: "false"
stage: test
script:
- ./mvnw test -st.skip=true
pile:
variables:
GIT_STRATEGY: none
GIT_CHECKOUT: "false"
stage: build
script:
-持续集成的概念
./mvnw clean package -st.skip=true
.deploy_release:
variables: # 全局定义新增变量值
GIT_STRATEGY: none #clone,fetch,和none 设置为none 防⽌git删除打包
GIT_CHECKOUT: "false" #默认true
stage: deploy
script:
- ./mvnw clean package dockerfile:build -Ddockerfile.tag=release
environment: # 定义此作业完成部署的环境名称
name: release
only:
-
release
deploy_master:
variables: # 全局定义新增变量值
GIT_STRATEGY: none #clone,fetch,和none 设置为none 防⽌git删除打包
GIT_CHECKOUT: "false" #默认true
GIT_CHECKOUT: "false" #默认true
stage: deploy
script:
- ./mvnw clean package dockerfile:build -Ddockerfile.tag=master$COMMIT_TIME
environment: # 定义此作业完成部署的环境名称
name: master
only:
- master
.release_push:
variables: # 全局定义新增变量值
GIT_STRATEGY: none #clone,fetch,和none 设置为none 防⽌git删除打包
GIT_CHECKOUT: "false" #默认true
stage: push
script:
- docker push $(./mvnw exec:exec -q -utable=echo -Dexec.args='${docker.image.prefix}'):release
- docker rmi $(./mvnw exec:exec -q -utable=echo -Dexec.args='${docker.image.prefix}'):release
only:
- release
master_push:
variables: # 全局定义新增变量值
GIT_STRATEGY: none #clone,fetch,和none 设置为none 防⽌git删除打包
GIT_CHECKOUT: "false" #默认true
stage: push
script:
- docker push $(./mvnw exec:exec -q -utable=echo -Dexec.args='${docker.image.prefix}'):master$COMMIT_TIME - docker rmi $(./mvnw exec:exec -q -utable=echo -Dexec.args='${docker.image.prefix}'):master$COMMIT_TIME only:
- master
推送远程仓库
注意事项:
1.runner job ⼀直等待执⾏
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论