Skip to content

Commit

Permalink
feat: configure AMI to include Jenkins configuration
Browse files Browse the repository at this point in the history
Update install script to include Jenkins plugins installation through a
`plugins.txt` file.
Include Configurations as Code `yaml` file that configures a basic
multi-branch pipeline seed job for job-dsl, written in Groovy.

Fixes issues #21 #23
  • Loading branch information
sydrawat01 committed Oct 20, 2023
1 parent 5ccd1fc commit 3e107ee
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 27 deletions.
16 changes: 16 additions & 0 deletions scripts/casc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
obs:
- script: >
multibranchPipelineJob('test-seed') {
branchSources {
git {
id('92098798334')
remote('https://github.com/username/example')
includes('*')
}
}
orphanedItemStrategy {
discardOldItems {
numToKeep(5)
}
}
}
132 changes: 106 additions & 26 deletions scripts/install.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| INSTALL SCRIPT v1.0 |"
echo "| INSTALL SCRIPT v2.0 |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

Expand All @@ -17,12 +17,31 @@ sudo apt update --quiet && sudo apt upgrade -y

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| INSTALL JAVA 11 |"
echo "| INSTALL JAVA 17 |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# Install Java for Jenkins
sudo sudo apt install openjdk-11-jdk -y
# Install Java for Jenkins: (https://adoptium.net/installation/linux/)

# Ensure necessary packages are present:
sudo apt install -y wget apt-transport-https

# Download the Eclipse Adoptium GPG key:
sudo mkdir -p /etc/apt/keyrings
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee \
/etc/apt/keyrings/adoptium.asc

# Configure the Eclipse Adoptium apt repository:
# To check the full list of versions supported take a look at the list in the tree at https://packages.adoptium.net/ui/native/deb/dists/.
# For Linux Mint (based on Ubuntu) you have to replace VERSION_CODENAME with UBUNTU_CODENAME.
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] \
https://packages.adoptium.net/artifactory/deb \
$(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | sudo tee \
/etc/apt/sources.list.d/adoptium.list

# Install the Temurin version you require:
sudo apt update # required to refresh apt with the newly installed keys
sudo apt install temurin-17-jdk -y

# Validate Java installation
JAVA=$?
Expand All @@ -32,13 +51,7 @@ else
echo "Unable to install Java"
fi

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| CHECK JAVA VERSION |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# Check Java version
# Check Java version:
echo "Java $(java -version)"

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
Expand All @@ -47,8 +60,7 @@ echo "| INSTALL J
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# Update local package index
sudo apt update --quiet
# Jenkins setup on Debian (stable): https://pkg.jenkins.io/debian-stable/

# Debian package repository of Jenkins to automate installation and upgrade.
# To use this repository, first add the key to the system:
Expand All @@ -60,8 +72,8 @@ echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list >/dev/null

# Update local package index, then finally install Jenkins:
sudo apt update --quiet
# Install Jenkins:
sudo apt update # required to refresh apt with the newly installed keys
sudo apt install jenkins -y

# Validate Jenkins installation
Expand All @@ -73,13 +85,7 @@ else
fi

# Check the status of Jenkins service
sudo systemctl status jenkins

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| CHECK JENKINS VERSION |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
sudo systemctl --full status jenkins

# Check Jenkins version
echo "Jenkins $(jenkins --version)"
Expand All @@ -91,17 +97,17 @@ echo "| INSTALL CA
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# Update local package index
sudo apt update --quiet
# Caddy(stable) installation docs: https://caddyserver.com/docs/install#debian-ubuntu-raspbian

# Install and configure keyring for caddy stable release:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo \
gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee \
/etc/apt/sources.list.d/caddy-stable.list

# Update local package index, then finally install caddy:
sudo apt update --quiet
# Install caddy:
sudo apt update # required to refresh apt with the newly installed keys
sudo apt install caddy -y

# Validate caddy installation:
Expand All @@ -111,3 +117,77 @@ if [ $CADDY -eq 0 ]; then
else
echo "Unable to install the Caddy Service"
fi

# Check the status of Caddy service
sudo systemctl --full status caddy

# https://caddyserver.com/docs/quick-starts/reverse-proxy
# For details on configuring reverse-proxy:
# https://github.com/cyse7125-fall2023-group05/infra-jenkins/blob/master/modules/ec2/userdata.sh
# To remove reverse proxy error: Jenkins->manage->configure->Jenkins URL->"caddy1"

# Jenkins Configuration
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| CONFIGURE JENKINS |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# Install Jenkins plugin manager tool:
wget https://github.com/jenkinsci/plugin-installation-manager-tool/releases/download/2.12.13/jenkins-plugin-manager-2.12.13.jar

# Install plugins with jenkins-plugin-manager tool:
sudo java -jar ./jenkins-plugin-manager-2.12.13.jar --war /usr/share/java/jenkins.war \
--plugin-download-directory /var/lib/jenkins/plugins --plugin-file plugins.txt

# Update users and group permissions to `jenkins` for all installed plugins:
cd /var/lib/jenkins/plugins/ || exit
sudo chown jenkins:jenkins *

# Configure JAVA_OPTS to disable setup wizard
sudo mkdir -p /etc/systemd/system/jenkins.service.d/
{
echo "[Service]"
echo "Environment=\"JAVA_OPTS=-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false -Dcasc.jenkins.config=/var/lib/jenkins/casc.yaml\""
} | sudo tee /etc/systemd/system/jenkins.service.d/override.conf
sudo systemctl daemon-reload
sudo systemctl stop jenkins
sudo systemctl start jenkins

# Docker Setup
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| INSTALL DOCKER |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# Add Docker's official GPG key:
sudo apt install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |
sudo tee /etc/apt/sources.list.d/docker.list >/dev/null

# Install Docker:
sudo apt update
sudo apt install docker-ce -y

# Validate docker installation:
DOCKER=$?
if [ $DOCKER -eq 0 ]; then
echo "Successfully installed Docker"
else
echo "Unable to install Docker"
fi

# Provide relevant permissions
sudo chmod 666 /var/run/docker.sock
sudo usermod -a -G docker jenkins

# Check Docker version
echo "Docker $(docker --version)"
23 changes: 23 additions & 0 deletions scripts/plugins.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ant:latest
antisamy-markup-formatter:latest
authorize-project:latest
build-timeout:latest
cloudbees-folder:latest
configuration-as-code:latest
credentials-binding:latest
email-ext:latest
git:latest
github-branch-source:latest
gradle:latest
ldap:latest
mailer:latest
matrix-auth:latest
pam-auth:latest
pipeline-github-lib:latest
pipeline-stage-view:latest
ssh-slaves:latest
timestamper:latest
workflow-aggregator:latest
ws-cleanup:latest
job-dsl:latest
configuration-as-code-groovy:latest
13 changes: 12 additions & 1 deletion ubuntu.ami.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ source "amazon-ebs" "ubuntu" {
build {
sources = ["source.amazon-ebs.ubuntu"]

# https://www.packer.io/docs/provisioners/file#uploading-files-that-don-t-exist-before-packer-starts
provisioners "file" {
source = "./scripts/plugins.txt"
destination = "/home/ubuntu/plugins.txt"
}

provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
Expand All @@ -135,8 +141,13 @@ build {
]
}

provisioners "file" {
source = "./scripts/casc.yaml"
destination = "/var/lib/jenkins/casc.yaml"
}

post-processor "manifest" {
output = "manifest.json"
strip_path = true
}
}
}

0 comments on commit 3e107ee

Please sign in to comment.