From 8eb7948609cc5b3f8cf02181bad231da0136936d Mon Sep 17 00:00:00 2001 From: furiousme Date: Mon, 30 Sep 2024 22:01:29 +0300 Subject: [PATCH] fix: separate jobs --- .github/workflows/terraform.yml | 65 ++++++++++++++++++++++++++++++--- buckets.tf | 6 +-- providers.tf | 7 +--- variables.tf | 4 ++ 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index e2a519e..b4b2de4 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -11,36 +11,89 @@ on: pull_request: branches: - main + jobs: terraform-check: runs-on: ubuntu-latest env: - TF_VAR_account_id: ${{ secrets.AWS_ACCOUNT_ID }} - TF_VAR_gh_username: ${{ secrets.TF_VAR_gh_username }} - TF_VAR_user_profile_name: ${{ secrets.TF_VAR_user_profile_name }} + TF_VAR_account_id: ${{ secrets.AWS_ACCOUNT_ID }} + TF_VAR_gh_username: ${{ secrets.TF_VAR_GH_USERNAME }} + TF_VAR_user_profile_name: ${{ secrets.TF_VAR_USER_PROFILE_NAME }} + TF_VAR_pipeline: ${{ secrets.TF_VAR_PIPELINE }} steps: - name: Checkout code uses: actions/checkout@v4 - name: Setup Terraform - uses: hashicorp/setup-terraform@v2 + uses: hashicorp/setup-terraform@v3 with: terraform_version: 1.9.6 - - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/GithubActionsRole aws-region: us-east-1 role-duration-seconds: 1200 + - name: Terraform Init + run: terraform init + + - name: Terraform Format Check + run: terraform fmt -check + terraform-plan: + runs-on: ubuntu-latest + needs: terraform-check + env: + TF_VAR_account_id: ${{ secrets.AWS_ACCOUNT_ID }} + TF_VAR_gh_username: ${{ secrets.TF_VAR_GH_USERNAME }} + TF_VAR_user_profile_name: ${{ secrets.TF_VAR_USER_PROFILE_NAME }} + TF_VAR_pipeline: ${{ secrets.TF_VAR_PIPELINE }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Terraform + uses: hashicorp/setup-terraform@v3 + with: + terraform_version: 1.9.6 + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/GithubActionsRole + aws-region: us-east-1 + role-duration-seconds: 1200 - name: Terraform Init run: terraform init - name: Terraform Plan run: terraform plan + terraform-apply: + runs-on: ubuntu-latest + needs: terraform-plan + env: + TF_VAR_account_id: ${{ secrets.AWS_ACCOUNT_ID }} + TF_VAR_gh_username: ${{ secrets.TF_VAR_GH_USERNAME }} + TF_VAR_user_profile_name: ${{ secrets.TF_VAR_USER_PROFILE_NAME }} + TF_VAR_pipeline: ${{ secrets.TF_VAR_PIPELINE }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v3 + with: + terraform_version: 1.9.6 + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/GithubActionsRole + role-duration-seconds: 1200 + aws-region: us-east-1 + + - name: Terraform Init + run: terraform init - name: Terraform Apply run: terraform apply -auto-approve - diff --git a/buckets.tf b/buckets.tf index 3bc0c0b..e05ba1f 100644 --- a/buckets.tf +++ b/buckets.tf @@ -1,9 +1,9 @@ -resource "aws_s3_bucket" "rsschool_course_app_main_bucket" { - bucket = "rsschool_course_app_main_bucket" +resource "aws_s3_bucket" "rsschool_course_app_bucket" { + bucket = "rsschool-course-app-bucket" } resource "aws_s3_bucket_versioning" "rsschool_course_app_main_bucket_versioning" { - bucket = aws_s3_bucket.rsschool_course_app_main_bucket.id + bucket = aws_s3_bucket.rsschool_course_app_bucket.id versioning_configuration { status = "Enabled" } diff --git a/providers.tf b/providers.tf index e57ba54..0c1a39c 100644 --- a/providers.tf +++ b/providers.tf @@ -14,10 +14,7 @@ terraform { } } - - provider "aws" { - region = "us-east-1" - shared_credentials_files = ["~/.aws/credentials"] - profile = var.user_profile_name + region = "us-east-1" + profile = var.pipeline ? "" : var.user_profile_name } \ No newline at end of file diff --git a/variables.tf b/variables.tf index c579c6d..ebaf195 100644 --- a/variables.tf +++ b/variables.tf @@ -10,4 +10,8 @@ variable "gh_username" { variable "user_profile_name" { type = string sensitive = true +} + +variable "pipeline" { + type = bool } \ No newline at end of file