|
| 1 | +name: Data replication pipeline |
| 2 | +run-name: ${{ inputs.action }} data replication resources for ${{ inputs.environment }} |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + environment: |
| 8 | + description: Deployment environment |
| 9 | + required: true |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - training |
| 13 | + - production |
| 14 | + - test |
| 15 | + - qa |
| 16 | + - sandbox-alpha |
| 17 | + - sandbox-beta |
| 18 | + image_tag: |
| 19 | + description: Docker image tag to deploy |
| 20 | + required: false |
| 21 | + type: string |
| 22 | + action: |
| 23 | + description: Action to perform on data replication env |
| 24 | + required: true |
| 25 | + type: choice |
| 26 | + options: |
| 27 | + - destroy |
| 28 | + - recreate |
| 29 | + default: recreate |
| 30 | + |
| 31 | +env: |
| 32 | + aws_role: ${{ inputs.environment == 'production' |
| 33 | + && 'arn:aws:iam::820242920762:role/GithubDeployDataReplicationInfrastructure' |
| 34 | + || 'arn:aws:iam::393416225559:role/GithubDeployDataReplicationInfrastructure' }} |
| 35 | + |
| 36 | +defaults: |
| 37 | + run: |
| 38 | + working-directory: terraform/data_replication |
| 39 | + |
| 40 | +concurrency: |
| 41 | + group: deploy-data-replica-${{ inputs.environment }} |
| 42 | + |
| 43 | +jobs: |
| 44 | + prepare: |
| 45 | + name: Prepare data replica |
| 46 | + runs-on: ubuntu-latest |
| 47 | + permissions: |
| 48 | + id-token: write |
| 49 | + steps: |
| 50 | + - name: Checkout code |
| 51 | + uses: actions/checkout@v4 |
| 52 | + - name: Configure AWS Credentials |
| 53 | + uses: aws-actions/configure-aws-credentials@v4 |
| 54 | + with: |
| 55 | + role-to-assume: ${{ env.aws_role }} |
| 56 | + aws-region: eu-west-2 |
| 57 | + - name: get latest snapshot |
| 58 | + id: get-latest-snapshot |
| 59 | + run: | |
| 60 | + set -e |
| 61 | + SNAPSHOT_ARN=$(aws rds describe-db-cluster-snapshots \ |
| 62 | + --query 'DBClusterSnapshots[?contains(DBClusterSnapshotIdentifier, `${{ inputs.environment}}`)].[DBClusterSnapshotArn, SnapshotCreateTime]' \ |
| 63 | + --output text | sort -k2 -r | head -n 1 | cut -f1) |
| 64 | + echo "SNAPSHOT_ARN=$SNAPSHOT_ARN" >> $GITHUB_OUTPUT |
| 65 | + - name: Install terraform |
| 66 | + uses: hashicorp/setup-terraform@v3 |
| 67 | + with: |
| 68 | + terraform_version: 1.10.5 |
| 69 | + - name: Get db secret arn |
| 70 | + id: get-db-secret-arn |
| 71 | + working-directory: terraform/app |
| 72 | + run: | |
| 73 | + terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade |
| 74 | + DB_SECRET_ARN=$(terraform output --raw db_secret_arn) |
| 75 | + echo "DB_SECRET_ARN=$DB_SECRET_ARN" >> $GITHUB_OUTPUT |
| 76 | + - name: ECR login |
| 77 | + id: login-ecr |
| 78 | + uses: aws-actions/amazon-ecr-login@v2 |
| 79 | + - name: Get docker image digest |
| 80 | + id: get-docker-image-digest |
| 81 | + run: | |
| 82 | + set -e |
| 83 | + DOCKER_IMAGE="${{ steps.login-ecr.outputs.registry }}/mavis/webapp:${{ inputs.image_tag || github.sha }}" |
| 84 | + docker pull "$DOCKER_IMAGE" |
| 85 | + DOCKER_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$DOCKER_IMAGE") |
| 86 | + DIGEST="${DOCKER_DIGEST#*@}" |
| 87 | + echo "DIGEST=$DIGEST" >> $GITHUB_OUTPUT |
| 88 | + outputs: |
| 89 | + SNAPSHOT_ARN: ${{ steps.get-latest-snapshot.outputs.SNAPSHOT_ARN }} |
| 90 | + DB_SECRET_ARN: ${{ steps.get-db-secret-arn.outputs.DB_SECRET_ARN }} |
| 91 | + DOCKER_DIGEST: ${{ steps.get-docker-image-digest.outputs.DIGEST }} |
| 92 | + |
| 93 | + destroy: |
| 94 | + name: Destroy data replication infrastructure |
| 95 | + runs-on: ubuntu-latest |
| 96 | + environment: ${{ inputs.environment }} |
| 97 | + permissions: |
| 98 | + id-token: write |
| 99 | + steps: |
| 100 | + - name: Checkout code |
| 101 | + uses: actions/checkout@v4 |
| 102 | + - name: Configure AWS Credentials |
| 103 | + uses: aws-actions/configure-aws-credentials@v4 |
| 104 | + with: |
| 105 | + role-to-assume: ${{ env.aws_role }} |
| 106 | + aws-region: eu-west-2 |
| 107 | + - name: Install terraform |
| 108 | + uses: hashicorp/setup-terraform@v3 |
| 109 | + with: |
| 110 | + terraform_version: 1.10.5 |
| 111 | + - name: Terraform Destroy |
| 112 | + id: destroy |
| 113 | + run: | |
| 114 | + set -e |
| 115 | + terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade |
| 116 | + terraform destroy -var-file="env/${{ inputs.environment }}.tfvars" -var="image_digest=filler_value" \ |
| 117 | + -var="db_secret_arn=filler_value" -var="imported_snapshot=filler_value" -auto-approve |
| 118 | +
|
| 119 | + plan: |
| 120 | + if: ${{ inputs.action == 'recreate' }} |
| 121 | + name: Terraform plan |
| 122 | + runs-on: ubuntu-latest |
| 123 | + needs: |
| 124 | + - prepare |
| 125 | + - destroy |
| 126 | + env: |
| 127 | + SNAPSHOT_ARN: ${{ needs.prepare.outputs.SNAPSHOT_ARN }} |
| 128 | + DB_SECRET_ARN: ${{ needs.prepare.outputs.DB_SECRET_ARN }} |
| 129 | + DOCKER_DIGEST: ${{ needs.prepare.outputs.DOCKER_DIGEST }} |
| 130 | + permissions: |
| 131 | + id-token: write |
| 132 | + steps: |
| 133 | + - name: Checkout code |
| 134 | + uses: actions/checkout@v4 |
| 135 | + - name: Configure AWS Credentials |
| 136 | + uses: aws-actions/configure-aws-credentials@v4 |
| 137 | + with: |
| 138 | + role-to-assume: ${{ env.aws_role }} |
| 139 | + aws-region: eu-west-2 |
| 140 | + - name: Install terraform |
| 141 | + uses: hashicorp/setup-terraform@v3 |
| 142 | + with: |
| 143 | + terraform_version: 1.10.5 |
| 144 | + - name: Terraform Plan |
| 145 | + id: plan |
| 146 | + run: | |
| 147 | + set -e |
| 148 | + terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade |
| 149 | + terraform plan -var="image_digest=${{ env.DOCKER_DIGEST }}" -var="db_secret_arn=${{ env.DB_SECRET_ARN }}" \ |
| 150 | + -var="imported_snapshot=${{ env.SNAPSHOT_ARN }}" -var-file="env/${{ inputs.environment }}.tfvars" \ |
| 151 | + -out ${{ runner.temp }}/tfplan | tee ${{ runner.temp }}/tf_stdout |
| 152 | + - name: Upload artifact |
| 153 | + uses: actions/upload-artifact@v4 |
| 154 | + with: |
| 155 | + name: tfplan_infrastructure-${{ inputs.environment }} |
| 156 | + path: ${{ runner.temp }}/tfplan |
| 157 | + |
| 158 | + apply: |
| 159 | + name: Terraform apply |
| 160 | + runs-on: ubuntu-latest |
| 161 | + needs: plan |
| 162 | + environment: ${{ inputs.environment }} |
| 163 | + permissions: |
| 164 | + id-token: write |
| 165 | + steps: |
| 166 | + - name: Checkout code |
| 167 | + uses: actions/checkout@v4 |
| 168 | + - name: Configure AWS Credentials |
| 169 | + uses: aws-actions/configure-aws-credentials@v4 |
| 170 | + with: |
| 171 | + role-to-assume: ${{ env.aws_role }} |
| 172 | + aws-region: eu-west-2 |
| 173 | + - name: Download artifact |
| 174 | + uses: actions/download-artifact@v4 |
| 175 | + with: |
| 176 | + name: tfplan_infrastructure-${{ inputs.environment }} |
| 177 | + path: ${{ runner.temp }} |
| 178 | + - name: Install terraform |
| 179 | + uses: hashicorp/setup-terraform@v3 |
| 180 | + with: |
| 181 | + terraform_version: 1.10.5 |
| 182 | + - name: Apply the changes |
| 183 | + run: | |
| 184 | + set -e |
| 185 | + terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade |
| 186 | + terraform apply ${{ runner.temp }}/tfplan |
0 commit comments