Update build-ami.yml #32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build AMI with Packer and Ansible | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-ami: | |
runs-on: ubuntu-latest | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
POSTGRES_USER: ${{ secrets.POSTGRES_USER }} | |
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} | |
POSTGRES_DB: ${{ secrets.POSTGRES_DB }} | |
steps: | |
- name: Checkout main branch | |
uses: actions/checkout@v2 | |
with: | |
ref: main | |
- name: Checkout packer_ansible branch | |
uses: actions/checkout@v2 | |
with: | |
ref: packer_ansible | |
path: packer_ansible | |
- name: Debug directory contents | |
run: | | |
ls -lR packer_ansible | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.11' | |
- name: Install Ansible | |
run: | | |
sudo apt update | |
sudo apt install -y ansible | |
pip install psycopg2 | |
- name: Install Latest Packer and Initialize | |
run: | | |
wget -O packer.zip https://releases.hashicorp.com/packer/1.11.1/packer_1.11.1_linux_amd64.zip | |
unzip -o packer.zip -d packer_install | |
sudo mv packer_install/packer /usr/local/bin/ | |
rm -rf packer.zip packer_install # Clean up extracted files and zip | |
packer --version # Verify Packer installation | |
packer init packer_ansible/packer/PosgresawsTemplate.pkr.hcl | |
- name: Debug directory contents | |
run: | | |
pwd | |
ls -l packer_ansible/packer | |
ls -l packer_ansible/ansible | |
- name: Validate Packer Template | |
run: packer validate packer_ansible/packer/PosgresawsTemplate.pkr.hcl | |
- name: Run Packer build and print AMI ID | |
run: | | |
packer build -machine-readable -var aws_access_key=${{ secrets.AWS_ACCESS_KEY_ID }} -var aws_secret_key=${{ secrets.AWS_SECRET_ACCESS_KEY }} /home/runner/work/Team1-react-rust-postgres/Team1-react-rust-postgres/packer_ansible/packer/PosgresawsTemplate.pkr.hcl | tee packer_output.txt | |
ami_id=$(awk -F, '$0 ~/artifact,0,id/ {print $6}' packer_output.txt) | |
echo "AMI_ID=$ami_id" >> $GITHUB_ENV | |
echo "AMI_ID=$ami_id" # Print AMI ID to console | |
- name: Save AMI_ID as artifact | |
uses: actions/upload-artifact@v2 | |
with: | |
name: packer-output | |
path: packer_output.txt | |
- name: Checkout main branch | |
run: git checkout main |