'Github Action running "terraform plan" failing with a 404
I am running the following Github action that should post the output from terraform planto the PR in Github but it's giving me a 404. I have pretty much copied the action from Terraform
The only thing I can see that is suspect is the URL that's generating the 404 looks like it's missing a value - it says "https://api.github.com/repos/orgname/reponame/issues//comments" but I think it probably should be "https://api.github.com/repos/orgname/reponame/issues/SOMETHING/comments":
- name: Run Terraform Plan
id: plan
run: |
cd operations/app/terraform/vars/staging
terraform plan -no-color -input=false
continue-on-error: true
- uses: actions/github-script@v6
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style 🖌 \`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
I get the following:
Plan: 0 to add, 1 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
RequestError [HttpError]: Not Found
at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:4469:21
at processTicksAndRejections (node:internal/process/task_queues:96:5) ***
status: 404,
response: ***
url: 'https://api.github.com/repos/myorg/myrepo/issues//comments',
Error: Unhandled error: HttpError: Not Found
status: 404,
headers: ***
<snip headers>
data: ***
message: 'Not Found',
documentation_url: 'https://docs.github.com/rest'
***
***,
request: ***
method: 'POST',
url: 'https://api.github.com/repos/orgname/reponame/issues//comments',
headers: ***
accept: 'application/vnd.github.-preview+json',
'user-agent': 'actions/github-script octokit-core.js/3.5.1 Node.js/16.13.0 (linux; x64)',
authorization: 'token [REDACTED]',
'content-type': 'application/json; charset=utf-8'
***,
Solution 1:[1]
Try putting await in front of the function call in your action.
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
Solution 2:[2]
The issue was this was running on both push and pull_request - but on a push there is no PR for it to add the comment to so that portion failed. Adding a conditional for the github-script got it working as expected
- uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mpriscella |
| Solution 2 | TheFiddlerWins |
