Part reference from the Dark Horse Programmer Java tutorial to automate Deploying Jenkins from environment configuration to project development

Pipeline project

Introduction to the

  • concept

Pipeline, simply put, is a workflow framework running on Jenkins that connects tasks that previously ran independently on a single or multiple nodes, enabling complex process choreography and visualization that would be difficult for a single task to accomplish.

  • advantage
  • Code: A Pipeline is implemented as code, usually checked in to source control, allowing the team to edit, review, and iterate on its delivery flow.
  • Persistence: Pipeline is recoverable from planned or unplanned server restarts. Stopable: The Pipeline may receive interactive input to determine whether to continue executing the Pipeline.
  • Multifunctional: Pipeline supports complex real-world continuous delivery requirements. It supports fork/join, circular execution, and parallel execution of tasks.
  • Extensibility: Pipeline plug-ins support custom scaling of their DSLS and multiple options for integration with other plug-ins.
  • tips
  • Pipeline scripts are implemented in the Groovy language, but we don’t need to learn Groovy alone
  • Pipeline supports two types of syntax: Declarative and Scripted Pipeline
  • There are also two ways to create a Pipeline:
  • Scripts can be entered directly into the Jenkins Web UI interface;
  • It is also possible to create a Jenkinsfile script file and put it into the project source library (it is generally recommended to load Jenkinsfile Pipeline directly from source control (SCM) in Jenkins).
  • Use of Declarative is usually recommended, and scripting is used only for complex projects

Installing pipeline Plug-ins

Create pipelined projects

Pipeline Syntax quick start

Next, enter the script directly into the Jenkins Web UI interface

The Declarative Declarative

  • Pipeline -> Select the HelloWorld template

  • The generated content is as follows:
 pipeline {
     agent any
 ​
     stages {
         stage('Hello') {
             steps {
                 echo 'Hello World'
             }
         }
     }
 }
Copy the code
  • Stages: Represents all stages of execution for the entire pipeline. Usually, there is only one stage, which contains multiple stages
  • Stage: represents a stage in the pipeline. There may be n stages. Generally divided into pull code, compile build, deployment and other phases.
  • Steps: Represents the logic that needs to be executed within a phase. Steps is shell script, Git pull code, SSH remote release and other arbitrary content.

Write a simple declarative Pipeline:

pipeline { agent any stages { stage('Pull') { steps { echo 'pull code' } } stage('Build') { steps { echo 'Build project'  } } stage('publish') { steps { echo 'publish project' } } } }Copy the code

  • Once the build is complete, you can see a phase view specific to a pipeline project

Scripted script type

  • Pipeline -> Select the Scripted Pipeline template

  • The following is a Scripted framework
 node {
     def mvnHome
     stage('Preparation') { // for display purposes
         
     }
     stage('Build') {
         
     }
     stage('Results') {
         
     }
 }
Copy the code
  • A Node is a Jenkins Node. The Master, or Agent, is the environment in which the Step is executed. This will be used later in the Jenkins master-slave architecture.
  • Stage: A Pipeline can be divided into several stages. Each Stage represents a group of operations, such as Build, Test, and Deploy. Stage is a concept of logical group.
  • Step: Step is the most basic operation unit, which can be to print a sentence or build a Docker image. It is provided by various Jenkins plug-ins, such as command: sh ‘make’, which is equivalent to the make command executed in our shell terminal at ordinary times.

Write a simple scripted Pipeline:

 node {
     def mvnHome
     stage('Pull') { // for display purposes
         echo 'pull code'
     }
     stage('Build') {
         echo 'build project'
     }
     stage('publish') {
         echo 'publish project'
     }
 }
Copy the code

The result of the build is the same as the declaration.

Write the Pipeline build project

Again, I’m going to go straight to the Jenkins Web UI interface, and we’re going to use declarative syntax

We don’t need to write complex scripts ourselves. We can use Jenkins’ tools to generate the corresponding syntax. Click “Pipeline Syntax”.

  • Use the Fragment Generator to select the code style to be generated

1. Pull the code

  • Select Checkout, select Git, fill in the warehouse address, select the corresponding credentials, and click “Generate pipeline Script”.

  • Copy and paste into steps

2. Compile and package

  • Select the shell

  • Write maven command
 mvn clean package
Copy the code

  • Copy the generated code to steps

Deployment of 3.

  • Select the deploy

  • Write the WAR packet address

  • Select the container type, our Tomcat is 8.5.69

  • Enter the Tomcat address

  • Copy the generated code to steps

build

pipeline { agent any stages { stage('Pull') { steps { checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '565ba95c-7302-465d-bdc4-55d561d7b6e7', url: 'https://gitee.com/whitesheep-y/javaweb_test.git']]]) } } stage('Build') { steps { sh 'mvn clean package' } } stage('publish') { steps { deploy adapters: [tomcat8(credentialsId: '2c037f4f-7484-4e0f-b795-1e3a870b24ec', path: "', url: 'http://192.168.1.107:8888/')], contextPath: null, war: 'the target / *. War'}}}}Copy the code
  • Apply save, go back to the project, and click Build

  • successful

Create the Jenkins script file

Our process above was to put the script into Jenkins’ UI interface, so this problem is a maintenance hassle. We then change the script’s code according to our build steps, without versioning. Another problem is that if the Jenkins server crashes, these scripts will also be lost.

Therefore, we prefer to use the file method and put the script under the project, so that our script file will be synchronized to the repository as our code is pushed

  • Create it in the project root directoryJenkinsfileFile, put the script code in there,Be careful not to have a suffix, such as.txt

  • And then push it to the repository.

  • Return to the project configuration and select Pipeline Script from ASCM at the Pipeline

  • Set relevant parameters and ensure that script paths match

  • To rebuild

  • Since then, we have achieved the initial implementation of programmable script automation continuous integration and deployment