Cómo ejecutar un script como root en Jenkins?


Necesito ejecutar un script de shell en Jenkins como root en lugar del usuario predeterminado. ¿Qué necesito cambiar?

Mi archivo sudoers es así:

# User privilege specification
root    ALL=(ALL) ALL
igx     ALL=(ALL) ALL
%wheel  ALL=(ALL) ALL

# Allow members of group sudo to execute any command
# (Note that later entries override this, so you might need to move
%sudo   ALL=(ALL) ALL
#
#includedir /etc/sudoers.d

# Members of the admin group may gain root privileges
%admin  ALL=(ALL) NOPASSWD:ALL
root    ALL=(ALL) ALL
jenkins ALL=NOPASSWD: /var/lib/jenkins/workspace/ing00112/trunk/source/
jenkins ALL=(ALL) NOPASSWD:ALL
#Defaults:jenkins !requiretty
Author: Rob Bajorek, 2012-08-09

3 answers

Debe ejecutar el script usando sudo:

sudo /path/to/script

Pero antes debes permitir que jenkins tun el script en /etc/sudoers.

jenkins    ALL = NOPASSWD: /path/to/script
 71
Author: Igor Chubin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-09 09:10:44

La respuesta de@Igor Chubin es 100% correcta, pero nunca abra el archivo sudoer con un editor normal. utilice siempre visudo

Simplemente escriba

sudo visudo

Esto le llevará a /etc/sudoers y al guardarlo se asegurará de que no haya ningún error en el formato.

Si comete un error en el archivo sudoer, perderá el acceso a sudo, por lo que siempre use visudo

 8
Author: contactlp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-29 14:34:32

Me doy cuenta de que llego tarde a la fiesta en esta pregunta, pero por referencia pensé en lanzar mi 2c aquí: Uso el plugin SSH para Jenkins para lograr esto (simplemente configure un destino localhost). De esta manera, puedo contener el script directamente dentro de Jenkins (como un paso normal de "Ejecutar shell"), en lugar de usar sudo para invocar un script externo.

 5
Author: Nathan Crause,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-06 19:09:20