{"id":179,"date":"2020-07-11T18:28:43","date_gmt":"2020-07-11T23:28:43","guid":{"rendered":"https:\/\/mtu.net\/~jpschewe\/blog\/?p=179"},"modified":"2020-07-14T12:11:54","modified_gmt":"2020-07-14T17:11:54","slug":"configure-linux-jenkins-node","status":"publish","type":"post","link":"https:\/\/mtu.net\/~jpschewe\/blog\/2020\/07\/configure-linux-jenkins-node\/","title":{"rendered":"Configure Linux Jenkins node"},"content":{"rendered":"\n<p>I have been setting up a few Jenkins nodes lately and decided that I should write up the configuration that I&#8217;m using to share with others.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create the node in Jenkins<\/h2>\n\n\n\n<p>The first thing to do is to create the node in Jenkins. Start by logging into your Jenkins host, then visit the &#8220;Manage Jenkins&#8221; link. Once there, visit &#8220;Manage Nodes&#8221; and then click &#8220;New Node&#8221; on the left.<\/p>\n\n\n\n<p>Give your node a name. It&#8217;s a good idea to avoid spaces and special characters. I use letters, numbers, underscores and hyphens. Select &#8220;Permanent Agent&#8221; and then &#8220;OK&#8221;.<\/p>\n\n\n\n<p>Here you need to specify the working directory, labels and the usage. I usually set the usage to only build jobs with a matching label expression. This is useful when setting up nodes per job to make sure that the node doesn&#8217;t get used for other random jobs. You may also want to specify an email address to notify when the node goes online and\/or offline.<\/p>\n\n\n\n<p>Once you have saved the configuration you will see a page specifying that the agent is offline and how to launch it. The important piece of information here is the secret. This will be a very long string of letters and numbers. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Linux Setup<\/h2>\n\n\n\n<p>First create a user in Linux that the node will run as. This user should not have any special privileges.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo adduser JENKINS_BUILD_USER<\/code><\/pre>\n\n\n\n<p>Replace &#8220;JENKINS_BUILD_USER&#8221; with the username that you are using. By default this user has a locked password so no one can login as this user.<\/p>\n\n\n\n<p>In &#8220;\/home\/JENKINS_BUILD_USER&#8221; create the file &#8220;start-jenkins-node.sh&#8221; to start the node<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\n\ndebug() { ! \"${log_debug-false}\" || log \"DEBUG: $*\" >&amp;2; }\nlog() { printf '%s\\n' \"$*\"; }\nwarn() { log \"WARNING: $*\" >&amp;2; }\nerror() { log \"ERROR: $*\" >&amp;2; }\nfatal() { error \"$*\"; exit 1; }\ntry() { \"$@\" || fatal \"'$@' failed\"; }\n\nmydir=$(cd \"$(dirname \"$0\")\" &amp;&amp; pwd -L) || fatal \"Unable to determine script directory\"\n\njenkins_host=JENKINS_HOST\njenkins_node_name=NODE_NAME\njenkins_node_secret=SECRET\n\ncd \"${mydir}\"\n# --no-check-certificate is needed if the certificate store does not recognize the jenkins host certificate\ntry wget https:\/\/${jenkins_host}\/jnlpJars\/agent.jar -O agent.jar\n\n# -noCertificateCheck is needed if the certificate isn't recognized\nnohup java -jar agent.jar -jnlpUrl https:\/\/${jenkins_host}\/computer\/${jenkins_node_name}\/slave-agent.jnlp -secret ${jenkins_node_secret} -workDir \"${HOME}\" > \"${HOME}\"\/jenkins-node.log 2>&amp;1\n<\/code><\/pre>\n\n\n\n<p>Replace JENKINS_HOST with the hostname that Jenkins is running on. This script assumes that Jenkins is running at hte root of your server. If that&#8217;s not the case you&#8217;ll want to append the base path to the end of JENKINS_HOST. Replace NODE_NAME with the name of the node and SECRET with the secret from the node configuration on the Jenkins host.<\/p>\n\n\n\n<p>Mark the file executable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x \/home\/JENKINS_BUILD_USER\/start-jenkins-node.sh<\/code><\/pre>\n\n\n\n<p>Create &#8220;\/etc\/systemd\/system\/jenkins_node.service&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Service]\nType=simple\nExecStart=\/home\/JENKINS_BUILD_USER\/start-jenkins-node.sh\nWorkingDirectory=\/home\/JENKINS_BUILD_USER\nRestart=always\nRestartSec=60\nUser=JENKINS_BUILD_USER\n\n&#91;Unit]\nAfter=network-online.target\nWants=network-online.target\n\n&#91;Install]\nWantedBy=default.target\n<\/code><\/pre>\n\n\n\n<p>Replace JENKINS_BUILD_USER with the user that you created. Then you can enable and start the service with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo systemctl enable jenkins_node\nsudo systemctl start jenkins_node<\/code><\/pre>\n\n\n\n<p>At this point you should see your node online in Jenkins and you are ready to use it for jobs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been setting up a few Jenkins nodes lately and decided that I should write up the configuration that I&#8217;m using to share with others. Create the node in Jenkins The first thing to do is to create the node in Jenkins. Start by logging into your Jenkins host, then visit the &#8220;Manage Jenkins&#8221; &hellip; <a href=\"https:\/\/mtu.net\/~jpschewe\/blog\/2020\/07\/configure-linux-jenkins-node\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Configure Linux Jenkins node<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,1],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-jenkins","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":3,"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/posts\/179\/revisions\/182"}],"wp:attachment":[{"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mtu.net\/~jpschewe\/blog\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}