[Jenkins] 配置任務中的坑s


Jenkins

坑1:sh: adb: command not found

背景:在任務中使用了adb命令

adb 使用時要在服務器上配Android-home的環境變量的

配置完成之后發現在服務器上 adb OK,但是在jenkins 執行job時不OK

參考一下兩篇文檔

https://stackoverflow.com/questions/29216244/jenkins-build-failed-due-to-missing-android-sdk

http://www.linuxdiyf.com/linux/28771.html

 

1. 在jenkins 設置里配置Android-home,然而並沒有用

2. 在Execute shell 里將#!/bin/bash 改成 #!/bin/bash -ilex

jenkins沒有加載/etc/profile導致,需要在jenkins調用shell腳本的最前面加一行腳本,#!/bin/bash -ilex,可以通過-i參數和-l參數讓bash為login shell and interactive shell,就可以讀取/etc/profile和~/.bash_profile等文件

3. 實際挨個試了下#!/bin/bash -ilex中的各個參數 發現有用的是-l

對於e參數表示一旦出錯,就退出當前的shell,x參數表示可以顯示所執行的每一條命令。

'''

-l        Make bash act as if it had been invoked as a login shell (see

                 INVOCATION below).

 

When  bash is invoked as an interactive login shell, or as a non-inter-

       active shell with the --login option, it first reads and executes  com-

       mands  from  the file /etc/profile, if that file exists.  After reading

       that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,

       in  that order, and reads and executes commands from the first one that

       exists and is readable.  The --noprofile option may be  used  when  the

       shell is started to inhibit this behavior.

''' 

 綜上,就是加載了/etc/profile或者其他幾個配置環境變量的文件

所以直接改成在 shell 腳本里增加source /etc/profile就好啦~

 

 

坑2 Jenkins + python 的任務,console output 沒有及時輸出日志

期初是以為是 #!/bin/bash -ilex 中四個參數導致console output 沒有輸出,后來突然意識到是python的print應該有buffer的問題

https://stackoverflow.com/questions/22826006/how-to-get-python-print-result-in-jenkins-console-output

https://stackoverflow.com/questions/11631951/jenkins-console-output-not-in-realtime

有兩種解釋

1.jenkins 要輸入換行符才能展示數據

Any output to stdout from a process spawned by Jenkins should be captured by Console Output. One caveat is that it won't be displayed until a newline character is printed, so make sure your lines are terminated.

2.print有buffer

  • ruby or python or any sensible scripting language will buffer the output; this is in order to minimize the IO; writing to disk is slow, writing to a console is slow...
  • usually the data gets flush()'ed automatically after you have enough data in the buffer with special handling for newlines. e.g. writing a string without newline then sleep() would not write anything until after the sleep() is complete (I'm only using sleep as an example, feel free to substitute with any other expensive system call).

解決方法看到兩個

1.print 信息后加sys.stdout.flush()

2. 執行python時增加 -u 參數,即 python -u XXX.py

  • for python, you can use python -u or the environment variable PYTHONUNBUFFERED to make stdin/stdout/stout not buffered, but there are other solutions that do not change stdin or stderr

    export PYTHONUNBUFFERED=1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM