zoukankan      html  css  js  c++  java
  • Jenkins简明入门(三) -- Blue Ocean,让一切变得简单

    我们在上一节Jenkins简明入门(二) 中见识到了Jenkins能做些什么:利用Jenkins完成python程序的build、test、deployment。

    同时,也有一种简单的方法,不需要写Jenkinsfile文件,只需要通过可视化配置,就可以完成Jenkins的部署。

    BlueOcean的背景就是为了适应Jenkins Pipeline和 Freestyle jobs任务,Jenkins推出了BlueOcean UI,让程序员执行任务时,降低工作流程的复杂度和提升工作流程的清晰度,它具有如下特征:

    1. 清晰的可视化,对CI/CD pipelines, 可以快速直观的观察项目pipeline状态。

    2. pipeline可编辑(开发中),可视化编辑pipeline,现在只能通过配置中Pipeline的Pipeline script编辑。

    3. pipeline精确度,通过UI直接介入pipeline的中间问题。

    4. 集成代码分支和pull请求。

    本节内容改编自https://jenkins.io/doc/tutorials/create-a-pipeline-in-blue-ocean/

    在Blue Ocean中创建你的Pipline Project

    1. 登录http://localhost:8080/,点击左边的Blue Ocean按钮

       
    2. 点击右上角的"New Pipline"创建新的Pipline

    3. 在In Where do you store your code中, 选择 GitHub.

    4. 如果遇到Github设置问题,请参考Jenkins简明入门(二)解决。
    5. 选择好organization和对应的repo(也就是在Jenkins简明入门(二)中你设置好的simple-python-pyinstaller-app)

    6. 点击 Create Pipeline.

    创建你的初始Pipeline

    1. 在Pipline的编辑界面,在Agent下拉列表中选择docker。

      Initial to GitHub

    2. 在Image参数界面,选择python:2-alpine

      Configuring the agent

    3. 回到主Pipeline编辑器,点击“+”符号,这个将会创建一个新的stage

      Add <em>Build</em> stage

    4. 在这个界面,将stage命名为Build,然后点击"Add step"

      Adding the Build stage

    5. 在这个界面,点击Shell Script,将会打开Build/Shell Script界面,你可以输入具体的Step

      Choosing a step type

    6. 在 the Build / Shell Script 界面, 输入 “python -m py_compile sources/add2vals.py sources/calc.py”.

      Specifying a shell step value

    7. Optional ) 点击图标 Return from step icon 来返回到主Pipeline编辑器。

    8. 点击右上角的Save按钮,把你的新的Pipeline的"Build" stage保存下来。

    9. 在保存Pipeline的对话框,可以输入commit信息来描述。(e.g. Add initial Pipeline (Jenkinsfile)).

      Save Pipeline dialog box

    10. 点击Save & run ,Jenkins 将会 build 你的 Pipeline.

    11. 当Blue Ocean的主界面出现时,点击你创建的Pipline的那一行去观察Jenkins Build你的Pipeline的过程。
      Note: 你可能需要等几分钟整个运行过程才会完成,在这个时间里,Jenkins做了以下几件事:

      1. 将你的Pipeline保存为Jenkinsfiile文件,保存在你的repo的分支上。

      2. 进行队列初始化,在Agent上build的你的Project.

      3. 下载Docker Image,将其run成container.

      4. 在Python的container里执行Build Stage(在Jenkinsfile里定义好的) 

        Downloading <em>npm</em> dependencies

      如果你的Application成功build,那么Blue Ocean的界面将会变绿。

      Initial Pipeline runs successfully

    12. 点击右上角的X,返回Blue Ocean主界面。

      Main Blue Ocean interface
      Note: 在继续之前,你可以check一下Jenkins已经为你创建了一个Jenkinsfile,路径是你的Github的repo的root路径。

    给你的Pipeline加一个Test Stage

    1. 在Blue Ocean的主界面,点击右上角的"Branches"来访问你的repo的branches page,在这里你可以访问到master branch。

      Repository branches page

    2. 点击master branch的"Edit Pipeline" icon Edit Pipeline on branch ,打开Pipeline编辑器。

    3. 在Pipeline主编辑器,点击Build Stage右边的 “ + ” 图标,这样就在右边打开了新的stage面板。

      Add <em>Test</em> stage

    4. 在本面板里,将stage命名为Test,然后点击Add Step按钮进入下一步。

    5. 在列表里选择Shell Script.

    6. 在Shell Script里输入“py.test --verbose --junit-xml test-reports/results.xml sources/test_calc.py”

    7. 点击Add Step选择"Archieve JUnit-formatted test results", 然后输入"test-reports/results.xml"

    8. 点击Setting右边图标,进入setting页面。在这里可以填写/编辑 Test Stage跑在什么环境里,环境变量是怎样的。

    9. Agent选择"docker",Image选择“qnib/pytest“”,环境变量不用添加。Environment directive

    10. 点击  Return from step icon 返回 Pipeline 编辑器.

    11. 点击Save按钮,在Save Pipeline对话框,填写commit message (e.g. Add 'Test' stage).

    12. 其他不用改,点击 Save & run and Jenkins,执行新编辑后的Pipeline.

    13. 当Blue Ocean主页面出现后,点击最上面的行观察Jenkins的运行情况。
      Note: 你将会看到Jenkins不再需要下载Python的Docker image,而是会直接用之前下好的Docker image运行为Cotainer,所以你的这次Pipeline运行的会更快。
      如果你新添加的Pipeline成功的运行,Blue Ocean界面将会如下所示。点击"Build"或者"Test"会进入相应的stage circle,访问到相应的结果。

      Test stage runs successfully (with output)

    14. Click the X at the top-right to return to the main Blue Ocean interface.

    Add a final deliver stage to your Pipeline

    1. From the main Blue Ocean interface, click Branches at the top-right to access your respository’s master branch.

    2. Click the master branch’s "Edit Pipeline" icon Edit Pipeline on branch to open the Pipeline editor for this branch.

    3. In the main Pipeline editor, click the + icon to the right of the Test stage you created above to open the new stage panel.

      Add <em>Deliver</em> stage

    4. In this panel, type Deliver in the Name your stage field and then click the Add Step button below to open the Choose step type panel.

    5. In this panel, click Shell Script near the top of the list.

    6. In the resulting Deliver / Shell Script panel, specify ./jenkins/scripts/deliver.sh and then click the top-left back arrow icon Return from step icon to return to the Pipeline stage editor.

      Add next step
      Note: For an explanation of this step, refer to the deliver.sh file itself located in the jenkins/scripts of your forked repository on GitHub.

    7. Click the Add Step button again.

    8. In the Choose step type panel, type input into the Find steps by name field.

      Choosing the input step type

    9. Click the filtered Wait for interactive input step type.

    10. In the resulting Deliver / Wait for interactive input panel, specify Finished using the web site? (Click "Proceed" to continue) in the Message field and then click the top-left back arrow icon Return from step icon to return to the Pipeline stage editor.

      Specifying input step message value
      Note: For an explanation of this step, refer to annotation 4 of the Declarative Pipeline in the “Add a final deliver stage…​” section of the Build a Node.js and React app tutorial.

    11. Click the Add Step button (last time).

    12. Click Shell Script near the top of the list.

    13. In the resulting Deliver / Shell Script panel, specify ./jenkins/scripts/kill.sh.
      Note: For an explanation of this step, refer to the kill.sh file itself located in the jenkins/scripts of your forked repository on GitHub.

    14. Optional ) Click the top-left back arrow icon Return from step icon to return to the main Pipeline editor.

    15. Click the Save button at the top right to begin saving your Pipeline with with its new "Deliver" stage.

    16. In the Save Pipeline dialog box, specify the commit message in the Description field (e.g. Add 'Deliver' stage).

    17. Leaving all other options as is, click Save & run and Jenkins proceeds to build your amended Pipeline.

    18. When the main Blue Ocean interface appears, click the top row to see Jenkins build your Pipeline project.
      If your amended Pipeline ran successfully, here’s what the Blue Ocean interface should look like. Notice the additional "Deliver" stage. Click on the previous "Test" and "Build" stage circles to access the outputs from those stages.

      Deliver stage pauses for user input

    19. Ensure you are viewing the "Deliver" stage (click it if necessary), then click the green ./jenkins/scripts/deliver.sh step to expand its content and scroll down until you see the http://localhost:3000 link.

      Deliver stage output only

    20. Click the http://localhost:3000 link to view your Node.js and React application running (in development mode) in a new web browser tab. You should see a page/site with the title Welcome to React on it.

    21. When you are finished viewing the page/site, click the Proceed button to complete the Pipeline’s execution.

      Deliver stage runs successfully

    22. Click the X at the top-right to return to the main Blue Ocean interface, which lists your previous Pipeline runs in reverse chronological order.

      Main Blue Ocean interface with all previous runs displayed

    Follow up (optional)

    If you check the contents of the Jenkinsfile that Blue Ocean created at the root of your forked creating-a-pipeline-in-blue-ocean repository, notice the location of the environment directive. This directive’s location within the "Test" stage means that the environment variable CI (with its value of true) is only available within the scope of this "Test" stage.

    You can set this directive in Blue Ocean so that its environment variable is available globally throughout Pipeline (as is the case in the Build a Node.js and React app with npm tutorial). To do this:

    1. From the main Blue Ocean interface, click Branches at the top-right to access your respository’s master branch.

    2. Click the master branch’s "Edit Pipeline" icon Edit Pipeline on branch to open the Pipeline editor for this branch.

    3. In the main Pipeline editor, click the Test stage you created above to begin editing it.

    4. In the stage panel on the right, click Settings to reveal this section of the panel.

    5. Click the minus (-) icon at the right of the CI environment directive (you created earlier) to delete it.

    6. Click the top-left back arrow icon Return from step icon to return to the main Pipeline editor.

    7. In the Pipeline Settings panel, click the + icon at the right of the Environment heading (for which you’ll configure a globalenvironment directive).

    8. In the Name and Value fields that appear, specify CI and true, respectively.

    9. Click the Save button at the top right to begin saving your Pipeline with with its relocated environment directive.

    10. In the Save Pipeline dialog box, specify the commit message in the Description field (e.g. Make environment directive global).

    11. Leaving all other options as is, click Save & run and Jenkins proceeds to build your amended Pipeline.

    12. When the main Blue Ocean interface appears, click the top row to see Jenkins build your Pipeline project.
      You should see the same build process you saw when you completed adding the final deliver stage (above). However, when you inspect the Jenkinsfile again, you’ll notice that the environment directive is now a sibling of the agent section.

    Wrapping up

  • 相关阅读:
    如何使用android模拟器截图
    android SD卡文件的读写
    res/raw下的资源文件读写
    window 运行指令(1)
    javax.swing.JOptionPane.showMessageDialog() 方法
    Eclipse快捷键
    EditPlus怎样自动换行
    java的HashCode方法
    eclipse汉化全程
    (转载)jdbc事务处理
  • 原文地址:https://www.cnblogs.com/ArsenalfanInECNU/p/8867318.html
Copyright © 2011-2022 走看看