How to Use GitHub Actions to Automate Open-Source Projects
Learn how to leverage GitHub Actions to automate tasks such as continuous integration, testing, deployment, etc., for your open-source projects.
Table of Contents
ToggleIntroduction
Imagine you are working on an open-source project with a team of developers. You have written and tested hundreds of lines of code and tested it thoroughly, but manually testing, building, and deploying your code to a production environment is tedious and time-consuming. This is where GitHub Actions come into play.
GitHub Actions enables developers to automate the software development life cycle. With GitHub Actions, you can automate tasks such as building, testing, and deploying your code, making the process faster.
So, what exactly are GitHub Actions, and how can you use them to automate your open-source project? You’ll find out in this article.
What are GitHub Actions?
GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline within a GitHub repository. GitHub Actions are written in YAML code. YAML stands for Yet Another Markup Language.
It integrates seamlessly with other GitHub features, such as pull requests, issues, and code reviews, making it an excellent choice for collaborative projects.
Components of GitHub Actions
Here are some of the components of GitHub Actions;
- Step: A step is an individual task within a job. Each step can be a command, script, or action.
- Jobs: One or more steps make up Jobs that perform specific tasks, such as testing, building, and deploying code changes.
- Workflow: A workflow is a collection of one or more jobs that run on a specific event, such as pushing code changes to a repository.
- Event: This is an action or occurrence that triggers a workflow
- Trigger: This is an event or condition that initiates the execution of a specific action or process.
- Runner: A runner is a machine or virtual environment on which jobs are run. GitHub provides hosted runners with different operating systems and software pre-installed.
Setup a GitHub Workflow
For this article, we use the First Interaction Github action from GitHub Marketplace. This action sends a congratulatory message to someone when they open their first pull request.
To automate your open-source project with GitHub Actions, you must define a workflow by setting up a workflow file in your repository.
To set up a GitHub workflow, here are the steps:
Step 1: Set up your repository
Create a new repository on GitHub or navigate to an existing repository. Ensure your repository contains the code or project files you want to automate.
Step 2: Create a Workflow file
In your repository, create a new directory called .github/workflows.
Inside the workflows directory, create a new YAML file and name it greetings.yml
.
Step 3: Write your Workflow file
- Open the YAML file in a text editor or the GitHub editor and paste the following content:
name: Greetings
on: pull_request
jobs:
greeting:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: actions/first-interaction@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
pr-message: "Congratulations on your first pull request"
- Name the workflow using the name key, such as Greetings.
- Define the trigger event that initiates the workflow. For example, to trigger the workflow on every push event, use
on: push
oron: pull request
to trigger the workflow on every pull request. - Define the jobs that the workflow will perform. Begin with jobs, followed by an indented block. Specify the name of the job using the name key, such as greeting. Also, state the environment that the job runs on. For this example, the job runs on an Ubuntu environment (
runs-on: ubuntu-latest
); jobs can also run onwindows-latest
. - Specify the permissions required for the job. Pull-requests: write allows the job to write to pull requests.
- Specify the steps that the job requires. Each step is a task that contributes to the job’s execution. Use the steps: key followed by an indented block.
- Add more steps as needed for your workflow. For example, you can use the
actions/first-interaction@v1
action to automate welcoming and acknowledging new contributors to a repository.
Step 4: Commit and push your Workflow file
- To save the changes to your workflow file, commit the file to your repository and add a commit message.
- Push the commit to your repository.
Step 5: View your Workflow
After pushing the changes, navigate to your GitHub repository’s Actions tab to view the status of your workflow.
Click on the workflow to view its details, including the executed steps and logs.
Congratulations! You have created your first GitHub Actions workflow.
GitHub Actions from the Marketplace
GitHub Marketplace provides a space to find pre-built actions that perform specific tasks. You can include these actions in your workflow by specifying the action in the steps of your job. You can use existing actions from the GitHub Marketplace or create custom actions within the steps. In the GitHub Marketplace, you can find predefined actions, or you have the option to create custom-built actions using languages such as JavaScript, Python, or Shell.
Some GitHub Actions that you can use for your open-source projects:
- Close Stale Issues: This action closes issues and PRs that have not received updates within a specified period.
- First Interaction: This action filters out pull requests and issues from first-time contributors.
- Docstring Auditor: Use this action to keep Python code documentation accurate and up-to-date.
- Sync and merge the upstream repository with your current repository: This GitHub Action merges changes from the remote repository.
Use Cases of GitHub Actions in Open-Source Projects
Let’s explore a few real-life examples of how open-source projects use GitHub Actions to automate tasks.
- Automated Code Formatting and Linting: GitHub workflow can be set up to format automatically and lint the project’s codebase on every push. This ensures a consistent code style and catches potential issues early.
- Continuous Deployment to Hosting Platforms: A GitHub workflow automatically builds and deploys the project to a hosting platform. This allows for rapid and reliable deployment of new features and bug fixes.
- Automating Release Processes: A GitHub workflow automates the release process of the project. It generates release notes, creates git tags, and publishes artefacts, saving time and effort for the project maintainers.
Conclusion
GitHub Actions can help you streamline your open-source project. By defining jobs and steps in a workflow file, you can automate tasks like testing, building, and deploying code changes.
I hope this blog post has helped you get started with GitHub Actions.