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: projeto automatizado completo etapa 3 cicd deploy aplicacao | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Fazer checkout do repositório | |
uses: actions/checkout@v2 | |
- name: Configurar credenciais AWS | |
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: us-east-1 | |
- name: Configurar Terraform | |
uses: hashicorp/setup-terraform@v1 | |
with: | |
terraform_version: 1.0.0 | |
- name: Inicializar Terraform | |
run: terraform init | |
working-directory: Terraform_IaC | |
- name: Aplicar Terraform | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
run: terraform apply -auto-approve -var "aws_access_key=${{ secrets.AWS_ACCESS_KEY_ID }}" -var "aws_secret_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" | |
working-directory: Terraform_IaC | |
- name: Obter IDs e IPs Públicos das Instâncias | |
id: get_instance_info | |
run: | | |
instance_ids=$(terraform output -json instance_ids | jq -r '.value[]') | |
instance_public_ips=$(terraform output -json instance_public_ips | jq -r '.value[]') | |
private_key=$(terraform output -raw private_key) | |
echo "instance_ids=$instance_ids" >> $GITHUB_ENV | |
echo "instance_public_ips=$instance_public_ips" >> $GITHUB_ENV | |
echo "$private_key" > /home/runner/.ssh/id_rsa | |
chmod 600 /home/runner/.ssh/id_rsa | |
working-directory: Terraform_IaC | |
- name: Instalar Docker nas Instâncias | |
run: | | |
for ip in ${{ env.instance_public_ips }}; do | |
ssh -o "StrictHostKeyChecking=no" -i /home/runner/.ssh/id_rsa ubuntu@$ip "sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common && curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository 'deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable' && sudo apt-get update && sudo apt-get install -y docker-ce && sudo usermod -aG docker ubuntu && sudo systemctl enable docker && sudo systemctl start docker && docker --version && echo Docker instalado em $ip || echo Docker não instalado em $ip" | |
done | |
# - name: Destruir Terraform | |
# env: | |
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
# run: terraform destroy -auto-approve -var "aws_access_key=${{ secrets.AWS_ACCESS_KEY_ID }}" -var "aws_secret_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" | |
# working-directory: Terraform_IaC |