1

I try to install EC2 instance, with pre-installed Jenkins, using the following Packer configuration:

The most important part here is setup.sh script, which installs Jenkins and Java:

#!/bin/bash

echo "Installing Amazon Linux extras"
amazon-linux-extras install epel -y

echo "Install Jenkins stable release"
yum remove -y java
yum install -y java-11-openjdk-devel
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins
chkconfig jenkins on

echo "Install git"
yum install -y git

echo "Setup SSH key"
mkdir /var/lib/jenkins/.ssh
touch /var/lib/jenkins/.ssh/known_hosts
chown -R jenkins:jenkins /var/lib/jenkins/.ssh
chmod 700 /var/lib/jenkins/.ssh
mv /tmp/id_rsa /var/lib/jenkins/.ssh/id_rsa
chmod 600 /var/lib/jenkins/.ssh/id_rsa
chown -R jenkins:jenkins /var/lib/jenkins/.ssh/id_rsa

echo "Configure Jenkins"
mkdir -p /var/lib/jenkins/init.groovy.d
mv /tmp/scripts/*.groovy /var/lib/jenkins/init.groovy.d/
chown -R jenkins:jenkins /var/lib/jenkins/init.groovy.d
mv /tmp/config/jenkins /etc/sysconfig/jenkins
chmod +x /tmp/config/install-plugins.sh
bash /tmp/config/install-plugins.sh
service jenkins start

See all other configuration files by this link:

https://github.com/mlabouardy/pipeline-as-code-with-jenkins/tree/master/chapter4/distributed/master

When I run:

packer build template.json

I get the following exception:

==> amazon-ebs: Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.

Everything works perfectly, If I use:

yum install -y java-1.8.0-openjdk

instead of:

yum install -y java-11-openjdk-devel

I'm able to start Jenkins, but I see the following warning:

Using Java 8 with the latest Jenkins is not recommended

Does anyone know how to make this configuration work with Java 11 ?

P.S. As a Source Amazon Machine Image for Packer I use:

Amazon Linux 2 AMI (HVM), SSD Volume Type - ami-02e136e904f3da870 (64-bit x86) / ami-0e341fcaad89c3650 (64-bit Arm)

It is available in the "us-east-1" region ("source_ami" : "ami-02e136e904f3da870" - the exact id of Amazon Linux 2 AMI depends on the region)

Here is the Packer template.json, which is used for baking Jenkins Image from the Source Image:

{
    "variables" : {
        "region" : "us-east-1",
        "aws_profile": "ops-account",
        "source_ami" : "ami-02e136e904f3da870",
        "instance_type": "t2.micro",
        "ssh_key": "./jenkins_ssh"
    },
    "builders" : [
        {
            "type" : "amazon-ebs",
            "profile" : "{{user `aws_profile`}}",
            "region" : "{{user `region`}}",
            "instance_type" : "{{user `instance_type`}}",
            "source_ami" : "{{user `source_ami`}}",
            "ssh_username" : "ec2-user",
            "ami_name" : "jenkins-master-2.204.1",
            "ami_description" : "Amazon Linux Image with Jenkins Server",
            "run_tags" : {
                "Name" : "packer-builder"
            }
        }
    ],
    "provisioners" : [
        {
            "type" : "file",
            "source" : "./scripts",
            "destination" : "/tmp/"
        },
        {
            "type" : "file",
            "source" : "./config",
            "destination" : "/tmp/"
        },
        {
            "type" : "file",
            "source" : "{{user `ssh_key`}}",
            "destination" : "/tmp/id_rsa"
        },
        {
            "type" : "shell",
            "script" : "./setup.sh",
            "execute_command" : "sudo -E -S sh '{{ .Path }}'"
        }
    ]
}
Mykhailo Skliar
  • 1,242
  • 1
  • 8
  • 19

3 Answers3

1

Adding the following command line to setup.sh solved the issue:

amazon-linux-extras install java-openjdk11 -y

It appears, that java-11-openjdk packages are not available in the Amazon Linux 2 main repository to install.

But they are now available in the Amazon Linux 2 extras repository

Mykhailo Skliar
  • 1,242
  • 1
  • 8
  • 19
1

Most of the people having issue for installation jenkins in ec2-user.

Jenkins Redhat Packages To use this repository, run the following command:

  1. sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

  2. sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

  3. yum install epel-release ( it will not work so use below command ) sudo amazon-linux-extras install epel

  4. yum install java-11-openjdk-devel -y yum install java-11-openjdk-devel (it will not working so use below command)

  5. yum search all "java"

After that:

yum install java-11-amazon-corretto.x86_64
java --version

enter image description here

enter image description here

  1. yum install jenkins
  2. systemctl start jenkins
  3. systemctl enable jenkins
ouflak
  • 2,458
  • 10
  • 44
  • 49
0

If you still face errors while starting jenkins after installing jdk 11? check the java version java -version

If it shows the java 1.8.* something that comes by default with amazon ami linux ec2, we need to change to point to jdk 11 version. try below command

alternatives --config java --> select 2 There are 2 programs which provide 'java'.

Selection Command

*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.amzn2.0.1.x86_64/jre/bin/java) 2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.amzn2.0.1.x86_64/bin/java)

then the java -version should show 11 version. Now installing jenkins and starting it should not be a problem.

Sindhu
  • 1