程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

After 37 failures, I finally successfully run CodeChinas Python based git project on Jenkins

編輯:Python

List of articles

  • background
  • problem
    • Tried sh Way
    • Tried paramiko Way
    • Tried the pagoda Python Project manager
  • Jenkins Of 38 Failure experience
  • The final command line
    • The complete configuration of the final version

background

I bought a small server in Tencent cloud

Build from scratch BI The first step of the system starts with buying a server

A scheduling system is built on it Jenkins

Tencent cloud server CentOS install Jenkins

And in codeChina There's a new one on Python Web project

After the public key is configured, you still cannot connect git The problem of

Now what I want to achieve is

① Write the project code on my computer ②push To Git Warehouse ③Git clone To server Linux On ④Python Run my project .py

from ①~④ Is an infinite loop , If not used Jenkins, My process will be very complicated . Will become
Write code >>ssh Connect to server >>cd To project directory >>pkill process >>rm -rf Catalog >>git clone project >>Python function
The whole process is very lengthy , Even a special sh file , Every time sh example.sh To batch run from cd To python Running projects , But it is still very complicated .sh Documents should also be modified frequently . At this time, I still feel Jenkins Incomparably good , In my push After code , It only needs one key to run , I can finish from ssh To python So many steps to run , You can also do it in console Record all processes , It's just too powerful


problem

The biggest problem is , The steps are too complicated , It just doesn't look like a BI What should be done

Tried sh Way

Create a new one sh file , The contents are as follows

pkill -9 -f main.py
rm -rf python_web
git clone https://xxx # project url
nohup python python_web.py # Operation without interruption after closing the terminal

And then every time

  1. ssh [email protected]
  2. cd /www/python
  3. sh exam.sh


It's too troublesome !!!


Tried paramiko Way

Write a paragraph Python Code , Dozens of lines

import paramiko
ssh = ...
...

There is a problem , stay git clone I always ask for a password when I'm in , I write Going in won't solve the problem . And every update py Have to upload it again , It's a real hassle !! Not yet log


Tried the pagoda Python Project manager

The advantage is that log
The shortcomings are too obvious

  • git clone Or manual
  • To reconfigure the module each time , Because each configuration item folder will be deleted
  • It can't be used at all requirements.txt Folder , No matter where it is, it can't be detected

so much trouble !!


Jenkins Of 38 Failure experience

Finally, I have to Jenkins, But it's not easy , I tried it here 37 Secondary failure

To sum up the experience and the lessons of blood and tears


  • Git Project voucher problem
ERROR: Error cloning remote repo 'origin'

You need to generate a key for the machine , Paste the public key into Git platform , After this process is completed, if you still do not have permission to pull , You can refer to

After the public key is configured, you still cannot connect git The problem of


  • Check whether there is a problem with the command selection
Cannot run program "cmd" (in directory...

I found that I chose the habit here perform Windows Batch command , Should be changed into perform shell


  • Python Module missing problem
+ python pyweb/main.py
Traceback (most recent call last):
File "pyweb/main.py", line 2, in <module>
import tornado.httpserver
ImportError: No module named tornado.httpserver

Here is the most troublesome problem I have ever encountered , Checked a lot of plans , All say Jenkins Will be in Workspace Create a new virtual directory , therefore Python The environment is also virtual , No modules installed . But before, when the local computer was running , There was no such problem . Finally, the problem may be

  • Python The execution path problem of
  • Linux Install multiple versions of by default Python problem
  • The path of the execution file

The details will be discussed in detail below


  • Python + Py Execute file server absolute path
    python /www/python/pyweb/main.py
    Throw the module missing error as above

  • Python + Py Executable files Jenkins Absolute path
    python /var/lib/jenkins/workspace/gittest/pyweb/main.py
    Error in file not found

    can't open file '/var/lib/jenkins/workspace/gittest/python_web_linux/pyweb/main.py': [Errno 2] No such file or directory
    
  • Python + Py Relative path of execution file
    python pyweb/main.py
    Throw the module missing error as above

  • Python Absolute path + Py Execute file server absolute path
    /root/.pyenv/shims/python /www/python/pyweb/main.py
    Throw the module missing error as above

  • Python Absolute path + Py Executable files Jenkins Absolute path
    /root/.pyenv/shims/python /var/lib/jenkins/workspace/gittest/pyweb/main.py
    Throw the module missing error as above

    can't open file '/var/lib/jenkins/workspace/gittest/python_web_linux/pyweb/main.py': [Errno 2] No such file or directory
    
  • Python Absolute path + Py Relative path of execution file
    /root/.pyenv/shims/python pyweb/main.py
    Throw the module missing error as above


  • Python Version of the problem
    adopt where python,which python Found that there are many versions of Python coexistence , So I tried
    python3 pyweb/main.py
    
    After the error in the above sentence is as follows
    python3: command not found
    
    I'll put it up 6 In contrast to x absolute (2x3) All the combinations have tried , The same mistake , Then I tried
    • python3.7 pyweb/main.py

    • python3.8 pyweb/main.py

      It's all the same command not found error


  • Try pip
    Then I think , Since you have no modules , Then I pip The installation is not good
    And then write that
pip install tornado
pip install pymysql
python pyweb/main.py

Report errors

commond not found
  • pip3 install tornado
  • /root/.pyenv/shims/pip install tornado
  • pip install pyweb/requirements.txt
  • pip3 install pyweb/requirements.txt
  • /root/.pyenv/shims/pip3 install pyweb/requirements
    All the above reports errors
    commond not found
    
  • /www/server/panel/pyenv/bin/pip install -r pyweb/requirements.txt
    There are some mistakes here
    Requirement already satisfied
    

  • Try running Python paramiko Files solve problems
    The result gave me an error
ImportError: No module named paramiko

  • Write directly on the command line git pull origin master
    It turned out to be rejected
Permission denied (publickey)

The final command line

After trying , By arranging and combining , It is found that some of them will be executed
So uninstall python3.8, Concentrate on using 3.7
Write... On the command line

/www/server/panel/pyenv/bin/python pyweb/main.py

Successful implementation


The complete configuration of the final version

  1. Specify the node as Linux

  2. Appoint Git Catalog
    First create a key pair and paste , Then write the project address

  3. Write the command line

After modifying the code each time ,push After arriving at the warehouse , Run directly from this key ! You can also view the complete record ~
A fun


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved