Jenkins 中文教程
  • Jenkins
  • Jenkins-快速开始
  • Jenkins 安装
  • 使用 Maven 构建 Java app
  • Jenkins-blue-ocean
  • Jenkins 资源
  • Jenkins 术语
Powered by GitBook
On this page
  • Jenkins Blue Ocean
  • 先决条件
  • 在 Docker 中运行 jenkins
  • 在 github 上 folk 实例仓库
  • 在 Blue Ocean 创建 Pipeline 项目
  • 创建初始化的 Pipeline
  • 在Pipeline中添加一个test阶段
  • 在Pipeline中添加一个最终提交阶段
  • 跟进(可选的)
  • 包装

Was this helpful?

Jenkins-blue-ocean

Previous使用 Maven 构建 Java appNextJenkins 资源

Last updated 5 years ago

Was this helpful?

Jenkins Blue Ocean

You can stop this tutorial at any point in time and continue from where you left off.

先决条件

For this tutorial, you will require:

  • A macOS, Linux or Windows machine with:

    • 256 MB of RAM, although more than 512MB is recommended.

    • 10 GB of drive space for Jenkins and your Docker images and containers.

  • The following software installed:

    • Note: If you use Linux, this tutorial assumes that you are not running Docker commands as the root user, but instead with a single user account that also has access to the other tools used throughout this tutorial.

在 Docker 中运行 jenkins

在 mac 和 linux 中运行 jenkins

  1. Open up a terminal window.

  2. docker run \
      --rm \
      -u root \
      -p 8080:8080 \
      -v jenkins-data:/var/jenkins_home \ 
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v "$HOME":/home \ 
      jenkinsci/blueocean

    Note: If copying and pasting the command snippet above doesn’t work, try copying and pasting this annotation-free version here:

    docker run \
      --rm \
      -u root \
      -p 8080:8080 \
      -v jenkins-data:/var/jenkins_home \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v "$HOME":/home \
      jenkinsci/blueocean

在 windows 中运行 jenkins

  1. Open up a command prompt window.

  2. docker run ^
      --rm ^
      -u root ^
      -p 8080:8080 ^
      -v jenkins-data:/var/jenkins_home ^
      -v /var/run/docker.sock:/var/run/docker.sock ^
      -v "%HOMEPATH%":/home ^
      jenkinsci/blueocean

访问 Jenkins Blue Ocean Docker 容器

This means you could access the Jenkins/Blue Ocean container (through a separate terminal/command prompt window) with a docker exec command like:

docker exec -it jenkins-tutorials bash

设置 wizard

Before you can access Jenkins, there are a few quick "one-off" steps you’ll need to perform.

解锁 jenkins

When you first access a new Jenkins instance, you are asked to unlock it using an automatically-generated password.

  1. After the 2 sets of asterisks appear in the terminal/command prompt window, browse to http://localhost:8080 and wait until the Unlock Jenkins page appears.

  2. From your terminal/command prompt window again, copy the automatically-generated alphanumeric password (between the 2 sets of asterisks).

  3. On the Unlock Jenkins page, paste this password into the Administrator password field and click Continue.

使用插件定制化 jenkins

On this page, click Install suggested plugins.

The setup wizard shows the progression of Jenkins being configured and the suggested plugins being installed. This process may take a few minutes.

创建第一个管理员

Finally, Jenkins asks you to create your first administrator user.

  1. When the Create First Admin User page appears, specify your details in the respective fields and click Save and Finish.

  2. When the Jenkins is ready page appears, click Start using Jenkins. Notes:

    • This page may indicate Jenkins is almost ready! instead and if so, click Restart.

    • If the page doesn’t automatically refresh after a minute, use your web browser to refresh the page manually.

  3. If required, log in to Jenkins with the credentials of the user you just created and you’re ready to start using Jenkins!

启动和终止 jenkins

To restart the Jenkins/Blue Ocean Docker container:

  1. Browse to http://localhost:8080.

  2. Wait until the log in page appears and log in.

在 github 上 folk 实例仓库

Fork the simple "Welcome to React" Node.js and React application on GitHub into your own GitHub account.

在 Blue Ocean 创建 Pipeline 项目

  1. Go back to Jenkins and ensure you have accessed the Blue Ocean interface. To do this, make sure you:

    • have browsed to http://localhost:8080/blue and are logged in or

    • have browsed to http://localhost:8080/, are logged in and have clicked Open Blue Ocean on the left.

  2. In the Welcome to Jenkins box at the center of the Blue Ocean interface, click Create a new Pipeline to begin the Pipeline creation wizard. Note: If you don’t see this box, click New Pipeline at the top right.

  3. In Where do you store your code?, click GitHub.

  4. In the new tab, sign in to your GitHub account (if necessary) and on the GitHub New Personal Access Token page, specify a brief Token description for your GitHub access token (e.g. Blue Ocean). Note: An access token is usually an alphanumeric string that respresents your GitHub account along with permissions to access various GitHub features and areas through your GitHub account. This access token will have the appropriate permissions pre-selected, which Blue Ocean requires to access and interact with your GitHub account.

  5. Scroll down to the end of the page (leaving all other Select scopes options with their default settings) and click Generate token.

  6. On the resulting Personal access tokens page, copy your newly generated access token.

  7. Back in Blue Ocean, paste the access token into the Your GitHub access token field and click Connect.

  8. In Choose a repository, click your forked repository creating-a-pipeline-in-blue-ocean.

  9. Click Create Pipeline. Blue Ocean detects that there is no Jenkinsfile at the root level of the repository’s master branch and proceed to help you create one. (Therefore, you’ll need to click another Create Pipeline at the end of the page to proceed.) Note: Under the hood, a Pipeline project created through Blue Ocean is actually "multibranch Pipeline". Therefore, Jenkins looks for the presence of at least one Jenkinsfile in any branch of your repository.

创建初始化的 Pipeline

  1. In the Image and Args fields that appear, specify node:6-alpine and -p 3000:3000 respectively.

  2. Back in the main Pipeline editor, click the + icon, which opens the new stage panel on the right.

  3. In this panel, type Build in the Name your stage field and then click the Add Step button below, which opens the Choose step type panel.

  4. In this panel, click Shell Script near the top of the list (to choose that step type), which opens the Build / Shell Script panel, where you can enter this step’s values.

  5. In the Build / Shell Script panel, specify npm install.

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

  7. In the Save Pipeline dialog box, specify the commit message in the Description field (e.g. Add initial Pipeline (Jenkinsfile)).

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

  9. When the main Blue Ocean interface appears, click the row to see Jenkins build your Pipeline project. Note: You may need to wait several minutes for this first run to complete. During this time, Jenkins does the following:

    1. Commits your Pipeline as a Jenkinsfile to the only branch (i.e. master) of your repository.

    2. Initially queues the project to be built on the agent.

    3. Downloads the Node Docker image and runs it in a container on Docker.

    4. Executes the Build stage (defined in the Jenkinsfile) on the Node container. (During this time, npm downloads many dependencies necessary to run your Node.js and React application, which will ultimately be stored in the local node_modules directory within the Jenkins home directory).

    The Blue Ocean interface turns green if Jenkins built your application successfully.

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

在Pipeline中添加一个test阶段

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

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

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

  4. At the lower-right of the panel, click Settings to reveal this section of the panel.

  5. Click the + icon at the right of the Environment heading (for which you’ll configure an environment directive).

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

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

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

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

  10. When the main Blue Ocean interface appears, click the top row to see Jenkins build your Pipeline project. Note: You’ll notice from this run that Jenkins no longer needs to download the Node Docker image. Instead, Jenkins only needs to run a new container from the Node image downloaded previously. Therefore, running your Pipeline this subsequent time should be much faster. If your amended Pipeline ran successfully, here’s what the Blue Ocean interface should look like. Notice the additional "Test" stage. You can click on the previous "Build" stage circle to access the output from that stage.

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

在Pipeline中添加一个最终提交阶段

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

  2. 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.

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

  4. Click the Add Step button again.

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

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

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

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

  9. 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.

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

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

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

  13. 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.

  14. 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.

  15. 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.

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

  17. 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.

跟进(可选的)

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

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

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

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

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

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

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

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

包装

Well done! You’ve just used the Blue Ocean feature of Jenkins to build a simple Node.js and React application with npm!

The "Build", "Test" and "Deliver" stages you created above are the basis for building other applications in Jenkins with any technology stack, including more complex applications and ones that combine multiple technology stacks together.

Because Jenkins is extremely extensible, it can be modified and configured to handle practically any aspect of build orchestration and automation.

To learn more about what Jenkins can do, check out:

This tutorial shows you how to use the feature of Jenkins to create a Pipeline that will orchestrate building a simple application.

Before starting this tutorial, it is recommended that you run through at least one of the initial set of tutorials from the page first to familiarize yourself with CI/CD concepts (relevant to a technology stack you’re most familiar with) and how these concepts are implemented in Jenkins.

This tutorial uses the same application that the tutorial is based on. Therefore, you’ll be building the same application although this time, completely through Blue Ocean. Since Blue Ocean provides a simplified Git-handling experience, you’ll be interacting directly with the repository on GitHub (as opposed to a local clone of this repository).

Duration: This tutorial takes 20-40 minutes to complete (assuming you’ve already met the below). The exact duration will depend on the speed of your machine and whether or not you’ve already from .

If you’ve already run though , you can skip the and sections below and proceed on to . If you need to restart Jenkins, simply follow the restart instructions in and then proceed on.

- Read more about installing Docker in the section of the page.

In this tutorial, you’ll be running Jenkins as a Docker container from the Docker image.

To run Jenkins in Docker, follow the relevant instructions below for either or .

You can read more about Docker container and image concepts in the and sections of the page.

Run the jenkinsci/blueocean image as a container in Docker using the following command (bearing in mind that this command automatically downloads the image if this hasn’t been done):

| | Maps the /var/jenkins_home directory in the container to the Docker with the name jenkins-data. If this volume does not exist, then this docker run command will automatically create the volume for you. | | | Maps the $HOME directory on the host (i.e. your local) machine (usually the /Users/<your-username> directory) to the /home directory in the container. |

Proceed to the .

Run the jenkinsci/blueocean image as a container in Docker using the following command (bearing in mind that this command automatically downloads the image if this hasn’t been done):

For an explanation of these options, refer to the instructions above.

Proceed to the .

If you have some experience with Docker and you wish or need to access the Jenkins/Blue Ocean Docker container through a terminal/command prompt using the command, you can add an option like --name jenkins-tutorials (with the above), which would give the Jenkins/Blue Ocean Docker container the name "jenkins-tutorials".

After , the Customize Jenkins page appears.

Throughout the remainder of this tutorial, you can stop the Jenkins/Blue Ocean Docker container by typing Ctrl-C in the terminal/command prompt window from which you ran the docker run ... command .

Run the same docker run ... command you ran for or above. Note: This process also updates the jenkinsci/blueocean Docker image, if an updated one is available.

Ensure you are signed in to your GitHub account. If you don’t yet have a GitHub account, sign up for a free one on the .

Fork the on GitHub into your local GitHub account. If you need help with this process, refer to the documentation on the GitHub website for more information. Note: This is a different repository to the one used in the tutorial. Although these repositories contain the same application code, ensure you fork and use the correct one before continuing on.

In Connect to GitHub, click Create an access key here. This opens GitHub in a new browser tab. Note: If you previously configured Blue Ocean to connect to GitHub using a personal access token, then Blue Ocean takes you directly to step 9 .

Jenkins now has access to your GitHub account (provided by your access token).

In Which organization does the repository belong to?, click your GitHub account (where you forked the repository ).

Following on from creating your Pipeline project (), in the Pipeline editor, select docker from the Agent dropdown in the Pipeline Settings panel on the right.

Note: For an explanation of these values, refer to annotations 1 and 2 of the Declarative Pipeline in the tutorial.

Tip: The most commonly used step types appear closest to the top of this list. To find other steps further down this list, you can filter this list using the Find steps by name option.

Note: For an explanation of this step, refer to annotation 4 of the Declarative Pipeline in the tutorial.

( Optional ) Click the top-left back arrow icon to return to the main Pipeline editor.

Note: Before continuing on, you can check that Jenkins has created a Jenkinsfile for you at the root of your forked GitHub repository (in the repository’s sole master branch).

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

In the main Pipeline editor, click the + icon to the right of the Build stage you created to open the new stage panel on the right.

In the resulting Test / Shell Script panel, specify ./jenkins/scripts/test.sh and then click the top-left back arrow icon to return to the Pipeline stage editor.

Note: For an explanation of this directive and its step, refer to annotations 1 and 3 of the Declarative Pipeline in the tutorial.

( Optional ) Click the top-left back arrow icon to return to the main Pipeline editor.

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

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

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

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.

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 to return to the Pipeline stage editor.

Note: For an explanation of this step, refer to annotation 4 of the Declarative Pipeline in the tutorial.

( Optional ) Click the top-left back arrow icon to return to the main Pipeline editor.

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 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 tutorial). To do this:

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

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

Click the top-left back arrow icon to return to the main Pipeline editor.

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 (). However, when you inspect the Jenkinsfile again, you’ll notice that the environment directive is now a sibling of the agent section.

The page for other introductory tutorials.

The for more detailed information about using Jenkins, such as (in particular ) and the interface.

The for the latest events, other tutorials and updates.

Blue Ocean
Tutorials overview
Build a Node.js and React app with npm
prerequisites
run Jenkins in Docker
another tutorial
another tutorial
Prerequisites
Run Jenkins in Docker
forking the sample repository
Stopping and restarting Jenkins
Docker
Installing Docker
Installing Jenkins
jenkinsci/blueocean
macOS and Linux
Windows
Docker
Downloading and running Jenkins in Docker
Installing Jenkins
docker run
volume
Setup wizard
docker run
macOS and Linux
Setup wizard
docker exec
docker run
unlocking Jenkins
above
macOS, Linux
Windows
GitHub website
creating-a-pipeline-in-blue-ocean
Fork A Repo
Build a Node.js and React app with npm
below
above
above
above
above
environment
Build a Node.js and React app with npm
above
above
Tutorials overview
User Handbook
Pipelines
Pipeline syntax
Blue Ocean
Jenkins blog
Jenkins Blue Ocean
先决条件
在 Docker 中运行 jenkins
在 mac 和 linux 中运行 jenkins
在 windows 中运行 jenkins
访问 Jenkins Blue Ocean Docker 容器
设置 wizard
解锁 jenkins
使用插件定制化 jenkins
创建第一个管理员
启动和终止 jenkins
在 github 上 folk 实例仓库
在 Blue Ocean 创建 Pipeline 项目
创建初始化的 Pipeline
在Pipeline中添加一个test阶段
在Pipeline中添加一个最终提交阶段
跟进(可选的)
包装
“Create your initial Pipeline…​” section of the Build a Node.js and React app
“Create your initial Pipeline…​” section of the Build a Node.js and React app
“Add a test stage…​” section of the Build a Node.js and React app
“Add a final deliver stage…​” section of the Build a Node.js and React app
Configuring the agent
Adding the Build stage
Initial to GitHub
Connecting to GitHub
Unlock Jenkins page
Add <em>Build</em> stage
Specifying a shell step value
Downloading <em>npm</em> dependencies
Choosing a step type
Return from step icon
Save Pipeline dialog box
Edit Pipeline on branch
Repository branches page
Main Blue Ocean interface
Initial Pipeline runs successfully
Return from step icon
Environment directive
Return from step icon
Test stage runs successfully (with output)
Add <em>Deliver</em> stage
Add <em>Test</em> stage
Choosing the input step type
Return from step icon
Edit Pipeline on branch
Return from step icon
Deliver stage pauses for user input
Deliver stage output only
Deliver stage runs successfully
Return from step icon
Add next step
Edit Pipeline on branch
Return from step icon
Copying initial admin password
Main Blue Ocean interface with all previous runs displayed
Specifying input step message value