github / branch-deploy

Enabling Branch Deployments through IssueOps with GitHub Actions - If you find this project useful, give it a star! ⭐️
https://github.com/marketplace/actions/branch-deploy
MIT License
364 stars 44 forks source link

Error: TypeError: Cannot read properties of undefined (reading 'body') using v9.1.3 #265

Closed xirkus closed 2 months ago

xirkus commented 3 months ago

Describe the Issue

When using the default configuration of the branch-deploy action, I encounter the following error:

Error: TypeError: Cannot read properties of undefined (reading 'body')
    at run (/home/runner/work/_actions/github/branch-deploy/v9.1.2/src/main.js:95:1)

Also tried with version 9.1.2 with the same results. Looking at the line of code in question, it assumes (without assertions) that context.payload.comment.body is not null.

// Get the body of the IssueOps command
    const body = context.payload.comment.body.trim()

What needs to be enabled for this action to work? Ideally, the code also performs an additional check with error handling if this assumption is false.

Action Configuration

  - name: branch-deploy
    id: branch-deploy
    uses: github/branch-deploy@v9.1.3

Relevant Actions Log Output

[debug]Evaluating condition for step: 'branch-deploy'

[debug]Evaluating: success()

[debug]Evaluating success:

[debug]=> true

[debug]Result: true

[debug]Starting: branch-deploy

[debug]Register post job cleanup for action: github/branch-deploy@v9.1.2

[debug]Loading inputs

[debug]Evaluating: github.token

[debug]Evaluating Index:

[debug]..Evaluating github:

[debug]..=> Object

[debug]..Evaluating String:

[debug]..=> 'token'

[debug]=> '***'

[debug]Result: '***'

[debug]Evaluating: job.status

[debug]Evaluating Index:

[debug]..Evaluating job:

[debug]..=> Object

[debug]..Evaluating String:

[debug]..=> 'status'

[debug]=> 'success'

[debug]Result: 'success'

[debug]Loading env

Run github/branch-deploy@v9.1.2 with: github_token: status: success environment: production environment_targets: production,development,staging environment_url_in_comment: true production_environments: production reaction: eyes trigger: .deploy noop_trigger: .noop lock_trigger: .lock unlock_trigger: .unlock help_trigger: .help lock_info_alias: .wcid permissions: write,maintain,admin param_separator: | global_lock_flag: --global stable_branch: main update_branch: warn outdated_mode: strict required_contexts: false checks: all allow_forks: true admins: false admins_pat: false merge_deploy_mode: false unlock_on_merge_mode: false skip_completing: false deploy_message_path: .github/deployment_message.md sticky_locks: false sticky_locks_for_noop: false allow_sha_deployments: false disable_naked_commands: false env: AWS_REGION: us-east-1 name: main TF_CLOUD_ORGANIZATION: steezy_ai TF_ACTIONS_WORKING_DIR: ./infra TF_API_TOKEN: TF_WORKSPACE: prod TOFU_CLI_PATH: /home/runner/work/_temp/04fadd97-b2a3-4e50-940a-deac43f231c3 AWS_DEFAULT_REGION: us-east-1 AWS_ACCESS_KEY_ID: AWS_SECRET_ACCESS_KEY: AWS_SESSION_TOKEN: ***

Error: TypeError: Cannot read properties of undefined (reading 'body') at run (/home/runner/work/_actions/github/branch-deploy/v9.1.2/src/main.js:95:1) Error: Cannot read properties of undefined (reading 'body')

[debug]Node Action run completed with exit code 1

[debug]Save intra-action state isPost = true

[debug]Save intra-action state actionsToken = ***

[debug]Save intra-action state bypass = true

[debug]Finishing: branch-deploy

Extra Information

No response

GrantBirki commented 3 months ago

Hey 👋 @xirkus! Thanks for opening this issue.

Do you happen to have the snippet of your yaml file that contains the Action workflow permissions? I want to rule out any possible permission issues.

Also, does your pull request have a body or is it completely empty?

xirkus commented 3 months ago

@GrantBirki Can't enable PRs for my project as I'm the sole developer (and can't approve my own PRs). Did try to enable this though to see if it would address the issue, but no luck :(

Here are the permissions that are enabled for my workflow:

permissions:
  id-token: write   # This is required for requesting the JWT
  deployments: write
  contents: write   # Required for IssueOps commands
  checks: read
  statuses: read

Thanks!

GrantBirki commented 3 months ago

Can't enable PRs for my project as I'm the sole developer

@xirkus can you please elaborate on this a bit more? I have many many (more than I can count 😅) projects where I am also the sole developer and use PRs all the time. I'll work on a branch, push it to GitHub, open a PR, deploy that PR with .deploy, and then just merge it.

If you aren't using pull requests, then where are you running .deploy from to try and ship your changes?

xirkus commented 3 months ago

@GrantBirki I had PRs initially enabled for my project, but when I attempted to approve the change with my dev account, I was unable to do so (could only comment). According to GitHub's official documentation, the submitter of a Pull Request cannot approve their own PR Pull request authors cannot approve their own pull requests.

Can you elaborate on your workflow which works around this limitation?

Also, from you answer, it seems that this action requires PRs for the project, correct?

Thanks!

GrantBirki commented 3 months ago

the submitter of a Pull Request cannot approve their own PR

That is correct! In my personal projects, I simply open PRs and deploy them. Since I cannot approve my own PRs, I don't have approvals required for those projects and thus this Action won't check for them. Here is an example of me opening a PR in a personal project, deploying that PR, and then merging it.

https://github.com/GrantBirki/dns/pull/6

xirkus commented 3 months ago

@GrantBirki Thanks for that info. Let me give this a try. Are there specific PR options that you recommend for this action?

GrantBirki commented 3 months ago

Are there specific PR options that you recommend for this action?

Pretty much all of my repos have CI checks (tests, linter jobs, etc) to some extent. So I use and enforce CI checks before a deployment can be triggered. You can learn more about using CI checks with this Action here

Here is an example ruleset that is a pretty common pattern for folks that use the branch-deploy Action

Image

Here is what that screenshot (of the ruleset) does in simplified terms:

  1. In order to merge a pull request, it must have a successful deployment to the production environment
  2. Commits must be signed (not relevant to this Action but I like it because security)
  3. In order to merge to the main branch, you need to do it through a PR
  4. Status checks must be passing and the branch must be up-to-date. The branch-deploy Action will read these settings and enforce them before I can deploy a pull request.
GrantBirki commented 3 months ago

@xirkus let me know if you have any further questions, if not we can close out this issue - thanks!