gitlab,gitlab runner自动化部署docke

发布时间:2020-04-06 23:37:38 作者:wangnannan36
来源:网络 阅读:1376

一 、知识点

gitlab,gitlab CI/CD相关组件以及介绍

docker基础知识

二、自动化部署流程


第一步:开发人员将代码上传到代码仓库,gitlab 根据gitlab-ci.yml中的命令,触发ci/cd逻辑,通知gitlab-runner进行构建部署
第二步:gitlab-runner根据.gitlab-ci.yml脚本进行代码检测,单元测试
第三步: gitlab-runner根据.gitlab-ci.yml脚本进行构建打包
第四步:gitlab-runner根据.gitlab--ci.yml脚本将代码部署到相应的环境中

三、利用gitlab CI/CD 自动部署spring boot实例讲解


运维架构流程图

gitlab,gitlab runner自动化部署docke

gitlab/gitlab runner安装

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中

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,gitlab runner自动化部署docke

.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


推荐阅读:
  1. gitlab+gitlab-runner+k8s
  2. 安装kubernetes gitlab runner

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

自动化 dock ne

上一篇:Apue Chapter 4 习题答案

下一篇:YARN环境中应用程序JAR包冲突问题的分析及解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》