From 749670c51d75bfeb8a0d32ffea5b52c92393ecf8 Mon Sep 17 00:00:00 2001 From: Dmitry Kisler Date: Tue, 10 Oct 2023 12:33:35 +0200 Subject: [PATCH] docs: update readme for partify with setup-terraform Signed-off-by: Dmitry Kisler --- README.md | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 221 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 21ff839..059a698 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,222 @@ steps: - uses: opentofu/setup-opentofu ``` +A specific version of OpenTofu CLI can be installed: + +```yaml +steps: +- uses: opentofu/setup-opentofu + with: + tofu_version: 1.6.0-alpha1 +``` + +Credentials for Terraform Cloud ([app.terraform.io](https://app.terraform.io/)) can be configured: + +```yaml +steps: +- uses: opentofu/setup-opentofu + with: + cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} +``` + +Credentials for Terraform Enterprise (TFE) can be configured: + +```yaml +steps: +- uses: opentofu/setup-opentofu + with: + cli_config_credentials_hostname: 'tofu.example.com' + cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} +``` + +The wrapper script installation can be skipped by setting the `tofu_wrapper` variable to `false`: + +```yaml +steps: +- uses: opentofu/setup-opentofu + with: + tofu_wrapper: false +``` + +Subsequent steps can access outputs when the wrapper script is installed: + +```yaml +steps: +- uses: opentofu/setup-opentofu + +- run: tofu init + +- id: plan + run: tofu plan -no-color + +- run: echo ${{ steps.plan.outputs.stdout }} +- run: echo ${{ steps.plan.outputs.stderr }} +- run: echo ${{ steps.plan.outputs.exitcode }} +``` + +Outputs can be used in subsequent steps to comment on the pull request: + +> **Notice:** There's a limit to the number of characters inside a GitHub comment (65535). +> +> Due to that limitation, you might end up with a failed workflow run even if the plan succeeded. +> +> Another approach is to append your plan into the $GITHUB_STEP_SUMMARY environment variable which supports markdown. + +```yaml +defaults: + run: + working-directory: ${{ env.tf_actions_working_dir }} +permissions: + pull-requests: write +steps: +- uses: actions/checkout@v3 +- uses: opentofu/setup-opentofu + +- name: OpenTofu fmt + id: fmt + run: tofu fmt -check + continue-on-error: true + +- name: OpenTofu Init + id: init + run: tofu init + +- name: OpenTofu Validate + id: validate + run: tofu validate -no-color + +- name: OpenTofu Plan + id: plan + run: tofu plan -no-color + continue-on-error: true + +- uses: actions/github-script@v6 + if: github.event_name == 'pull_request' + env: + PLAN: "tofu\n${{ steps.plan.outputs.stdout }}" + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const output = `#### OpenTofu Format and Style 🖌\`${{ steps.fmt.outcome }}\` + #### OpenTofu Initialization ⚙️\`${{ steps.init.outcome }}\` + #### OpenTofu Validation 🤖\`${{ steps.validate.outcome }}\` +
Validation Output + + \`\`\`\n + ${{ steps.validate.outputs.stdout }} + \`\`\` + +
+ + #### OpenTofu Plan 📖\`${{ steps.plan.outcome }}\` + +
Show Plan + + \`\`\`\n + ${process.env.PLAN} + \`\`\` + +
+ + *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + }) +``` + +Instead of creating a new comment each time, you can also update an existing one: + +```yaml +defaults: + run: + working-directory: ${{ env.tf_actions_working_dir }} +permissions: + pull-requests: write +steps: +- uses: actions/checkout@v3 +- uses: opentofu/setup-opentofu + +- name: OpenTofu fmt + id: fmt + run: tofu fmt -check + continue-on-error: true + +- name: OpenTofu Init + id: init + run: tofu init + +- name: OpenTofu Validate + id: validate + run: tofu validate -no-color + +- name: OpenTofu Plan + id: plan + run: tofu plan -no-color + continue-on-error: true + +- uses: actions/github-script@v6 + if: github.event_name == 'pull_request' + env: + PLAN: "tofu\n${{ steps.plan.outputs.stdout }}" + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + // 1. Retrieve existing bot comments for the PR + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + }) + const botComment = comments.find(comment => { + return comment.user.type === 'Bot' && comment.body.includes('OpenTofu Format and Style') + }) + + // 2. Prepare format of the comment + const output = `#### OpenTofu Format and Style 🖌\`${{ steps.fmt.outcome }}\` + #### OpenTofu Initialization ⚙️\`${{ steps.init.outcome }}\` + #### OpenTofu Validation 🤖\`${{ steps.validate.outcome }}\` +
Validation Output + + \`\`\`\n + ${{ steps.validate.outputs.stdout }} + \`\`\` + +
+ + #### OpenTofu Plan 📖\`${{ steps.plan.outcome }}\` + +
Show Plan + + \`\`\`\n + ${process.env.PLAN} + \`\`\` + +
+ + *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`; + + // 3. If we have a comment, update it, otherwise create a new one + if (botComment) { + github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: botComment.id, + body: output + }) + } else { + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + }) + } +``` + ## Inputs The action supports the following inputs: @@ -29,7 +245,11 @@ The action supports the following inputs: place within the credentials block of the OpenTofu CLI configuration file. Defaults to `app.terraform.io`. - `cli_config_credentials_token` - (optional) The API token for a Terraform Cloud/Enterprise instance to place within the credentials block of the OpenTofu CLI configuration file. -- `tofu_version` - (optional) The version of OpenTofu CLI to install. If no version is given, it will default to `latest`. +- `tofu_version` - (optional) The version of OpenTofu CLI to install. Instead of a full version string, + you can also specify a constraint string (see [Semver Ranges](https://www.npmjs.com/package/semver#ranges) + for available range specifications). Examples are: `<1.6.0-beta`, `~1.6.0-alpha`, `1.6.0-alpha2` (all three installing + the latest available `1.6.0-alpha2` version). Prerelease versions can be specified and a range will stay within the + given tag such as `beta` or `rc`. If no version is given, it will default to `latest`. - `tofu_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of the `tofu` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.