ArchLinux 2018.10.01: Install Jenkins for CI server

This article will describe installing Jenkins running on apache2.

1 Install Jenkins

The following script will install Jenkins and enable http access via Apache.

#!/bin/sh -e

jenkins_install()
{
  sudo pacman -Sy --noconfirm jenkins

  # shellcheck disable=SC1091
  . /etc/conf.d/jenkins
  JENKINS_OPTS="$JENKINS_OPTS --prefix=/jenkins"
  sudo sed -i /etc/conf.d/jenkins \
       -e "s;^JENKINS_OPTS=.*;JENKINS_OPTS=\"$JENKINS_OPTS\";g"

  sudo systemctl enable jenkins
  sudo systemctl start jenkins
}

apache_install()
{
  sudo pacman -Sy --noconfirm apache

  # https configuration.
  yes "" | sudo openssl req -new -x509 -nodes -newkey rsa:4096 -days 1095 \
                -keyout /etc/httpd/conf/server.key \
                -out /etc/httpd/conf/server.crt
  sudo sed -e 's/^#LoadModule ssl_module/LoadModule ssl_module/g' \
       -e 's/^#LoadModule socache_shmcb_module/LoadModule socache_shmcb_module/g' \
       -i /etc/httpd/conf/httpd.conf
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/httpd-ssl.conf
EOF

  # proxy configuration.
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^#LoadModule proxy_module/LoadModule proxy_module/g' \
       -e 's/^#LoadModule proxy_http_module/LoadModule proxy_http_module/g'

  # rewrite configuration.
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^#LoadModule rewrite_module/LoadModule rewrite_module/g'

  # Jenkins configuration.
  cat << EOF | sudo tee /etc/httpd/conf/extra/jenkins.conf
ProxyRequests     Off
ProxyPreserveHost On
ProxyPass         /jenkins http://localhost:8090/jenkins
ProxyPassReverse  /jenkins http://localhost:8090/jenkins

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

AllowEncodedSlashes NoDecode

<Proxy http://localhost:8090/jenkins>
  Order deny,allow
  Allow from all
</Proxy>
EOF
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/jenkins.conf
EOF

  sudo systemctl enable httpd
  sudo systemctl restart httpd
}

jenkins_main()
{
  jenkins_install
  apache_install
}

jenkins_main

2 Access to Jenkins

Access to the following URL.

https://<server>/jenkins

0001_Jenkins.png