这是小小本周的第四篇

这是小小本周的第四篇,本周感冒流鼻涕,啥的,继续更新文章啦。

建立配置文件

这里建立相关的配置文件

设置默认的开发环境为dev环境

设置默认的开发环境

spring:
  profiles:
    active: dev

对POM文件进行修改进行打包

这里对POM文件进行修改

<profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>

修改DockerFile文件

在顶级目录修改DockerFile文件,如下

FROM openjdk:11.0.3-jdk-stretch
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
VOLUME /tmp
ADD subject-lib-0.0.1-SNAPSHOT.jar app.jar
# spring.profiles.active指定启动环境
#-c为清除以前启动的数据
ENTRYPOINT ["java","-jar","/app.jar","--spring.profiles.active=${profiles}","-c"]

修改GitLab文件

这里修改GitLab文件

after_script:
  - docker rmi $( docker images -q -f dangling=true)

stages:
  - build
  - test
  - deploy

job_build:
  stage: build
  script:
    - mvn clean install

job_test:
  stage: test
  script:
    - mvn test


job_deploy_dev:
  stage: deploy
  script:
    - mvn package docker:build -P dev
    - if [ $(docker ps -aq --filter name=spring-boot-docker-multi-environment) ]; then docker rm -f spring-boot-docker-multi-environment-dev;fi
    - docker run -e profiles="dev" -it --name spring-boot-docker-multi-environment-dev -d -p 8081:8081 unchin/spring-boot-docker-multi-environment
  only:
    - dev

job_deploy_uat:
  stage: deploy
  script:
    - mvn package docker:build -P uat
    - if [ $(docker ps -aq --filter name=spring-boot-docker-multi-environment-uat) ]; then docker rm -f spring-boot-docker-multi-environment-uat;fi
    - docker run -e profiles="uat" -it --name spring-boot-docker-multi-environment-uat -d -p 8082:8082 unchin/spring-boot-docker-multi-environment
  only:
    - uat

job_deploy_pro:
  stage: deploy
  script:
    - mvn package docker:build -P pro
    - if [ $(docker ps -aq --filter name=spring-boot-docker-multi-environment-pro) ]; then docker rm -f spring-boot-docker-multi-environment-pro;fi
    - docker run -e profiles="pro" -it --name spring-boot-docker-multi-environment-pro -d -p 8090:8090 unchin/spring-boot-docker-multi-environment
  only:
    - tags

查看构建过程