Skip to content

.github/workflows/aws-vm-packer-build.yml #19

.github/workflows/aws-vm-packer-build.yml

.github/workflows/aws-vm-packer-build.yml #19

on: workflow_dispatch
env:
AWS_REGION: "eu-central-1"
# Permission can be added at job level or workflow level
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
packer-build:
runs-on: ubuntu-latest
name: Run Packer
outputs:
ami_id: ${{ steps.build.outputs.ami_id }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup `packer`
uses: hashicorp/setup-packer@main
id: setup
with:
version: "latest"
- name: Run `packer init`
id: init
working-directory: ./aws/packer
run: "packer init ./aws-ubuntu.pkr.hcl"
- name: Run `packer validate`
id: validate
working-directory: ./aws/packer
run: "packer validate ./aws-ubuntu.pkr.hcl"
- name: configure aws credentials
uses: aws-actions/[email protected]
with:
role-to-assume: arn:aws:iam::853805194132:role/GitHubAction-Gitstafette #change to reflect your IAM role’s ARN
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ env.AWS_REGION }}
# this is a massive file, 1M+ lines
#- name: test aws credentials
# run: aws ec2 describe-images --region eu-central-1
- name: Run `packer build`
id: build
working-directory: ./aws/packer
run: |
packer build ./aws-ubuntu.pkr.hcl
cat manifest.json | jq -r '.builds[-1].artifact_id' | cut -d':' -f2
ami_id=$(cat manifest.json | jq -r '.builds[-1].artifact_id' | cut -d':' -f2)
ami_id="ami-09652ef7f1166e7d3"
echo "ami_id=${ami_id}" | tee -a $GITHUB_OUTPUT
terraform-build:
runs-on: ubuntu-latest
name: Gitstafette AWS VM Rebuild
needs: packer-build
steps:
- id: tf-checkout
name: Checkout code for TF
uses: actions/checkout@v4
- id: tf-aws-creds
name: Configure AWS credentials for Terraform
uses: aws-actions/[email protected]
with:
role-to-assume: arn:aws:iam::853805194132:role/GitHubAction-Gitstafette #change to reflect your IAM role’s ARN
role-session-name: GitHub_to_AWS_via_FederatedOIDC
aws-region: ${{ env.AWS_REGION }}
- id: tf-setup
name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- id: tf-init
name: Terraform Init
working-directory: ./aws/terraform
run: terraform init
- id: tf-validate
name: Terraform Validate
working-directory: ./aws/terraform
run: terraform validate -no-color
- id: tf-plan
name: Terraform Plan
continue-on-error: true # because some exit codes are not 0, even if they are just warnings/informative
env:
AMI_ID: ${{ needs.packer-build.outputs.ami_id }}
working-directory: ./aws/terraform
run: |
echo "AMI_ID=${AMI_ID}"
export TF_VAR_ami_id=${AMI_ID}
terraform plan \
-var "ami_id=${AMI_ID}" \
-no-color -out plan.out \
-input=false
# https://github.com/camillehe1992/demo-for-aws-deployment-via-oidc/blob/main/.github/workflows/deploy.yaml
# Apply the pending changes
- name: Terraform apply
working-directory: ./aws/terraform
run: |
terraform apply -auto-approve -input=false "plan.out"