Skip to content

Commit

Permalink
Added opentofu support to terratest command (#160)
Browse files Browse the repository at this point in the history
* Added opentofu support to terratest command

* Made terratest as matrix

* Update terratest-command.yml

* Update terratest-command.yml

* Test workflow dispatch

* Test workflow dispatch
  • Loading branch information
goruha authored Jun 6, 2024
1 parent 8678b08 commit 0b661f7
Showing 1 changed file with 95 additions and 15 deletions.
110 changes: 95 additions & 15 deletions .github/workflows/terratest-command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,44 @@ jobs:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
reactions: '+1'

pending:
runs-on: ubuntu-latest
steps:
- name: "Update GitHub Status for pending"
uses: docker://cloudposse/github-status-updater
with:
args: >-
-action update_state
-ref "${{ github.event.client_payload.pull_request.head.sha }}"
-repo "${{ github.event.client_payload.github.payload.repository.name }}"
-state pending
-context "test/terratest"
-description "Tests started by @${{ github.event.client_payload.github.actor }}"
-url "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-owner "${{ github.event.client_payload.github.payload.repository.owner.login }}"
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}

terratest:
runs-on: ubuntu-latest
container: cloudposse/test-harness:latest
needs: [pending]
env:
MAKE_INCLUDES: Makefile
continue-on-error: true
strategy:
max-parallel: 1
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@v3
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
ref: ${{ github.event.client_payload.pull_request.head.ref }}

- name: "Update GitHub Status for pending"
uses: docker://cloudposse/github-status-updater
with:
Expand All @@ -35,21 +67,15 @@ jobs:
-ref "${{ github.event.client_payload.pull_request.head.sha }}"
-repo "${{ github.event.client_payload.github.payload.repository.name }}"
-state pending
-context "test/terratest"
-context "test/terratest/${{ matrix.platform }}"
-description "Tests started by @${{ github.event.client_payload.github.actor }}"
-url "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-owner "${{ github.event.client_payload.github.payload.repository.owner.login }}"
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}

- name: "Checkout code for ChatOps"
uses: actions/checkout@v3
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
ref: ${{ github.event.client_payload.pull_request.head.ref }}

- name: "Determine required terraform version"
if: ${{ matrix.platform == 'terraform' }}
shell: bash -x -e -o pipefail {0}
run: |
# Some legacy support is on 0.11 branches and we determine the Terraform version based on the target branch name
Expand Down Expand Up @@ -79,9 +105,9 @@ jobs:
[[ $VERSION =~ ^1\. ]] && VERSION=1
PATH_TO_TERRAFORM="/usr/local/terraform/${VERSION}/bin"
if [ -x "${PATH_TO_TERRAFORM}/terraform" ]; then
echo "${PATH_TO_TERRAFORM}" >> "$GITHUB_PATH"
PATH_TO_TERRAFORM=$(update-alternatives --list terraform | grep "/${VERSION}")
if [ -x "${PATH_TO_TERRAFORM}" ]; then
update-alternatives --set terraform ${PATH_TO_TERRAFORM}
else
echo "Unable to locate executable for terraform ${VERSION}" >&2
exit 1
Expand All @@ -91,6 +117,22 @@ jobs:
BASE_REF: ${{ github.event.client_payload.pull_request.base.ref }}
LABELS: ${{ join(github.event.client_payload.pull_request.labels.*.name, '\n') }}

- name: "Determine required opentofu version"
if: ${{ matrix.platform == 'opentofu' }}
shell: bash -x -e -o pipefail {0}
run: |
PATH_TO_TERRAFORM=$(update-alternatives --list terraform | grep "/tofu")
if [ -x "${PATH_TO_TERRAFORM}" ]; then
update-alternatives --set terraform ${PATH_TO_TERRAFORM}
else
echo "Unable to locate executable for opentofu" >&2
exit 1
fi
env:
# Pull request target branch
BASE_REF: ${{ github.event.client_payload.pull_request.base.ref }}
LABELS: ${{ join(github.event.client_payload.pull_request.labels.*.name, '\n') }}

- name: "Initialize terratest Go project"
run: |
make -C test/src clean init
Expand Down Expand Up @@ -166,7 +208,9 @@ jobs:
fi
- name: "Test `examples/complete` with terratest"
run: make -C test/src
run: |-
terraform --version
make -C test/src
- name: "Update GitHub Status for failure"
if: ${{ failure() }}
Expand All @@ -177,7 +221,7 @@ jobs:
-ref "${{ github.event.client_payload.pull_request.head.sha }}"
-repo "${{ github.event.client_payload.github.payload.repository.name }}"
-state failure
-context "test/terratest"
-context "test/terratest/${{ matrix.platform }}"
-description "Tests failed"
-url "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-owner "${{ github.event.client_payload.github.payload.repository.owner.login }}"
Expand All @@ -192,7 +236,7 @@ jobs:
-ref "${{ github.event.client_payload.pull_request.head.sha }}"
-repo "${{ github.event.client_payload.github.payload.repository.name }}"
-state success
-context "test/terratest"
-context "test/terratest/${{ matrix.platform }}"
-description "Tests passed"
-url "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-owner "${{ github.event.client_payload.github.payload.repository.owner.login }}"
Expand All @@ -208,9 +252,45 @@ jobs:
-ref "${{ github.event.client_payload.pull_request.head.sha }}"
-repo "${{ github.event.client_payload.github.payload.repository.name }}"
-state error
-context "test/terratest"
-context "test/terratest/${{ matrix.platform }}"
-description "Tests cancelled"
-url "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-owner "${{ github.event.client_payload.github.payload.repository.owner.login }}"
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}

finalize:
runs-on: ubuntu-latest
needs: [terratest]
if: ${{ always() }}
steps:
- shell: bash
id: status
run: |
if [[ ${{ needs.terratest.result }} == 'success' ]]; then
echo "result=success" >> $GITHUB_OUTPUT
elif [[ ${{ needs.terratest.result }} == 'cancelled' ]]; then
echo "result=failure" >> $GITHUB_OUTPUT
elif [[ ${{ needs.terratest.result }} == 'failure' ]]; then
echo "result=failure" >> $GITHUB_OUTPUT
elif [[ ${{ needs.terratest.result }} == 'skipped' ]]; then
echo "result=failure" >> $GITHUB_OUTPUT
else
echo "Some tests failed"
exit 1
fi
- name: "Update GitHub Status for pending"
uses: docker://cloudposse/github-status-updater
with:
args: >-
-action update_state
-ref "${{ github.event.client_payload.pull_request.head.sha }}"
-repo "${{ github.event.client_payload.github.payload.repository.name }}"
-state ${{ steps.status.outputs.result }}
-context "test/terratest"
-description "Tests started by @${{ github.event.client_payload.github.actor }}"
-url "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-owner "${{ github.event.client_payload.github.payload.repository.owner.login }}"
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}

0 comments on commit 0b661f7

Please sign in to comment.