02 Build AMIs and Deploy #25
This file contains 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: 02 Build AMIs and Deploy | |
on: | |
# push: | |
# branches: [ "main" ] | |
workflow_dispatch: | |
env: | |
AWS_REGION: ${{ vars.AWS_REGION }} | |
TERRAFORM_PATH: "./terraform/" | |
permissions: | |
contents: read | |
jobs: | |
packer: | |
name: 'Build AMI with Packer' | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Prepare certs and config files | |
run: export PATH=/tmp/kthw-certs:$PATH; for i in $(ls ./scripts-for-certs-and-configs/); do bash ./scripts-for-certs-and-configs/$i; done; ls -la /tmp/kthw-certs | |
- uses: actions/upload-artifact@v2 | |
with: | |
name: kthw-certs | |
path: /tmp/kthw-certs/ | |
- name: Packer Init | |
run: packer init ./packer/ | |
- name: Packer Build | |
run: packer build ./packer/ | |
terraform: | |
needs: packer | |
name: 'Deploy EC2 on AWS' | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
- uses: actions/download-artifact@v2 | |
with: | |
name: kthw-certs | |
path: /tmp/kthw-certs/ | |
- name: Replace Region in Provider section of Terraform | |
run: sed -i 's/us-east-1/${{ env.AWS_REGION }}/g' $TERRAFORM_PATH/00-provider.tf | |
- name: Terraform Init | |
run: terraform -chdir=$TERRAFORM_PATH init | |
- name: Terraform Plan | |
run: terraform -chdir=$TERRAFORM_PATH plan -input=false -var "aws_region=${{ env.AWS_REGION }}" | |
- name: Terraform Apply | |
run: terraform -chdir=$TERRAFORM_PATH apply -auto-approve -input=false -var "aws_region=${{ env.AWS_REGION }}" |