Skip to content

Commit

Permalink
feat: add nodejs installation tool config with JCasC
Browse files Browse the repository at this point in the history
Fixes issue #36
  • Loading branch information
sydrawat01 authored and karanwadhwa committed Oct 26, 2023
1 parent 91d5217 commit 2d9d0ca
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
55 changes: 52 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ packer build --var-file=<variables-file>.pkrvars.hcl <build-config>.pkr.hcl

> NOTE: It is considered best practice to build a custom AMI with variables using HCP Packer!
## ⤵️ Install Java & Jenkins in AMI
## ⤵️ Install required software

In order for Jenkins to run it requires `Java`

Expand All @@ -134,7 +134,7 @@ sudo apt update --quiet
java -version
```

### 💁‍♂️ Jenkins installation
### 💁‍♂️ Jenkins Installation

```bash
# Installing Jenkins
Expand All @@ -157,6 +157,54 @@ sudo apt install jenkins -y
sudo systemctl status jenkins
```

### 📦 NodeJS Installation

```bash
# https://github.com/nodesource/distributions#installation-instructions
# Download and import the Nodesource GPG key
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo \
gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

# Create a deb repository
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo \
tee /etc/apt/sources.list.d/nodesource.list

# Run update and install
sudo apt-get update && sudo apt-get install nodejs -y

# Check Node version:
echo "Node $(node --version)"
```

### 🐳 Docker Installation

```bash
# Add Docker's official GPG key:
sudo apt-get 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-get update && sudo apt-get install docker-ce -y

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

# Check Docker version
echo "Docker $(docker --version)"
```

## 🔒 Configure Caddy Service

```bash
Expand Down Expand Up @@ -185,6 +233,7 @@ We will configure the Jenkins server using the `install.sh` script that configur
There are a couple of plugins that will help us setup the Jenkins server with `Jenkins Configuration as Code`:

- `job-dsl`: Configure seed jobs to setup multi-branch pipelines
- `configuration-as-code`: Configure Jenkins with a JCasC `yaml` file that installs required tools and creates users
- `configuration-as-code-groovy`: Configure Jenkins with a JCasC `yaml` file that runs the `Groovy` scripts defined in the seed jobs

In order to install the plugins on the EC2 instance, we need to download and run the `plugin-installation-manager-tool` from [GitHub](https://github.com/jenkinsci/plugin-installation-manager-tool/).
Expand Down Expand Up @@ -256,7 +305,7 @@ We have to update the user and group permissions for the JCasC and groovy files:
```bash
# Update file ownership
cd /var/lib/jenkins/ref/ || exit
sudo chown jenkins:jenkins jcasc.yaml webapp_seed.groovy webapp_db_seed.groovy
sudo chown jenkins:jenkins <your-jcasc>.yaml <your_seed_job>.groovy
```

### ⏫ Update jenkins service
Expand Down
18 changes: 18 additions & 0 deletions jenkins/jcasc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ jobs:
- file: ./webapp_seed.groovy
- file: ./webapp_db_seed.groovy
- file: ./webapp_helm_chart_seed.groovy
# https://github.com/jenkinsci/configuration-as-code-plugin/issues/701
tool:
#This tool can't be installed against alpine OS
nodejs:
defaultProperties:
- installSource:
installers:
- nodeJSInstaller:
npmPackagesRefreshHours: 72
installations:
- name: "node"
home: ""
properties:
- installSource:
installers:
- nodeJSInstaller:
id: "20.9.0"
npmPackagesRefreshHours: 72
unclassified:
location:
url: https://jenkins.sydrawat.me
1 change: 1 addition & 0 deletions jenkins/plugins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ antisamy-markup-formatter:latest
build-timeout:latest
cloudbees-folder:latest
command-launcher:latest
configuration-as-code:latest
configuration-as-code-groovy:latest
credentials:latest
credentials-binding:latest
Expand Down
30 changes: 28 additions & 2 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ echo "+-------------------------------------------------------------------------
# Update packages and dependencies
sudo apt-get update --quiet && sudo apt-get upgrade -y

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| INSTALL NODEJS |"
echo "| |"
echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"

# https://github.com/nodesource/distributions#installation-instructions
# Download and import the Nodesource GPG key
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo \
gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

# Create a deb repository
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo \
tee /etc/apt/sources.list.d/nodesource.list

# Run update and install
sudo apt-get update && sudo apt-get install nodejs -y

sleep 3

# Check Node version:
echo "Node $(node --version)"

echo "+-----------------------------------------------------------------------------------------------------------------------------------------+"
echo "| |"
echo "| INSTALL JAVA 11 |"
Expand Down Expand Up @@ -124,11 +150,11 @@ sudo chown jenkins:jenkins ./*

# Move Jenkins files to Jenkins home
cd /home/ubuntu/ || exit
sudo mv jcasc.yaml webapp_seed.groovy webapp_db_seed.groovy /var/lib/jenkins/
sudo mv jcasc.yaml webapp_seed.groovy webapp_db_seed.groovy webapp_helm_chart_seed.groovy /var/lib/jenkins/

# Update file ownership
cd /var/lib/jenkins/ || exit
sudo chown jenkins:jenkins jcasc.yaml webapp_seed.groovy webapp_db_seed.groovy
sudo chown jenkins:jenkins jcasc.yaml webapp_seed.groovy webapp_db_seed.groovy webapp_helm_chart_seed.groovy

# Configure JAVA_OPTS to disable setup wizard
sudo mkdir -p /etc/systemd/system/jenkins.service.d/
Expand Down
5 changes: 5 additions & 0 deletions ubuntu.ami.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ build {
destination = "/home/ubuntu/webapp_db_seed.groovy"
}

provisioner "file" {
source = "./scripts/webapp_helm_chart_seed.groovy"
destination = "/home/ubuntu/webapp_helm_chart_seed.groovy"
}

provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
Expand Down

0 comments on commit 2d9d0ca

Please sign in to comment.