Skip to content

Commit

Permalink
Added read custom properties for terratest (#128)
Browse files Browse the repository at this point in the history
* Added read custom properties for terratest

* Added read custom properties for terratest

* Use custom properties

* Use custom properties

* Use custom properties

* Use custom properties

* Use custom properties

* Use custom properties
  • Loading branch information
goruha authored Sep 28, 2024
1 parent edd7df5 commit 6647629
Showing 1 changed file with 67 additions and 48 deletions.
115 changes: 67 additions & 48 deletions .github/workflows/shared-terraform-chatops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ jobs:
permission: run_terratest
user: ${{ github.event.comment.user.login }}

pr:
name: PR Info
context:
name: Context
needs: [access]
if: ${{ needs.access.outputs.granted == 'true' }}
runs-on: ${{ fromJSON(inputs.runs-on) }}
Expand All @@ -40,6 +40,23 @@ jobs:
with:
id: ${{ github.event.issue.number }}

- uses: actions/github-script@v7
id: properties
with:
result-encoding: string
script: |
const properites = await github.request('GET /repos/{owner}/{repo}/properties/values', {
owner: context.repo.owner,
repo: context.repo.repo,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
properites.data.forEach((element) =>
core.setOutput(element.property_name, element.value)
);
outputs:
base: ${{ fromJSON(steps.pr.outputs.json).base.sha }}
base_repo_owner: ${{ fromJSON(steps.pr.outputs.json).base.repo.owner.login }}
Expand All @@ -57,32 +74,34 @@ jobs:
merged_at: ${{ steps.pr.outputs.merged_at }}
closed_at: ${{ steps.pr.outputs.closed_at }}
labels: ${{ steps.pr.outputs.labels }}
terratest_aws_role: ${{ steps.properties.outputs.test-aws-role || 'arn:aws:iam::799847381734:role/cptest-test-ue2-sandbox-gha-iam-terratest' }}
terratest_skip_concurrency: ${{ steps.properties.outputs.test-skip-concurrency }}

ack:
if: github.event.comment.id != ''
needs: [pr]
needs: [context]
runs-on: ${{ fromJSON(inputs.runs-on) }}
steps:
- name: "Add reaction"
uses: peter-evans/create-or-update-comment@v4
with:
repository: ${{ needs.pr.outputs.base_repo_owner }}/${{ needs.pr.outputs.base_repo_name }}
repository: ${{ needs.context.outputs.base_repo_owner }}/${{ needs.context.outputs.base_repo_name }}
comment-id: ${{ github.event.comment.id }}
token: ${{ github.token }}
reactions: '+1'

pending:
needs: [pr]
needs: [context]
runs-on: ${{ fromJSON(inputs.runs-on) }}
steps:
- name: "Update GitHub Status for pending"
uses: docker://cloudposse/github-status-updater
with:
args: >-
-action update_state
-ref "${{ needs.pr.outputs.head_sha }}"
-owner "${{ needs.pr.outputs.base_repo_owner }}"
-repo "${{ needs.pr.outputs.base_repo_name }}"
-ref "${{ needs.context.outputs.head_sha }}"
-owner "${{ needs.context.outputs.base_repo_owner }}"
-repo "${{ needs.context.outputs.base_repo_name }}"
-state pending
-context "test/terratest"
-description "Tests started by @${{ github.actor }}"
Expand All @@ -94,32 +113,32 @@ jobs:
container: cloudposse/test-harness:latest
runs-on: ${{ fromJSON(inputs.runs-on) }}
environment: terratest
needs: [pr, pending]
needs: [context, pending]
env:
MAKE_INCLUDES: Makefile
AWS_REGION: us-east-2
AWS_ROLE_TO_ASSUME: arn:aws:iam::799847381734:role/cptest-test-ue2-sandbox-gha-iam-terratest
AWS_ROLE_TO_ASSUME: ${{ needs.context.outputs.terratest_aws_role }}
continue-on-error: false
strategy:
max-parallel: 10
max-parallel: ${{ needs.context.outputs.terratest_skip_concurrency == 'true' && 1 || 10 }}
fail-fast: false # Don't fail fast to avoid locking TF State
matrix:
platform: [terraform, opentofu]
steps:
- name: "Checkout code for ChatOps"
uses: actions/checkout@v4
with:
repository: ${{ needs.pr.outputs.head_repo_owner }}/${{ needs.pr.outputs.head_repo_name }}
ref: ${{ needs.pr.outputs.head_sha }}
repository: ${{ needs.context.outputs.head_repo_owner }}/${{ needs.context.outputs.head_repo_name }}
ref: ${{ needs.context.outputs.head_sha }}

- name: "Update GitHub Status for pending"
uses: docker://cloudposse/github-status-updater
with:
args: >-
-action update_state
-ref "${{ needs.pr.outputs.head_sha }}"
-owner "${{ needs.pr.outputs.base_repo_owner }}"
-repo "${{ needs.pr.outputs.base_repo_name }}"
-ref "${{ needs.context.outputs.head_sha }}"
-owner "${{ needs.context.outputs.base_repo_owner }}"
-repo "${{ needs.context.outputs.base_repo_name }}"
-state pending
-context "test/terratest/${{ matrix.platform }}"
-description "Tests started by @${{ github.actor }}"
Expand Down Expand Up @@ -167,8 +186,8 @@ jobs:
fi
env:
# Pull request target branch
BASE_REF: ${{ needs.pr.outputs.base }}
LABELS: ${{ needs.pr.outputs.labels }}
BASE_REF: ${{ needs.context.outputs.base }}
LABELS: ${{ needs.context.outputs.labels }}

- name: "Determine required opentofu version"
if: ${{ matrix.platform == 'opentofu' }}
Expand All @@ -183,8 +202,8 @@ jobs:
fi
env:
# Pull request target branch
BASE_REF: ${{ needs.pr.outputs.base }}
LABELS: ${{ needs.pr.outputs.labels }}
BASE_REF: ${{ needs.context.outputs.base }}
LABELS: ${{ needs.context.outputs.labels }}

- name: "Initialize terratest Go project"
run: |
Expand All @@ -196,26 +215,26 @@ jobs:
id: config
env:
USES_GITHUB: >-
${{ contains(needs.pr.outputs.base_repo_name, '-github-')
|| contains(needs.pr.outputs.labels, 'terraform-github-provider') }}
${{ contains(needs.context.outputs.base_repo_name, '-github-')
|| contains(needs.context.outputs.labels, 'terraform-github-provider') }}
USES_OPSGENIE: >-
${{ contains(needs.pr.outputs.base_repo_name, 'terraform-opsgenie-')
|| contains(needs.pr.outputs.labels, 'terraform-opsgenie-provider') }}
${{ contains(needs.context.outputs.base_repo_name, 'terraform-opsgenie-')
|| contains(needs.context.outputs.labels, 'terraform-opsgenie-provider') }}
USES_AWS: >-
${{ contains(needs.pr.outputs.base_repo_name, 'terraform-aws-')
|| contains(needs.pr.outputs.labels, 'terraform-aws-provider') }}
${{ contains(needs.context.outputs.base_repo_name, 'terraform-aws-')
|| contains(needs.context.outputs.labels, 'terraform-aws-provider') }}
USES_SPOTINST: >-
${{ contains(needs.pr.outputs.base_repo_name, '-spotinst-')
|| contains(needs.pr.outputs.labels, 'terraform-spotinst-provider') }}
${{ contains(needs.context.outputs.base_repo_name, '-spotinst-')
|| contains(needs.context.outputs.labels, 'terraform-spotinst-provider') }}
USES_DATADOG: >-
${{ contains(needs.pr.outputs.base_repo_name, '-datadog-')
|| contains(needs.pr.outputs.labels, 'terraform-datadog-provider') }}
${{ contains(needs.context.outputs.base_repo_name, '-datadog-')
|| contains(needs.context.outputs.labels, 'terraform-datadog-provider') }}
USES_TFE: >-
${{ contains(needs.pr.outputs.base_repo_name, '-tfe-')
|| contains(needs.pr.outputs.labels, 'terraform-tfe-provider') }}
${{ contains(needs.context.outputs.base_repo_name, '-tfe-')
|| contains(needs.context.outputs.labels, 'terraform-tfe-provider') }}
USES_CLOUDFLARE: >-
${{ contains(needs.pr.outputs.base_repo_name, '-cloudflare-')
|| contains(needs.pr.outputs.labels, 'terraform-cloudflare-provider') }}
${{ contains(needs.context.outputs.base_repo_name, '-cloudflare-')
|| contains(needs.context.outputs.labels, 'terraform-cloudflare-provider') }}
run: |-
echo "uses_github=${USES_GITHUB}" >> $GITHUB_OUTPUT
echo "uses_opsgenie=${USES_OPSGENIE}" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -295,9 +314,9 @@ jobs:
with:
args: >-
-action update_state
-ref "${{ needs.pr.outputs.head_sha }}"
-owner "${{ needs.pr.outputs.base_repo_owner }}"
-repo "${{ needs.pr.outputs.base_repo_name }}"
-ref "${{ needs.context.outputs.head_sha }}"
-owner "${{ needs.context.outputs.base_repo_owner }}"
-repo "${{ needs.context.outputs.base_repo_name }}"
-state failure
-context "test/terratest/${{ matrix.platform }}"
-description "Tests failed"
Expand All @@ -310,9 +329,9 @@ jobs:
with:
args: >-
-action update_state
-ref "${{ needs.pr.outputs.head_sha }}"
-owner "${{ needs.pr.outputs.base_repo_owner }}"
-repo "${{ needs.pr.outputs.base_repo_name }}"
-ref "${{ needs.context.outputs.head_sha }}"
-owner "${{ needs.context.outputs.base_repo_owner }}"
-repo "${{ needs.context.outputs.base_repo_name }}"
-state success
-context "test/terratest/${{ matrix.platform }}"
-description "Tests passed"
Expand All @@ -326,9 +345,9 @@ jobs:
with:
args: >-
-action update_state
-ref "${{ needs.pr.outputs.head_sha }}"
-owner "${{ needs.pr.outputs.base_repo_owner }}"
-repo "${{ needs.pr.outputs.base_repo_name }}"
-ref "${{ needs.context.outputs.head_sha }}"
-owner "${{ needs.context.outputs.base_repo_owner }}"
-repo "${{ needs.context.outputs.base_repo_name }}"
-state error
-context "test/terratest/${{ matrix.platform }}"
-description "Tests cancelled"
Expand All @@ -338,8 +357,8 @@ jobs:

finalize:
runs-on: ${{ fromJSON(inputs.runs-on) }}
needs: [terratest, pr]
if: ${{ always() && needs.pr.result != 'skipped' }}
needs: [terratest, context]
if: ${{ always() && needs.context.result != 'skipped' }}
steps:
- shell: bash
id: status
Expand All @@ -362,9 +381,9 @@ jobs:
with:
args: >-
-action update_state
-ref "${{ needs.pr.outputs.head_sha }}"
-owner "${{ needs.pr.outputs.base_repo_owner }}"
-repo "${{ needs.pr.outputs.base_repo_name }}"
-ref "${{ needs.context.outputs.head_sha }}"
-owner "${{ needs.context.outputs.base_repo_owner }}"
-repo "${{ needs.context.outputs.base_repo_name }}"
-state ${{ steps.status.outputs.result }}
-context "test/terratest"
-description "Tests started by @${{ github.actor }}"
Expand Down

0 comments on commit 6647629

Please sign in to comment.