您好,登錄后才能下訂單哦!
gitlab,gitlab CI/CD相關組件以及介紹
GitLab CI / CD是GitLab內置的強大工具,允許您將所有連續方法(持續集成,交付和部署)應用于您的軟件,而無需第三方應用程序或集成。
.gitlab-ci.yml
)docker基礎知識
第一步:開發人員將代碼上傳到代碼倉庫,gitlab 根據gitlab-ci.yml中的命令,觸發ci/cd邏輯,通知gitlab-runner進行構建部署
第二步:gitlab-runner根據.gitlab-ci.yml腳本進行代碼檢測,單元測試
第三步: gitlab-runner根據.gitlab-ci.yml腳本進行構建打包
第四步:gitlab-runner根據.gitlab--ci.yml腳本將代碼部署到相應的環境中
docker搭建gitlab
docker run -d --publish 22:22 -p 80:80 -p 443:443 --name gitlab --restart unless-stopped -v /data/gitlab/config:/etc/gitlab -v /data/gitlab/logs:/var/log/gitlab -v /data/gitlab/data:/var/opt/gitlab -v /var/run/docker.sock:/var/run/docker.sock twang2218/gitlab-ce-zh:10.5.4
docker啟動gitlab-runner
sudo docker run -d --name gitlab-runner --restart=always -v /data/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:v10.6.0
友情鏈接:gitlab-runner官網安裝指南
gitlab-runner注冊到gitlab中
gitlab-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com ) :輸入gitlab地址\
Please enter the gitlab-ci token for this runner:輸入gitlab token
Please enter the gitlab-ci description for this runner:輸入runner描述信息\
Please enter the gitlab-ci tags for this runner (comma separated):輸入runner tag信息\
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:輸入runner的類型\
Please enter the Docker image (eg. ruby:2.1):(如果選擇docker需要輸入鏡像)
.gitlab-ci.ym腳本編寫
stages:
- test
- build
- deploy
cache:
paths:
- .m2/
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2"
MAVEN_IMAGE: registry.cn-beijing.aliyuncs.com/qian/prod:maven3-jdk8
CONTAINER_NAME: daily_report_java
LAN_REPO: registry-vpc.cn-beijing.aliyuncs.com/qian/prod
WAN_REPO: registry.cn-beijing.aliyuncs.com/qian/prod
DEV_REPO: $WAN_REPO/dev
PRED_REPO: $WAN_REPO/pred
PROD_REPO: $LAN_REPO/prod
test and build image:
image: $MAVEN_IMAGE
stage: test
script:
- mvn package docker:build -q -Dmaven.test.skip=false -Dspring.profiles.active=test sonar:sonar -Dsonar.host.url=http://sonar.allhome.com.cn -Dsonar.login=admin -Dsonar.password=admin -Dsonar.projectName=$CONTAINER_NAME
tags:
- maven
only:
- release
build image:
image: $MAVEN_IMAGE
stage: test
script:
- mvn package docker:build -q -Dmaven.test.skip=true
tags:
- maven
only:
- dev
- master
push image dev:
stage: build
before_script:
- docker login --username=阿里云鏡像倉庫地址 --password="password" registry-vpc.cn-beijing.aliyuncs.com
script:
- echo $version
- docker tag $DEV_REPO:$CONTAINER_NAME $LAN_REPO/qianjia_dev:$CONTAINER_NAME
- docker push $LAN_REPO/qianjia_dev:$CONTAINER_NAME
tags:
- docker-run
only:
- dev
when: on_success
push image master:
stage: build
before_script:
- docker login --username=阿里云鏡像倉庫地址 --password="password" registry-vpc.cn-beijing.aliyuncs.com
script:
- docker tag $DEV_REPO:$CONTAINER_NAME $LAN_REPO/qianjia_pred:$CONTAINER_NAME
- docker push $LAN_REPO/qianjia_pred:$CONTAINER_NAME
tags:
- docker-run
only:
- release
when: on_success
push image prod:
stage: build
before_script:
- docker login --username=阿里云鏡像倉庫地址 --password="password" registry-vpc.cn-beijing.aliyuncs.com
script:
- docker tag $DEV_REPO:$CONTAINER_NAME $LAN_REPO/qianjia_prod:$CONTAINER_NAME
- docker push $LAN_REPO/qianjia_prod:$CONTAINER_NAME
tags:
- docker-run
only:
- master
when: on_success
ssh run on remote machine dev:
stage: deploy
script:
- bash ~/.local/bin/200.sh $CONTAINER_NAME 8801 8801 $DEV_REPO:$CONTAINER_NAME guojia 192.168.10.200
tags:
- shell-200
only:
- dev
when: on_success
ssh run on remote machine master:
stage: deploy
script:
- bash ~/.local/bin/deploy.sh $CONTAINER_NAME 8801 8801 $PRED_REPO:$CONTAINER_NAME zp 預生產ip地址
tags:
- shell-prod
only:
- release
when: on_success
ssh run on remote machine prod:
stage: deploy
script:
- bash ~/.local/bin/deploy.sh $CONTAINER_NAME 8801 8801 $PROD_REPO:$CONTAINER_NAME zp 生產ip地址
tags:
- shell-prod
only:
- master
when: on_success
修改pom.xml,添加如下代碼
<!--熱部署-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 如果沒有該項配置,devtools不會起作用,即應用不會restart -->
<fork>true</fork>
</configuration>
</plugin>
<!--
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.3</version>
<configuration>
<imageName>registry.cn-beijing.aliyuncs.com/qian2018/qianjia_dev:${project.artifactId}
</imageName>
<dockerDirectory>.</dockerDirectory>
<resources>
<resource>
<targetPath>target</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>server</id>
<name>releases</name>
<url>http://nexus地址:9000/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>server</id>
<name>snapshots</name>
<url>http://nexusi地址:9000/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://nexus地址:9000/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://39.106.145.141:9000/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
項目根目錄下添加 Dockerfile文件
FROM registry-vpc.cn-beijing.aliyuncs.com/qian/qianjia_public:oraclejdk
VOLUME /tmp
add target/*.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Pipelines 中文稱為流水線,是分階段執行的構建任務,一次自動發布流程就稱為一次流水線,以上邊腳本舉例,安裝依賴、運行測試、打包、部署開發服務器等整套流程叫做一次流水線
Stages:Stages 表示構建階段,一次流水線包括多個stages,在腳本實例中stages定義了一次流水線的三個階段:第一個階段:運行測試,進行代碼檢測,第二個階段:打包java程序并生成鏡像,第三個階段:執行shell腳本,把容器部署到服務器
Jobs :表示構建的作業(或稱之為任務),表示某個 Stage 里面執行的具體任務。我們可以在 Stages 里面定義多個 Jobs
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。