Automating GitHub Actions with Custom Comment Commands

Azamat Murzagalin
4 min readJan 27, 2024

--

GitHub Actions provide a powerful platform for automating workflows in response to various events. While predefined sets of commands are commonly used, there are scenarios where a more flexible approach is desired. In this guide, we’ll explore a customizable solution that allows users to trigger GitHub Actions workflows by leaving comments with specific templates. This method provides a high degree of flexibility, enabling users to run a wide range of commands tailored to their specific needs.

By implementing a workflow triggered by comments following a custom template ($run: <command>), users can dynamically execute commands in response on pull requests. This approach not only simplifies the process of triggering actions but also allows for a more adaptable and extensible automation setup. Follow the steps outlined in this guide to implement a GitHub Actions workflow that responds to comments, providing a versatile solution for your automation needs.

Explore the Example Project in the Repository

For a practical demonstration, check out the associated GitHub repository, which includes a simple Java project. This project has a minimal codebase, featuring two test classes — one with failing tests and another with passing tests. This setup showcases the flexibility of the GitHub Actions workflow, allowing for dynamic command execution in response to comment commands.

Repository: link

Explaining the Setup Process Step by Step

In the upcoming sections, I’ll walk you through the setup process step by step. If you encounter any questions or uncertainties during the process, don’t hesitate to reach out for assistance!

Step 1: Create a GitHub Actions Workflow File

Create a file named .github/workflows/run-command-on-comment.yml in the root of your repository. This file defines the workflow.

It sets up a job named `process_comment` that triggers when a new comment is created on an issue or pull request. The job runs on Ubuntu, checks out the repository code, and sets up a Java environment for the example project. This environment is then ready for further steps.

Step 2: Define the Command from the comment

Define check_comment step to check if the comment contains a command:

This step is more complicated and requires a proper explanation for understanding.

1.comment='${{ github.event.comment.body }}': Stores the body of the comment in the comment variable.

2. command=$(echo $comment | grep -oE '\$run: (.+)' | cut -d ' ' -f 2-): Uses a combination of commands to extract the command following the $run: template from the comment. Let's break it down:

  • grep -oE '\$run: (.+)': Searches for the pattern $run: followed by one or more characters and outputs only the matched part.
  • cut -d ' ' -f 2-: Cuts the output using space as the delimiter and selects the part starting from the second field. This effectively isolates the command to be run.

3. echo "command=$command" >> $GITHUB_OUTPUT: Appends the extracted command to the GITHUB_OUTPUT file. This allows you to use the output in subsequent steps of the workflow, making it accessible to other parts of the workflow.

Step 3: Execute the Command

The last step is running the command:

this step executes the command if and only if the check_comment step successfully extracted a command from the comment, and that extracted command is not an empty string. This setup ensures that the workflow only runs a command when a valid $run: template is found in the comment.

Testing the GitHub Actions Workflow

To ensure the effectiveness of your GitHub Actions workflow for running commands based on comments, follow these simple steps:

1. Create a Test Pull Request:

  • Create a pull request in your repository.
  • Leave a comment in the pull request using the specified $run: <command> template.

2. Check the Workflow Execution:

  • Navigate to the GitHub Actions tab in your repository.
  • Review the workflow run logs to ensure the workflow is triggered.

3. View the Showcase Pull Request:

Link to the pull request

Link to the actions

Test run execution:

Sample comment to run tests in PassingTests.java

Link to the test execution logs

By following these steps, you can verify that the workflow correctly extracts and executes the command based on the comment in your test pull request.

Conclusion

This basic setup should provide you with a starting point. Depending on your specific needs, you may want to enhance the workflow with additional checks, security measures, or customizations. As we wrap up our guide, it’s essential to highlight a few key points.

Security! 🛑

Allowing the execution of custom commands brings flexibility, but it also raises security concerns. Exercise caution and limit access to this feature to trusted contributors. Malicious commands can have serious consequences, so be mindful of who can run them.

How vs. Why

This guide focuses on the “how” of implementing the workflow. The “why” is often defined by software engineers who understand their project’s unique needs. Once you have a clear “why,” this article provides a practical “how” to get you started quickly.

Remember, thoughtful implementation and testing of your GitHub Actions workflow ensure a balance between efficiency and security in your development process. Happy automating! 🚀

--

--