• Jenkins~配合Docker及dotnetCore进行生产和测试环境的灵活部署


    回到目录

    首先要清楚本文是讲dotnetcore项目在生产和测试环境部署的,这在过去的frameworks项目里,我们可以通过设置web.config的环境变量,然后再发布时指定具体的变量,去实现生产环境和测试环境的发布,发布之后,每个环境有自己的配置文件,frameworks会更新环境把web.config进行合并,而在dotnetcore项目里,这种方法不适用了,所以需要在这里再总结一下了。

    环境说明

    1. jenkins自动部署
    2. docker,docker-swarm集群
    3. dotnet core api项目

    要实现功能

    1. 将调试,测试,生产等环境的配置信息提前配置好,不需要上线后在去修改它,实现自动化发布和部署
    2. dotnet core项目配置自己的appsettings.development.json和appsettings.production.json文件
    3. 运行容器前需要设置环境变量,dockerfile打到镜像也行,docker service启动时加载也可以

    实现部署

    jenkins调用sh脚本添加环境参数
    #!/bin/sh
    set -xe
    cd ${WORKSPACE}/deploy/
    /bin/bash publish.sh
    /bin/bash build.sh "Production"
    build.sh脚本添加了描述环境的输入参数
    #!/bin/sh
    set -ex
    export IMAGE_NAME=svt/sms
    export Registry_Url="ciregistry.i-counting.cn:8443"
    #输入参数source,目前支持Development外测和Production生产环境两个值
    docker build --no-cache --pull -t $IMAGE_NAME --build-arg source=$1 ./
    docker tag $IMAGE_NAME $Registry_Url/$IMAGE_NAME
    docker push $Registry_Url/$IMAGE_NAME
    Dockerfile里添加了设置环境变量的代码
    FROM microsoft/aspnetcore:2.0
    ARG source
    run echo $source
    COPY sources.list /etc/apt/sources.list
    RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
    RUN apt-get update && apt-get -y install libgdiplus && apt-get clean
    ENV ASPNETCORE_ENVIRONMENT=$source
    WORKDIR /app
    EXPOSE 80
    COPY obj/Docker/publish .
    ENTRYPOINT ["dotnet", "Validate.dll"]
    aspnetcore的项目里添加了Development和Production两种配置的appsettings.json
    最后就是代码获取配置时,一定要加上环境参数
    config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
      .AddJsonFile(file, optional: true, reloadOnChange: true)
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
      .Build();

    好了,今天咱们主要实现的是比较实用的按环境去部署项目的方法!

    希望本文章对各位有所帮助!

     回到目录

  • 相关阅读:
    yii2 gii 命令行自动生成控制器和模型
    控制器中的方法命名规范
    Vue Property or method "" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based
    IDEA插件:GsonFormat
    Spring Boot : Access denied for user ''@'localhost' (using password: NO)
    Typora添加主题
    Git基础命令图解
    Java Joda-Time 处理时间工具类(JDK1.7以上)
    Java日期工具类(基于JDK1.7版本)
    Oracle SQL Developer 连接Oracle出现【 状态: 失败 -测试失败: ORA-01017: invalid username/password; logon denied】
  • 原文地址:https://www.cnblogs.com/lori/p/7610990.html
Copyright © 2020-2023  润新知