Skip to content

Commit

Permalink
Merge pull request #1 from yashwanth170/main
Browse files Browse the repository at this point in the history
Packer Configuration for Building Jenkins Server AMI
  • Loading branch information
yashwanth170 authored May 30, 2024
2 parents 3396f57 + e22fc63 commit 14e7db0
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
51 changes: 51 additions & 0 deletions .github/workflows/packer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Build Jenkins AMI

on:
push:
branches:
- main # Change this to the branch you want to trigger the workflow

jobs:
packer:
name: Build AMI with Packer
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Packer
uses: hashicorp/setup-packer@v2
with:
packer_version: latest

- name: Install amazon plugin for packer
run: |
packer plugins install github.com/hashicorp/amazon
- name: Install AWS CLI
run: |
sudo apt-get update
sudo apt-get install -y awscli
- name: Configure AWS Credentials
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
run: |
mkdir -p ~/.aws
echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.aws/credentials
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials
- name: Run Packer Build
env:
AWS_PROFILE: default
run: |
packer build -var "region=${{ secrets.AWS_DEFAULT_REGION }}" \
-var "source_ami=ami-04b70fa74e45c3917" \
-var "instance_type=t2.micro" \
-var "profile=default" \
-var "ssh_username=ubuntu" \
jenkins-config.json
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
# ami-jenkins
# Packer Configuration for Building Jenkins Server AMI

This repository contains a Packer configuration file (`jenkins-config.json`) and a shell script (`script.sh`) to build an Amazon Machine Image (AMI) for a Jenkins server on AWS using Packer.

## Prerequisites

Before you begin, ensure you have the following prerequisites installed:

- [Packer](https://www.packer.io/downloads)
- AWS IAM credentials with permissions to create EC2 instances and AMIs

## Usage

### 1. Configure Variables

Open `jenkins-config.json` in a text editor and customize the following variables as needed:

- `region`: The AWS region where the AMI will be created.
- `source_ami`: The ID of the base AMI to use as the source.
- `instance_type`: The EC2 instance type to use for building the AMI.
- `profile`: The AWS profile to use for authentication (optional).
- `ssh_username`: The username used to SSH into the EC2 instance.

### 2. Build the AMI

Run the following command to build the AMI:

```bash
packer build jenkins-json.json

The script file (`script.sh`) installs the following software on the Amazon Machine Image (AMI) during the provisioning process:

- **curl**: Command-line tool for transferring data with URLs.
- **OpenJDK 17**: Java Runtime Environment required for Jenkins.
- **Jenkins**: Automation server for continuous integration and continuous delivery (CI/CD).
- **Nginx**: Web server used for reverse proxying to Jenkins and handling SSL.
- **Certbot**: Tool for automatically enabling HTTPS on your server using Let's Encrypt certificates.
29 changes: 29 additions & 0 deletions jenkins-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"variables": {
"region": "us-east-1",
"source_ami": "ami-04b70fa74e45c3917",
"instance_type": "t2.micro",
"profile": "default",
"ssh_username": "ubuntu"
},
"builders": [
{
"type": "amazon-ebs",
"region": "{{user `region`}}",
"source_ami": "{{user `source_ami`}}",
"profile": "{{user `profile`}}",
"instance_type": "{{user `instance_type`}}",
"ssh_username": "{{user `ssh_username`}}",
"ami_name": "jenkins-{{timestamp}}",
"tags":{
"Name": "Jenkins - {{timestamp}}"
}
}
],
"provisioners": [
{
"type": "shell",
"script": "script.sh"
}
]
}
14 changes: 14 additions & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get install -y curl
sudo apt install -y openjdk-17-jre
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
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
sudo apt-get update
sudo apt-get install -y jenkins
sudo apt-get install -y nginx
sudo apt-get install -y nginx certbot python3-certbot-nginx
sudo systemctl enable jenkins
sudo systemctl enable nginx

0 comments on commit 14e7db0

Please sign in to comment.