File tree Expand file tree Collapse file tree 15 files changed +742
-1
lines changed Expand file tree Collapse file tree 15 files changed +742
-1
lines changed Original file line number Diff line number Diff line change 1+ FROM ubuntu:20.04
2+
3+ # Set the frontend to avoid prompts
4+ ENV DEBIAN_FRONTEND=noninteractive
5+
6+ # Install Terraform, Packer, and Ansible
7+ RUN apt-get update && \
8+ apt-get install -y curl unzip git bash ansible gnupg && \
9+ curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg && \
10+ 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 && \
11+ apt-get update && \
12+ apt-get install -y terraform packer xorriso
13+ # RUN mkdir packer ansible terraform vagrant
14+ # Copy the entrypoint script into the container
15+ COPY entrypoint.sh /entrypoint.sh
16+ RUN chmod +x /entrypoint.sh
17+
18+ # EXPOSE 8826 if using http_directory
19+
20+ # Set the entrypoint of the Docker container to be the entrypoint.sh
21+ ENTRYPOINT ["/entrypoint.sh" ]
Original file line number Diff line number Diff line change 1+ name : " Build Infrastructure"
2+ description : " Build VM image using Packer with vSphere"
3+
4+ inputs :
5+ vcenter_user :
6+ required : true
7+ vcenter_password :
8+ required : true
9+ vcenter_server :
10+ required : true
11+
12+ runs :
13+ using : " docker"
14+ image : " Dockerfile"
15+ env :
16+ VCENTER_USER : ${{ inputs.vcenter_user }}
17+ VCENTER_PASSWORD : ${{ inputs.vcenter_password }}
18+ VCENTER_SERVER : ${{ inputs.vcenter_server }}
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+ set -euo pipefail
3+
4+ # Print info
5+ echo " [INFO] Starting Packer build..."
6+
7+ # Required env check
8+ : " ${VCENTER_USER:? VCENTER_USER not set} "
9+ : " ${VCENTER_PASSWORD:? VCENTER_PASSWORD not set} "
10+ : " ${VCENTER_SERVER:? VCENTER_SERVER not set} "
11+
12+ # Export as Packer vars
13+ export PACKER_VAR_vcenter_user=" $VCENTER_USER "
14+ export PACKER_VAR_vcenter_password=" $VCENTER_PASSWORD "
15+ export PACKER_VAR_vcenter_server=" $VCENTER_SERVER "
16+
17+ # Optional debug
18+ echo " [INFO] Using vCenter: $VCENTER_SERVER "
19+
20+ packer plugins install github.com/hashicorp/vsphere
21+
22+ # Move into packer directory if not already
23+ cd " ${PACKER_DIR:- ./ packer} "
24+ pwd
25+
26+ ls -al ./
27+ ls -al /root/ansible-optimize/packer
28+ ls -al ../
29+
30+ # Validate template
31+ packer fmt -check -diff .
32+ packer validate centos9.json
33+
34+ echo " validated no error"
35+
36+ # Build image
37+ packer build -force centos9.json
38+
39+ echo " [SUCCESS] Packer build complete."
40+
41+ # Now, let's run Terraform to provision the VM
42+ echo " [INFO] Starting Terraform provisioning..."
43+
44+ # Run Terraform init and apply
45+ terraform init
46+ terraform apply -auto-approve
47+
48+ echo " [SUCCESS] Terraform apply complete."
Original file line number Diff line number Diff line change 1+ name : Build Infrastructure
2+
3+
4+ on :
5+ push :
6+ branches :
7+ - main
8+ pull_request :
9+ branches :
10+ - main
11+
12+ jobs :
13+ packer-build :
14+ # runs-on: ubuntu-latest # Can be changed based on your needs
15+ runs-on : self-hosted
16+
17+ container :
18+ image : ghcr.io/catthehacker/ubuntu:act-latest
19+ # volumes:
20+ # - ansible:/ansible
21+ # - packer:/packer
22+ # - terraform:/terraform
23+ # - vagrant:/vagrant
24+
25+ steps :
26+ - name : Checkout repository
27+ uses : actions/checkout@v4
28+ with :
29+ submodules : true
30+
31+ - name : Set up Docker Build Environment
32+ run : |
33+ echo "Setting up Docker environment for Packer build"
34+
35+ - name : Set environment variables from GitHub Secrets
36+ run : |
37+ echo "VCENTER_USER=${{ secrets.VCENTER_USER }}" >> $GITHUB_ENV
38+ echo "VCENTER_PASSWORD=${{ secrets.VCENTER_PASSWORD }}" >> $GITHUB_ENV
39+ echo "VCENTER_SERVER=${{ secrets.VCENTER_SERVER }}" >> $GITHUB_ENV
40+
41+ - name : Run Packer Build
42+ uses : ./.github/actions # Reference custom action (Docker container)
43+ with :
44+ vcenter_user : ${{ secrets.VCENTER_USER }}
45+ vcenter_password : ${{ secrets.VCENTER_PASSWORD }}
46+ vcenter_server : ${{ secrets.VCENTER_SERVER }}
47+
Original file line number Diff line number Diff line change 1+ name : Terraform CI/CD Pipeline
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ workflow_run :
8+ workflows : [Build Infrastructure]
9+ types : [completed]
10+
11+ jobs :
12+
13+ terraform :
14+ # runs-on: ubuntu-latest
15+ if : ${{ github.event.workflow_run.conclusion == 'success' }}
16+ runs-on : self-hosted
17+
18+ container :
19+ image : ghcr.io/catthehacker/ubuntu:act-latest
20+
21+ steps :
22+ - name : Checkout code
23+ uses : actions/checkout@v3
24+
25+ - name : Set environment variables from GitHub Secrets
26+ run : |
27+ echo "VCENTER_USER=${{ secrets.VCENTER_USER }}" >> $GITHUB_ENV
28+ echo "VCENTER_PASSWORD=${{ secrets.VCENTER_PASSWORD }}" >> $GITHUB_ENV
29+ echo "VCENTER_SERVER=${{ secrets.VCENTER_SERVER }}" >> $GITHUB_ENV
30+
31+ - name : Set up Terraform
32+ uses : hashicorp/setup-terraform@v2
33+ with :
34+ terraform_version : ' latest'
35+
36+ - run : pwd
37+
38+ - name : Initialize Terraform
39+ run : |
40+ terraform init
41+
42+ - name : Terraform Plan
43+ run : |
44+ terraform plan
45+
46+ - name : Apply Terraform Configuration
47+ run : |
48+ terraform apply -auto-approve
Original file line number Diff line number Diff line change 11# Local .terraform directories
22** /.terraform /*
3-
3+ .env
44# .tfstate files
55* .tfstate
66* .tfstate. *
@@ -15,6 +15,7 @@ crash.*.log
1515# to change depending on the environment.
1616* .tfvars
1717* .tfvars.json
18+ .secrets
1819
1920# Ignore override files as they are usually used to override resources locally and so
2021# are not checked in
Original file line number Diff line number Diff line change 1+ - hosts : all
2+ tasks :
3+ - debug :
4+ var : ansible_facts
Original file line number Diff line number Diff line change 1+ [centos9]
2+ centos9.local # The VM hostname or IP address
You can’t perform that action at this time.
0 commit comments