教程:配置 Jenkins 环境以便为 Service Fabric 上的 Java 应用程序启用 CI/CDTutorial: Configure a Jenkins environment to enable CI/CD for a Java application on Service Fabric

本教程是系列教程的第五部分,This tutorial is part five of a series. 介绍如何使用 Jenkins 将升级部署到应用程序。It shows you how to use Jenkins to deploy upgrades to your application. 本教程结合使用 Service Fabric Jenkins 插件和托管投票应用程序的 GitHub 存储库,将应用程序部署到群集。In this tutorial, the Service Fabric Jenkins plugin is used in combination with a GitHub repository hosting the Voting application to deploy the application to a cluster.

本系列教程的第五部分介绍以下操作:In part five of the series, you learn how to:

  • 在计算机上部署 Service Fabric Jenkins 容器Deploy Service Fabric Jenkins container on your machine
  • 设置要部署到 Service Fabric 的 Jenkins 环境Set up Jenkins environment for deployment to Service Fabric
  • 升级应用程序Upgrade your application

在此系列教程中,你将学习如何:In this tutorial series you learn how to:

先决条件Prerequisites

提取并部署 Service Fabric Jenkins 容器映像Pull and deploy Service Fabric Jenkins container image

可在 Service Fabric 群集内部或外部设置 Jenkins。You can set up Jenkins either inside or outside a Service Fabric cluster. 以下说明介绍如何使用提供的 Docker 映像在群集外部设置 Jenkins。The following instructions show how to set it up outside a cluster using a provided Docker image. 但也可以使用预配置的 Jenkins 生成环境。However, a preconfigured Jenkins build environment can also be used. 以下容器映像已连同 Service Fabric 插件一起安装,随时可与 Service Fabric 配合使用。The following container image comes installed with the Service Fabric plugin and is ready for use with Service Fabric immediately.

  1. 拉取 Service Fabric Jenkins 容器映像:docker pull rapatchi/jenkins:v10Pull the Service Fabric Jenkins container image: docker pull rapatchi/jenkins:v10. 此映像附带了预安装的 Service Fabric Jenkins 插件。This image comes with Service Fabric Jenkins plugin pre-installed.

  2. 结合 Azure 证书存储在已装载本地计算机上的位置运行容器映像。Run the container image with the location where your Azure certificates are stored on your mounted local machine.

    docker run -itd -p 8080:8080 -v /service-fabric-java-quickstart/AzureCluster rapatchi/jenkins:v10
    
  3. 获取容器映像实例的 ID。Get the ID of the container image instance. 可以使用命令 docker ps -a 列出所有 Docker 容器You can list all the Docker containers with the command docker ps -a

  4. 运行以下命令,检索 Jenkins 实例的密码:Retrieve the password of your Jenkins instance by running the following command:

    docker exec [first-four-digits-of-container-ID] cat /var/jenkins_home/secrets/initialAdminPassword
    

    如果容器 ID 为 2d24a73b5964,请使用 2d24。If container ID is 2d24a73b5964, use 2d24.

    • 从门户 http://<HOST-IP>:8080 登录到 Jenkins 仪表板时需要此密码This password is required for signing in to the Jenkins dashboard from portal, which is http://<HOST-IP>:8080
    • 首次登录后,可以创建自己的用户帐户或使用管理员帐户。After you sign in for the first time, you can create your own user account or use the admin account.
  5. 使用生成新的 SSH 密钥并将其添加到 SSH 代理中所述的步骤,将 GitHub 设置为使用 Jenkins。Set up GitHub to work with Jenkins by using the steps mentioned in Generating a new SSH key and adding it to the SSH agent. 由于命令是从 Docker 容器运行的,因此请遵照适用于 Linux 环境的说明。Since the commands are run from the Docker container, follow the instructions for the Linux environment.

    • 使用 GitHub 提供的说明生成 SSH 密钥。Use the instructions provided by GitHub to generate the SSH key. 接下来,将 SSH 密钥添加到托管存储库的 GitHub 帐户。Next, add the SSH key to the GitHub account that is hosting the repository.

    • 在 Jenkins Docker shell(而不是主机)中运行上述链接中提到的命令。Run the commands mentioned in the preceding link in the Jenkins Docker shell (and not on your host).

    • 若要从主机登录到 Jenkins shell,请使用以下命令:To sign in to the Jenkins shell from your host, use the following commands:

      docker exec -t -i [first-four-digits-of-container-ID] /bin/bash
      

      确保托管 Jenkins 容器映像的群集或计算机使用公共 IP。Ensure that the cluster or machine where the Jenkins container image is hosted has a public-facing IP. 使用公共 IP 可让 Jenkins 实例从 GitHub 接收通知。Having a public-facing IP enables the Jenkins instance to receive notifications from GitHub.

创建和配置 Jenkins 作业Create and configure a Jenkins job

  1. 首先,如果没有可用于在 GitHub 上托管投票项目的存储库,请创建一个存储库。First, if you do not have a repository that you can use to host the Voting project on GitHub, create one. 在本教程的余下内容中,此存储库名为 dev_testThe repository is called dev_test for the remaining of this tutorial.

  2. http://<HOST-IP>:8080 的 Jenkins 仪表板上创建一个新项Create a new item on your Jenkins dashboard at http://<HOST-IP>:8080.

  3. 输入项名称(例如 MyJob)。Enter an item name (for example, MyJob). 选择“自由格式的项目”,并单击“确定”。Select free-style project, and click OK.

  4. 转到作业页,单击“配置”。Go the job page, and click Configure.

    a.a. 在常规部分中,选择“GitHub 项目”所对应的复选框,指定 GitHub 项目 URL。In the general section, select the checkbox for GitHub project, and specify your GitHub project URL. 此 URL 托管要与 Jenkins 持续集成和持续部署 (CI/CD) 流(例如 https://github.com/testaccount/dev_test)集成的 Service Fabric Java 应用程序。This URL hosts the Service Fabric Java application that you want to integrate with the Jenkins continuous integration, continuous deployment (CI/CD) flow (for example, https://github.com/testaccount/dev_test).

    b.b. 在“源代码管理”部分,选择 GitUnder the Source Code Management section, select Git. 指定用于托管要与 Jenkins CI/CD 流(例如 https://github.com/testaccount/dev_test.git )集成的 Service Fabric Java 应用程序的存储库 URL。Specify the repository URL that hosts the Service Fabric Java application that you want to integrate with the Jenkins CI/CD flow (for example, https://github.com/testaccount/dev_test.git). 也可在此处指定要生成的分支(例如 /master)。Also, you can specify here which branch to build (for example, /master).

  5. 配置 GitHub(存储库的托管位置),使它能够与 Jenkins 通信。Configure your GitHub (which is hosting the repository) so that it is able to talk to Jenkins. 请使用以下步骤:Use the following steps:

    a.a. 转到 GitHub 存储库页。Go to your GitHub repository page. 转到“设置” > “集成和服务”。Go to Settings > Integrations and Services.

    b.b. 选择“添加服务”,键入 Jenkins,并选择“Jenkins-GitHub 插件”。Select Add Service, type Jenkins, and select the Jenkins-GitHub plugin.

    c.c. 输入 Jenkins Webhook URL(默认为 http://<PublicIPorFQDN>:8081/github-webhook/)。Enter your Jenkins webhook URL (by default, it should be http://<PublicIPorFQDN>:8081/github-webhook/). 单击“添加/更新服务”。Click add/update service.

    d.d. 将向 Jenkins 实例发送一个测试事件。A test event is sent to your Jenkins instance. GitHub 中的 Webhook 旁边应会显示一个绿色复选标记,同时会生成项目。You should see a green check by the webhook in GitHub, and your project builds.

    Service Fabric Jenkins 配置

  6. 在“生成触发器”部分下面,选择所需的生成选项。Under the Build Triggers section, select which build option you want. 在此示例中,我们希望每当向存储库推送信息,就会触发生成。For this example, you want to trigger a build whenever some push to the repository happens. 为此,可以选择“用于 GITScm 轮询的 GitHub 挂钩触发器”。So you select GitHub hook trigger for GITScm polling.

  7. 在“生成”部分下面,从“添加生成步骤”下拉列表中选择“调用 Gradle 脚本”。Under the Build section, from the drop-down Add build step, select the option Invoke Gradle Script. 在出现的小组件中,打开高级菜单,为应用程序指定“根生成脚本”的路径。In the widget that comes open the advanced menu, specify the path to Root build script for your application. 该脚本将从指定的路径中选择 build.gradle,然后执行相应的操作。It picks up build.gradle from the path specified and works accordingly.

    Service Fabric Jenkins 生成操作

  8. 在“生成后操作”下拉列表中,选择“部署 Service Fabric 项目”。From the Post-Build Actions drop-down, select Deploy Service Fabric Project. 此处需要提供有关在何处部署 Jenkins 编译的 Service Fabric 应用程序的群集详细信息。Here you need to provide cluster details where the Jenkins compiled Service Fabric application would be deployed. 证书的路径是卷的装载位置 (/tmp/myCerts)。The path to the certificate is where the volume was mounted (/tmp/myCerts).

    还可以提供用于部署应用程序的其他详细信息。You can also provide additional details used to deploy the application. 有关应用程序详细信息的示例,请参阅以下屏幕截图:See the following screenshot for an example for the application details:

    Service Fabric Jenkins 生成操作

    备注

    如果使用 Service Fabric 部署 Jenkins 容器映像,此处的群集可与托管 Jenkins 容器应用程序的群集相同。The cluster here could be same as the one hosting the Jenkins container application, in case you are using Service Fabric to deploy the Jenkins container image.

  9. 单击“保存” 。Click Save.

更新现有应用程序Update your existing application

  1. 使用 Service Fabric 投票示例 V2 更新 VotingApplication/VotingWebPkg/Code/wwwroot/index.html 文件中 HTML 的标题。Update the title of the HTML in the VotingApplication/VotingWebPkg/Code/wwwroot/index.html file with Service Fabric Voting Sample V2.

    <div ng-app="VotingApp" ng-controller="VotingAppController" ng-init="refresh()">
        <div class="container-fluid">
            <div class="row">
                <div class="col-xs-8 col-xs-offset-2 text-center">
                    <h2>Service Fabric Voting Sample V2</h2>
                </div>
            </div>
        </div>
    </div>
    
  2. Voting/VotingApplication/ApplicationManifest.xml 文件中,将 ApplicationTypeVersionServiceManifestVersion 版本更新为 2.0.0Update the ApplicationTypeVersion and ServiceManifestVersion version to 2.0.0 in the Voting/VotingApplication/ApplicationManifest.xml file.

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <ApplicationManifest xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="VotingApplicationType" ApplicationTypeVersion="2.0.0">
      <Description>Voting Application</Description>
      <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="VotingWebPkg" ServiceManifestVersion="2.0.0"/>
      </ServiceManifestImport>
      <ServiceManifestImport>
            <ServiceManifestRef ServiceManifestName="VotingDataServicePkg" ServiceManifestVersion="1.0.0"/>
        </ServiceManifestImport>
        <DefaultServices>
          <Service Name="VotingWeb">
             <StatelessService InstanceCount="1" ServiceTypeName="VotingWebType">
                <SingletonPartition/>
             </StatelessService>
          </Service>
       <Service Name="VotingDataService">
                <StatefulService MinReplicaSetSize="3" ServiceTypeName="VotingDataServiceType" TargetReplicaSetSize="3">
                    <UniformInt64Partition HighKey="9223372036854775807" LowKey="-9223372036854775808" PartitionCount="1"/>
                </StatefulService>
            </Service>
        </DefaultServices>
    </ApplicationManifest>
    
  3. Voting/VotingApplication/VotingWebPkg/ServiceManifest.xml 文件中,将 ServiceManifest 中的 Version 字段以及 CodePackage 标记中的 Version 字段更新为 2.0.0Update the Version field in the ServiceManifest and the Version field in the CodePackage tag in the Voting/VotingApplication/VotingWebPkg/ServiceManifest.xml file to 2.0.0.

    <CodePackage Name="Code" Version="2.0.0">
    <EntryPoint>
        <ExeHost>
        <Program>entryPoint.sh</Program>
        </ExeHost>
    </EntryPoint>
    </CodePackage>
    
  4. 若要初始化执行应用程序升级的 Jenkins 作业,请将新更改推送到 GitHub 存储库。To initialize a Jenkins job that performs an application upgrade, push your new changes to your GitHub repository.

  5. 在 Service Fabric Explorer 中,单击“应用程序”下拉列表。In Service Fabric Explorer, click on the Applications dropdown. 若要查看升级状态,请单击“正在进行升级”选项卡。To see the status of your upgrade, click on the Upgrades in Progress tab.

    正在进行升级

  6. 如果访问 http://<Host-IP>:8080,则具有完整功能的投票应用程序现在已启动且正在运行。If you access http://<Host-IP>:8080 the Voting application with full functionality is now up and running.

    本地 Voting 应用

后续步骤Next steps

在本教程中,你了解了如何执行以下操作:In this tutorial, you learned how to:

  • 在计算机上部署 Service Fabric Jenkins 容器Deploy Service Fabric Jenkins container on your machine
  • 设置要部署到 Service Fabric 的 Jenkins 环境Set up Jenkins environment for deployment to Service Fabric
  • 升级应用程序Upgrade your application