In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Jenkins automation to build. NET Core application under Linux", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Jenkins to automate the construction of NET Core applications under Linux.
Deploy Jenkins
Please install Docker on Linux in advance. In Linux, we use Docker to start Jenkins, which avoids manual installation of a large number of dependencies and pollutes the local environment, and facilitates quick startup and failure recovery.
After installing Docker, use docker version to check the Docker version. The Docker version cannot be 1.x or 3.x. Please upgrade to version 18.x or above. Generally, in Ubuntu systems, the latest version is downloaded and installed, but in Centos, because the version in the default mirror source is older and the Doker version is lower, you need to manually add a new version of the image source and then upgrade.
Please refer to: https://www.yisu.com/article/243104.htm
Because you will use the container to provide the environment, and use Docker to package the .NET Core program as a Docker image, you need to map the Docker .sock file in the Jenkins container so that you can also use the Docker command in the container.
Create the / var/jenkins_home directory in the host to back up the data in Jenkins.
Start Jenkins using Docker in Linux with the following command:
Docker run\-u root\-itd\-p 8080 var/jenkins_home:/var/jenkins_home 50000\-v / var/jenkins_home:/var/jenkins_home\-v / var/run/docker.sock:/var/run/docker.sock\ jenkinsci/blueocean
After startup, use docker logs {Container ID} to view the log to get the Jenkins login password, which is a hash string, for example: 1abc12445adcf.
Finally, open port 8080 to access Jenkins.
Install the plug-in
After logging in to Jenkins, you will be prompted to install the recommended plug-ins. Follow the prompts to install the recommended plug-ins. The list of recommended plug-ins is as follows:
Folders PluginOWASP Markup Formatter PluginBuild TimeoutCredentials Binding PluginTimestamperWorkspace CleanupAntGradlePipelineGitHub Branch Source PluginPipeline: GitHub Groovy LibrariesPipeline: Stage ViewGit pluginSSH Build AgentsMatrix Authorization Strategy PluginPAM AuthenticationLDAPEmail ExtensionMailer PluginLocalization: Chinese (Simplified)
In addition, in order to provide container packaging support, the following plug-ins need to be installed manually.
# provide Docker support (required) tools for dockerDocker Pipelinedocker-build-step# visual design pipeline script (required) Blue Ocean# provides support for gitlab, if you don't use it, you can provide Git parameterization and hook trigger construction support without installing gitlab#, and pulling images without installing Git ParameterGeneric Webhook Trigger
Here are two images that we will use later.
Pull a .NET Core SDK, and each time you start the pipeline, you start a .NET Core SDK container, providing us with an environment for building, compiling and publishing .NET Core programs.
Docker pull mcr.microsoft.com/dotnet/sdk:3.1
Pull an ASP.NET Core Runtime to make the basic image of the application image.
Docker pull mcr.microsoft.com/dotnet/aspnet:3.1
Image list:
Mcr.microsoft.com/dotnet/aspnet 3.1 ac199e8d6dff 2 weeks ago 208MBmcr.microsoft.com/dotnet/sdk 3.1 82e2a34647f0 2 weeks ago 710MB make Jenkinsfile script
The author carefully prepared the Demo project for all the students, please go to the https://github.com/whuanle/DotNetCoreJenkinsDemo warehouse, fork to your warehouse, and then we use the Demo program of ASP.NET Core to practice the process.
In the project file, you can find a Jenkinsfile file that defines the pipeline steps for us.
Let's look at a simple Jenkinsfile template:
Pipeline {stages {stage ('Build') {steps {echo' Building..'}} stage ('Test') {steps {echo' Testing..'}} stage ('Deploy') {steps { Echo 'Deploying....'}
In stages, the phase of the pipeline is defined. In the template, one defines three phases, namely Build, Test, and Deploy.
Multiple steps (steps) can be defined for each phase, and multiple different types of step can be executed in each step.
Stage ('Test') {steps {echo' Testing..'}}
Jenkinsfile in demo is relatively simple, so we can take a look at the content of the Publish phase:
Stage ('Publish') {steps {sh' dotnet publish src/WebDemo-c Release'}}
In fact, it's very simple. Just execute the shell command at each step. We can use sh '...' Represents the shell command to be executed for this step.
Then let's take a look at the first two parts of Jenkinsfile.
/ / Global environment variable environment {IMAGENAME = 'webdemo' / / Image name IMAGETAG =' 1.0.0' / Image tag APPPORT = '8089' / / Port occupied by the application APPDIR =' / opt/app' / / directory where the application works} Agent {docker {image 'mcr.microsoft.com/dotnet/sdk:3.1' args'-v / var/run/docker.sock:/var/run/docker.sock-v / usr/bin/docker:/usr/bin/docker'}}
In environment, you can define the environment variables for this build process.
Agent is a node selector, you can choose in which environment to start the pipeline, there are node, docker and other types.
Build an assembly line
Log in to the background of the Jenkins page and click New Item in the left menu bar to start creating the pipeline.
Jenkins has a variety of pipeline styles, you can also add plug-ins to add more pipeline types, we mainly understand Freestyle project, Pipeline, Multibranch Pipeline.
Freestyle project
Freestyle project pipeline is relatively free to use, you do not need to make changes to the project to create the pipeline, you can add a lot of options and configurations. If the process needs to change, it is troublesome to change it.
Pineline 、 Multibranch Pipeline
Both of them can handle multiple branches at the same time, both of which depend on the Jenkinsfile file in the project. The advantage of Multibranch Pipeline is that you can examine multiple branches at the same time, creating a build Job for each branch, while Pipeline puts multiple branches into a single Job.
Click New Item, create the pipeline, and select Mutibranch Pipline.
Add Git source code repository, we are free to choose a type, it is recommended to use Single repository & branch.
Since we did not add a key, Jenkins cannot access your Github repository, so we need to add an accessible key.
Find a computer that already has git installed, generate a public and private key, and provide an encrypted connection between Jenkins and GitHub.
Execute the command:
Ssh-keygen-t rsa-b 2048-C "email@example.com"
When prompted, fill in the path where you want to store the key, such as D:/id_rsa.
After executing the command, two files, id_rsa and id_rsa.pub, are generated, and the contents of the id_rsa (private key) are copied to the Jenkins.
Then add the contents of id_rsa.pub (public key) to your Github account.
You can use a variety of ways to add authentication, the simplest and most secure is the key, we can select Secret file, and then upload the private key, or use other methods, which will not be discussed here.
Then select the branch to publish and save it directly.
Then go back to the main interface, open the pipeline we created, click Build Now, and start the pipeline.
Screenshot of successful construction:
On the Linux server, view the list of images:
Root@localhost ~ 19:54:20 # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEwebdemo 1.0.0 79b636ddef73 2 minutes ago 208MB
As you can see, it has been automatically packaged and applied as an image, and the image name and version number can be set in Jenkinsfile.
Observe
We can observe the log of each step in the build interface.
In Open Blue Ocean, there are better-looking UI and convenient tools.
Visual design pipeline steps.
Observe the construction process.
At this point, I believe you have a deeper understanding of "how to use Jenkins automation to build NET Core applications under Linux." you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 213
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.