Your support means a lot to me!

🔥 Hi, I’m Peng. This article has been included in GitHub · Android-Notebook. Here are Android advanced growth route notes & blog, like-minded friends, welcome to grow with me. (Contact & Group entry on GitHub)

preface

  • Jenkins is a Continuous Integration tool based on Java development, which can be easily used as an extensible automation server to provide Continuous Integration capabilities for projects.
  • In this article, I will walk you through building a Jenkins LAN service and implementing a simple Jenkins project. I hope to give you a basic impression of What it would be like to build a rough model of a Jenkins project. Please be sure to like and follow if you can help, it really means a lot to me.

1. Deploy Jenkins LAN service

Let’s go over the steps to deploy the Jenkins service, because there’s a lot of material on the web about this, so I’ll just go over the key steps and what needs to go wrong. My requirement is to realize a Jenkins service available on the LAN. The deployment steps will be relatively simple. First, a service host that is powered on for a long time is required.

  • 1. Download Jenkins installation program and execute it:The official website
  • 2, Installation process – Select login permission assignment type: here select “Run Server as LocalSystem” option (not recommended, but sufficient) :

  • 3, installation process – select port number: select an idle port:

  • 4. Unlock Jenkins: Enter localhost:7777 in the browser to unlock Jenkins when it is first started. Follow the instructions:

  • 5. Create an administrator account: Create an additional administrator account, which can be shared with other students in the group to log in to Jenkins service:

  • 6. Access Jenkins service from other hosts:Native access Jenkins service is accessiblelocalhost:7777. This parameter must be used before other LAN hosts access itipconfigCheck the local IP address before using itip:7777Access, and then useStep 5Log in with the administrator account of. Enter Jenkins Web interface:

After completing the above steps, a simple Jenkins service is deployed.


2. Manage Jenkins environment

You’ll need to do some extra configuration work before you actually start your Jenkins mission, and you’ll get a look at the Jenkins interface and related concepts.

2.1 Download Jenkins plugin

Jenkins can be extended with plugins. You can build your continuous integration process directly using existing plugins in the Jenkins Plugins Center.

Enter the plug-in middle you can see “updatable – optional plug-ins – installed” items, “updatable” list is used to keep updated. Xiaopeng’s project and the following examples will use the plug-ins of GitLab and Qy Wechat Notification. Please download and Install the plug-ins of GitLab in the “Optional plug-ins”, check the plug-ins and click the Install button at the bottom to Install them.

During the installation process, you can check “Restart Jenkins after installation (when idle)”, because some plug-ins need to restart Jenkins service to take effect, we just keep the habit of restarting. If you need to manually restart, you can directly access: IP :7777/reload.

2.2 Adding Credentials

Jenkins can interact with third-party application platforms (Github/GitLab), and these apps usually do permissions, so you need to configure credentials on Jenkins. Currently, Jenkins can store the following types of credentials:

credentials describe
1, the Secret of the text API tokens (e.g. GitHub’s personal Access Token)
Username with password Username: a string in the format of password (for example, GitHub username and password)
3, Secret file Encrypted content in the form of files
SSH Username with private key SSH private key (e.g. SSH public key configured on GitHub, SSH private key configured on Jenkins)
5, Certificate PKCS#12 certificate file and optional password
Docker Host Certificate Authentication Docker host certificate authentication credentials

Jenkins: Adding SSH global credentials, written by a.net developer

2.3 Global Tool Configuration

The Global Tool Configuration is used to configure the build Tool installation path, or the automatic installation program, such as configuring the automatic installation of Gradle tools here:

2.4 Configure System

Configure System configures globally available build configurations, such as Jenkins urls, global environment variables, etc. :

ANDROID_SDK_ROOT and GRADLE_USER_HOME are the environment variables that need to be configured.

* What went wrong: Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'. > SDK location not found. Define location  with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at  'C:\ProgramData\Jenkins\.jenkins\workspace\JenkinsTest2\local.properties'.Copy the code
> Could not resolve all files for configuration ': app: debugRuntimeClasspath'. > Failed to transform appcompat - 1.2.0. Aar (androidx. Appcompat: appcompat: 1.2.0) to match the attributes {artifactType = android - compiled as dependencies - resources, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}. > Execution failed for AarResourcesCompilerTransform: C: \ Windows \ System32 \ config \ systemprofile \. Gradle \ caches \ transforms - 2 \ \ b9972f23873aac7b9a345e4f8f7b8547 \ appcompa files - 2.1 > Android Resource compilation failed ERROR:: AAPT: C: \ Windows \ System32 \ config \ systemprofile \. Gradle \ caches \ transforms - 2 \ \ f05998388cdecf8820753332a73f5edf \ androidx files - 2.1 Appcompat: error: The system cannot find the specified file. (2)Copy the code


3. First Jenkins Project

Now that we’re ready, we can start building our own build project. From the homepage “New Items”, you can enter the project creation page. Freestyle and Pipeline are the two most commonly used project types. Let’s start with simple Freestyle Freestyle projects. Jenkins can do a lot of things, so we’re just going to cover the main things today. Learn a new subject from the whole to the parts, to avoid getting bogged down in details at first.

Let’s first imagine what we need to achieve in our first Jenkins project: compile and build the Android project, and notify the enterprise wechat when the build is successful. Step by step:

  • Jenkins supports adding parameters at build time (This project is parameterized). Here we add a BRANCH parameter (default: develop) :

  • 2. Source code management: Associated source repository. Here we are associated with projects hosted on Gitlab, using the previously defined parameter BRANCH in the “specify BRANCH” area. Where, Pengxr Window is the Jenkins credential added in advance. For details, please refer to 2.2 Adding Credentials.

  • 3. Configure build triggers: Triggers determine the timing of the build task and allow multiple triggers to be set at the same time, as long as one of the triggers meets the criteria, the build task will be executed. The simplest manual remote trigger is configured first. We will cover the other triggers in Section 4.

  • 4. Define build tasks:The build task defines the actual required build logic, and here we use the invokeGradle type. The specific task is simpleassembleDebug. You can also Use the Use Gradle Wrapper option directly. Jenkins will automatically download the Gradle version required for the project.

  • 5. Define post-build operations: Some notifications can be made after the completion of the build, such as sending a message to the enterprise wechat/Dingding. Here, we use the Qy Wechat Notification plug-in to send messages to the enterprise Wechat group:

In the Build with Parameters section of the Project panel, you can see the parameter configuration interface:

  • 6. Trigger the build: You can trigger the build directly by clicking the interface button, or you can use the remote trigger set in step 3 to access the build directly in the browser:

    • No parameters:
    http://ip:7777/job/91160AndroidDaily/build?token=myToken
    Copy the code
    • Carrying parameters:
    http://ip:7777/job/91160AndroidDaily/buildWithParameters?token=myToken&BRANCH=feature_user
    Copy the code

Enter build task details and view logs in console Output:


4. Build Triggers

Jenkins automates build tasks. You can select different build triggers to set trigger conditions, some of which require a special Jenkins plugin installed. I categorize the common Jenkins build triggers into three categories: manual trigger – cycle trigger – associated trigger, and give some examples:

category Flip-flop example describe
Manual trigger Triggering remote builds (for example, using scripts) Triggered by accessing a specific URL link, which concatenates an authentication Token for identification
Cycle trigger Build periodically Trigger at an interval
Cycle to build Poll SCM Periodically check for source repository changes and trigger if they happen (note: this is a relatively time-consuming operation because you need to compare the entire CVS workspace)
Associated with the trigger Build after other projects are built Trigger after another Projects build (e.g. trigger a build task for automated tests after build)
Link building GitHub hook trigger for GITScm polling The build is triggered when code is submitted to GitHub

This review focuses on the periodically Build trigger calendar syntax, and other triggers are used in practice.

A calendar is simply a configuration of build cycles/frequencies, syntactically divided into five parameters, each separated by a space:

  • Minute: The value ranges from 0 to 59. The special value H indicates random
  • Hour: The value ranges from 0 to 23
  • Day: The value ranges from 1 to 31
  • Month: Value ranges from 1 to 12
  • Week: The value ranges from 0 to 7. 0 and 7 are Sunday.

Such as:

  • H 9 * * *The command is executed randomly at 9:00 a.m. every day
  • 0, 1, 1, * *It is executed at 1 o ‘clock on the first day of each month
  • 0, 1 times * 1, minus 5This parameter is executed from 1 a.m. to 1 a.m. Friday
  • 0 1 minus 12/3 times * * *The command is executed from 1:00 to 12:00 every day at an interval of three hours

* indicates all. For example, the month parameter * indicates the period from January to December

In the command, – indicates the range. For example, if the parameter ranges from 1 to 5, it indicates the range from 1 to 5

In the command, / indicates the interval. For example, the daily parameter 1-12/3 indicates the interval of 3 hours from 1 to 12


5. To summarize

So that’s it for Jenkins, have you learned it? A few smart people will ask: You don’t talk too much about Pipeline lines and multi-channel packaging. Yes, first of all, I think Jenkins is just an efficiency tool at the end of the day. Tools can be mastered to solve problems. Don’t put the cart before the horse. Second of all, we finished Jenkins’ main mission, so we can do the side missions with ease. Look forward to the sequel. I’ll see you next time.

The resources

  • Start using Jenkins — official documentation
  • Jenkins: Adding SSH global Credentials — by a rookie.NET developer

Your likes mean a lot to me! Wechat search public number [Peng Xurui], I hope we can discuss technology together, find like-minded friends, we will see you next time!