Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python+JenkinsApi Automation deployment .net Core Project

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Deployment environment and process

1) Jenkins is a java product and needs to be installed with JDK8. Since the automatic release of the .netFreamwork project is based on Windows, we continue to use Windows, deploy and build the dotnet Core project under the existing Jenkins environment under Windows server 2012, and continue to operate under the Windows platform.

2) .NET Core SDK:2.2.402. Dotnet build build.

3) Code repository GitLab.

4) .NET Core server CentOS7

Deployment environment process:

Deployment requirements

A project is divided into two sub-projects, and the sub-projects need to be built and deployed separately, and after the construction, the sub-projects are packaged and synchronized to different server. So, plan to create three Job:A, B, and C on Jenkins. An and B represent sub-projects respectively, and C carries out unified management of An and B. That is, C is the construction entrance of An and B. Building an application based on parameters in C-Job will trigger the corresponding Job-B or Job-C construction. The idea of implementation is that python calls JenkinsAPI to implement, which will be discussed in detail later. The general process is as follows:

Jenkins operation

1) the specific installation of Jenkins. For download, see the official website: https://jenkins.io/download/, choose to install the Windows version.

Before installing Jenkins, you need to configure the JDK environment and configure JDK8.

2) plug-in installation

After Jenkins is installed, go to system Management-> plug-in Management, and install Msbuild, GitLab, python and so on.

3) create a new Job-A

Lists the main settings in Job.

The Gitlab code repository and branch configuration are as follows:

Build the build:

Create a new "Exceute Windows batch command" before build, as shown below:

Build command: dotnet restore "% WORKSPACE%\ PreventFraudAPI.Server" dotnet build "% WORKSPACE%\ PreventFraudAPI.Server" dotnet publish "% WORKSPACE%\ PreventFraudAPI.Server\ PreventFraudAPI.sln"-o "E:\ Publish-web\ PreventFraudAPI-test\ PreventFraudAPI.Server"

Dotnet restore: restore. The main purpose is to find the project file (project.json) under the current directory, use the NuGet library to restore the dependent library of the whole project, and traverse each directory to generate the project file, and continue to restore the dependencies in the project file.

Dotnet build: compile the application. This command generates the project and its dependencies into a set of binaries. The binaries include the project code in an intermediate language (IL) file with a .dll extension.

Dotnet publish: release the project so that programs can be run across platforms. After the Windows environment build is finished, it can be published and run in the Linux environment.

Dotnet run: run the application.

After the Build is built, create a new "Exceute Windows batch command" and pull the project file configuration item from the gitlab. Centralized storage in gitlbab. Copy the centralized configuration item file into the directory after the project has been built.

After the configuration file is operated, the project files that have been built under the Jenkins platform need to be packaged and synchronized to the CentOS7 Server on the .NET Core server. Windows and Linux different platform file operation, here choose Python implementation, mainly for server-side dotnet application service operation, application backup, code synchronization and so on.

This is implemented using the paramiko module under python. The paramiko module belongs to a third-party library and implements the SSHv2 protocol. You can directly use the SSH protocol in the Python code to perform operations on the remote server without having to operate the remote server through the ssh command. You need to install it with the following command before use:

Pip install paramiko

Paramiko contains two core components: SSHClient and SFTPClient.

SSHClient: similar to Linux's ssh command, it encapsulates a SSH session, which encapsulates Transport, Channel, and SFTPClient establishment methods (open_sftp), and is often used to execute remote commands.

SFTPClient: similar to Linux's sftp command, it encapsulates the SFTP client to implement remote file operations, such as file upload, download, file modification, and other operations.

The SSHClient component is used here according to the actual requirements.

The details of Python code are as follows:

Import sysimport paramikoclass SSHconnection (object): def _ init__ (self, host, port, username, password): self._host = host self._port = port self._username = username self._password = password self._transport = None self._client = None self._connect () # establish connect connection def _ connect (self): transport = paramiko.Transport ((self._host, self._port)) transport.connect (username=self._username Password=self._password) self._transport = transportdef exec_command (self, command, step): if self._client is None: self._client = paramiko.SSHClient () self._client.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) self._client._transport = self._transport stdin, stdout Stderr = self._client.exec_command (command) data = stdout.read () print ('% s execution result:'% step) print (data.decode ('utf-8')) # # output result # if len (data) > 0: # print (' execution result:') # print (data.decode ('utf-8')) err = stderr.read () # Output error result if len (err) > 0: print (error output executed by'% s:'% step) print (err.decode ('utf-8')) # close close connection def close (self): if self._transport: self._transport.close () if self._client: self._client.close () if _ name__== "_ _ main__ ": host =" 10.10.10.75 "port= 65089username =" root "password =" password "cmd_stop =" systemctl stop supervisor "cmd_backup =" cp-r / usr/soft/package/HiCore.PreventFraudAPI.Web/ / usr/soft/backup/HiCore.PreventFraudAPI.Web- `date +% Y-%m-%d-%H:% M` "cmd_rsync =" rsync-vzrtopg-- no-super-numeric-ids-- progress-- port=873-- password-file=/ Opt/scripts/passwd.sh rsync_user@10.10.10.69::PreventFraudAPI-test / usr/soft/package/HiCore.PreventFraudAPI.Web/ "cmd_start =" systemctl start supervisor "conn = SSHconnection (host Port, username, password) print ('start stopping supervisor services...') conn.exec_command (cmd_stop, 'supervisor_stop_service') print (' start backing up local PreventFraudAPI programs...') conn.exec_command (cmd_backup, 'backup_app') print (' start synchronizing PreventFraudAPI programs..') conn.exec_command (cmd_rsync, 'rsync_app') print (' start supervisor services..') conn.exec_command (cmd_start) 'supervisor_start_service') print (' PreventFraudAPI program release completed Please verify!')

In the same way, Job-B operates like Job-A. The main reason is that there are differences in Job-C, and there are parameters in the Job configuration.

4) create a new Job-C

Lists the main settings in Job.

Select "This project is parameterized" and add parameters to select "Choice Parameter". The configuration is as follows:

Triggers a remote Job build based on the selected parameters. If you select the option "Web" to trigger the remote Job-A build, select the option "File" to trigger the remote Job-B build.

Build the build:

Select the new "Exceute Windows batch command" in the build construction step, as shown below:

Python operation Jenkins API

At present, there are two third-party packages for API supported by the Python version of Jenkins.

JenkinsApi and Python-Jenkins

1) Python-Jenkins

Most articles recommend using the Python-Jenkins module, which is better encapsulated than the JenkinsApi,Python-Jenkins module, and the interface call is more convenient and easier. This is also the preferred recommended Python-Jenkins module, but it is wrong in practice:

This kind of error report read several articles on the Internet, most of which are described because jenkins-python exists in python3.6 and bug,url does not transcode. Here is the python3.7 version, which is also classified as a version issue. It can be used in Python2. The Python-Jenkins mode code is posted here, mainly to import the import jenkins module. The code is as follows: import jenkinsjenkins_server_url = 'http://jenkins.hicore.local/'user =' yuhuanghui'api_token= '11f6714b10b086b9165ed507dd2f5e161a'# instantiates the jenkins object and connects to the remote jenkins serverserver = jenkins.Jenkins (jenkins_server_url,username=user,password=api_token) print (server) # build jobserver.build_job (' PreventFraudAPI-test') # View the construction information of a job job_info=server.get_job_info ('PreventFraudAPI-test') print (job_info)

The python version will not be replaced here. The python used by other Job is Python3, so JenkinsApi is another way of API.

2) JenkinsApi

To use JenkinsApi mode, you need to import from jenkinsapi.jenkins import Jenkins and from jenkinsapi.build import Build modules.

The python script in Job-C above:

Import os,sysfrom jenkinsapi.jenkins import Jenkinsfrom jenkinsapi.build import Builddef get_server_instance (): jenkins_url = 'http://10.10.10.69:8080/'server = Jenkins (jenkins_url, username='xiaoming', password='password') return serverserver = get_server_instance () # version print (' Jenkins version:', server.version) # all job lists # print ('Jobs:', server.keys ()) print (' View Jobs list:' Server.get_jobs_list () # determine whether job exists There is a return true, there is no return false. Web_Job = server.has_job ('PreventFraud-test') File_Job = server.has_job (' PreventFraudFile-test') env = os.getenv ("ENV") print ('choose to release and build the application is:', env) if env = 'Web':print (' start Build to build PreventFraud-Web project!') If Web_Job = = True: # get job name job = server.get_job ('PreventFraud-test') print (' job to be built by Build is:', job) # build, no parameter build # params = {'Branch':' oriin/master', 'host':' 192.168.1.1'} res = server.build_job ('PreventFraud-test') print (' start triggering the construction of remote Job Please check the remote Job:%s'% job) print (res) # print (job.__dict__ ['_ data'] ['builds']) url = job.__dict__ [' _ data'] ['lastBuild'] [' url'] number = job.__dict__ ['_ data'] ['number'] obj = Build (url, number, job) print (' Job name of this build:% s The URL of Job is:% s, which is the% d build.' % (job, url, number) print ('result of build:', obj.get_status ()) else: print ('the Job to be built does not exist, please check!') Elif env = 'File':print (' start Build to build the PreventFraud-File project!') If File_Job = = True: # get job name job = server.get_job ('PreventFraudFile-test') print (' job to remotely trigger Build build is:', job) # build, no parameter build # params = {'Branch':' oriin/master', 'host':' 192.168.1.1'} res = server.build_job ('PreventFraudFile-test') print (' start triggering the construction of remote Job Please check the remote Job:%s'% job) print (res) # print (job.__dict__ ['_ data'] ['builds']) url = job.__dict__ [' _ data'] ['lastBuild'] [' url'] number = job.__dict__ ['_ data'] ['number'] obj = Build (url, number, job) print (' Job name of this build:% s The URL of Job is:% s, which is the% d build.' % (job, url, number) print ('result of build:', obj.get_status ()) else: print ('the Job to be built does not exist, please check!') Else:print ('Please select the correct ENV environment project!') Construction

As shown in the following figure:

Job-C console output:

After a few seconds, check the output of the Job-A console:

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: 0

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

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report