Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/actions/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM ubuntu:20.04

# Set the frontend to avoid prompts
ENV DEBIAN_FRONTEND=noninteractive

# Install Terraform, Packer, and Ansible
RUN apt-get update && \
apt-get install -y curl unzip git bash ansible gnupg && \
curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com focal main" > /etc/apt/sources.list.d/hashicorp.list && \
apt-get update && \
apt-get install -y terraform packer xorriso
#RUN mkdir packer ansible terraform vagrant
# Copy the entrypoint script into the container
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

#EXPOSE 8826 if using http_directory

# Set the entrypoint of the Docker container to be the entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
18 changes: 18 additions & 0 deletions .github/actions/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Build Infrastructure"
description: "Build VM image using Packer with vSphere"

inputs:
vcenter_user:
required: true
vcenter_password:
required: true
vcenter_server:
required: true

runs:
using: "docker"
image: "Dockerfile"
env:
VCENTER_USER: ${{ inputs.vcenter_user }}
VCENTER_PASSWORD: ${{ inputs.vcenter_password }}
VCENTER_SERVER: ${{ inputs.vcenter_server }}
48 changes: 48 additions & 0 deletions .github/actions/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash
set -euo pipefail

# Print info
echo "[INFO] Starting Packer build..."

# Required env check
: "${VCENTER_USER:?VCENTER_USER not set}"
: "${VCENTER_PASSWORD:?VCENTER_PASSWORD not set}"
: "${VCENTER_SERVER:?VCENTER_SERVER not set}"

# Export as Packer vars
export PACKER_VAR_vcenter_user="$VCENTER_USER"
export PACKER_VAR_vcenter_password="$VCENTER_PASSWORD"
export PACKER_VAR_vcenter_server="$VCENTER_SERVER"

# Optional debug
echo "[INFO] Using vCenter: $VCENTER_SERVER"

packer plugins install github.com/hashicorp/vsphere

# Move into packer directory if not already
cd "${PACKER_DIR:-./packer}"
pwd

ls -al ./
ls -al /root/ansible-optimize/packer
ls -al ../

# Validate template
packer fmt -check -diff .
packer validate centos9.json

echo "validated no error"

# Build image
packer build -force centos9.json

echo "[SUCCESS] Packer build complete."

# Now, let's run Terraform to provision the VM
echo "[INFO] Starting Terraform provisioning..."

# Run Terraform init and apply
terraform init
terraform apply -auto-approve

echo "[SUCCESS] Terraform apply complete."
47 changes: 47 additions & 0 deletions .github/workflows/build_inf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build Infrastructure


on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
packer-build:
#runs-on: ubuntu-latest # Can be changed based on your needs
runs-on: self-hosted

container:
image: ghcr.io/catthehacker/ubuntu:act-latest
# volumes:
# - ansible:/ansible
# - packer:/packer
# - terraform:/terraform
# - vagrant:/vagrant

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true

- name: Set up Docker Build Environment
run: |
echo "Setting up Docker environment for Packer build"

- name: Set environment variables from GitHub Secrets
run: |
echo "VCENTER_USER=${{ secrets.VCENTER_USER }}" >> $GITHUB_ENV
echo "VCENTER_PASSWORD=${{ secrets.VCENTER_PASSWORD }}" >> $GITHUB_ENV
echo "VCENTER_SERVER=${{ secrets.VCENTER_SERVER }}" >> $GITHUB_ENV

- name: Run Packer Build
uses: ./.github/actions # Reference custom action (Docker container)
with:
vcenter_user: ${{ secrets.VCENTER_USER }}
vcenter_password: ${{ secrets.VCENTER_PASSWORD }}
vcenter_server: ${{ secrets.VCENTER_SERVER }}

48 changes: 48 additions & 0 deletions .github/workflows/terra_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Terraform CI/CD Pipeline

on:
push:
branches:
- main
workflow_run:
workflows: [Build Infrastructure]
types: [completed]

jobs:

terraform:
#runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: self-hosted

container:
image: ghcr.io/catthehacker/ubuntu:act-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set environment variables from GitHub Secrets
run: |
echo "VCENTER_USER=${{ secrets.VCENTER_USER }}" >> $GITHUB_ENV
echo "VCENTER_PASSWORD=${{ secrets.VCENTER_PASSWORD }}" >> $GITHUB_ENV
echo "VCENTER_SERVER=${{ secrets.VCENTER_SERVER }}" >> $GITHUB_ENV

- name: Set up Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 'latest'

- run: pwd

- name: Initialize Terraform
run: |
terraform init

- name: Terraform Plan
run: |
terraform plan

- name: Apply Terraform Configuration
run: |
terraform apply -auto-approve
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Local .terraform directories
**/.terraform/*

.env
# .tfstate files
*.tfstate
*.tfstate.*
Expand All @@ -15,6 +15,7 @@ crash.*.log
# to change depending on the environment.
*.tfvars
*.tfvars.json
.secrets

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
Expand Down
4 changes: 4 additions & 0 deletions ansible/gather_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- hosts: all
tasks:
- debug:
var: ansible_facts
2 changes: 2 additions & 0 deletions ansible/inventory/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[centos9]
centos9.local # The VM hostname or IP address
Loading
Loading