How to add an action to my GitHub repository?
Description
GitHub Actions is a powerful automation tool that allows you to define workflows for your repository. It helps automate tasks such as testing, building, and deploying code. Setting up your first GitHub Action enables you to streamline development processes and improve efficiency.
Considerations
- Ensure your repository has a
.github/workflows/
directory where workflow files will be stored. - Workflows are defined using YAML files (
.yml
or.yaml
). - Actions can be triggered by various GitHub events, such as
push
,pull_request
, or scheduled times. - GitHub provides a marketplace with reusable actions that can be integrated into workflows.
- Understanding basic YAML syntax is essential for configuring GitHub Actions correctly.
- The workflow execution environment supports different operating systems, including Ubuntu, Windows, and macOS.
- Review the repository’s security settings to control access to sensitive information, such as API keys and credentials, stored as secrets.
Solutions
-
Creating a basic GitHub Action workflow
Setting up a GitHub Action workflow allows you to automate tasks like testing and deployment. By defining a workflow file, you can specify triggers and actions to execute when specific events occur.
To create a basic workflow follow these steps:
- Navigate to your GitHub repository and create a
.github/workflows/ directory
if it doesn’t exist. - Create a new YAML file (e.g.,
first-action.yml
) inside this directory. - Define a basic workflow that runs on
push
events:name: First GitHub Action on: [push] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Run a simple command run: echo "Hello, GitHub Actions!"
- Commit and push this file to your repository.
-
Go to the Actions tab in your GitHub repository to see the workflow run. The real-time visualization graph displayed here provides a clear sequence of executed jobs, making it easier to track progress and identify potential issues. Click on individual jobs to expand the steps, where you’ll find detailed logs and error messages to help with troubleshooting and debugging efficiently.
- Navigate to your GitHub repository and create a
-
Using pre-built actions from the GitHub Marketplace
The GitHub Marketplace provides pre-built actions that can be easily integrated into workflows to automate tasks. To use an action from the Marketplace, follow these steps:
-
Search for a relevant action in the Marketplace and open its page.
-
Copy the code provided under the “Use latest version” section and add it to your workflow file.
-
For example, you could decide to use the pre-built Python setup action to ensure the correct Python version is available for running scripts, tests, or installations. Copy the following
step
into your workflow file:- name: Setup Python uses: actions/setup-python@v4 with: python-version: '3.10'
-
-
Automating testing with GitHub Actions
-
Use actions to run tests automatically after each push.
-
Example: Running unit tests for a Python project using pytest:
jobs: test: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup Python uses: actions/setup-python@v4 with: python-version: '3.10' - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Run tests run: pytest
-
Further guidance
-
General documentation:
- GitHub Actions Documentation - Official documentation covering all aspects of GitHub Actions.
- GitHub Marketplace for Actions - Browse and search for actions to use in your workflows.
- GitHub Actions Examples - Collection of sample workflows and configurations.
- YAML Syntax Guide - Learn YAML syntax for writing workflow files.
-
Specific tools used in the examples:
- setup-python Action - Official action for setting up Python in GitHub Actions workflows.
- pytest Documentation - Comprehensive guide for writing and running Python tests with pytest.
-
Security Considerations:
- Using Secrets in GitHub Actions - Guide on securely managing sensitive information in GitHub Actions workflows.
References
The official GitHub Actions documentation was consulted for accuracy and best practices.
Skip tool tableTools and resources on this page
Tool or resource | Description | Related pages | Registry |
---|---|---|---|
GitHub | GitHub is a platform that allows developers to create, store, manage, and share their code. It uses Git to provide distributed version control. GitHub provides access control, bug tracking, software feature requests, task management, continuous integration, and wikis for every project. | Creating a CITATION.cf... Writing a CodeMeta file Creating a 'Read the D... Releasing code Software documentation Version control Digital Software Ident... | |
GitHub Actions | GitHub's infrastructure for continuous integration, deployment and delivery | Creating a 'Read the D... Testing code |
How to cite this page
Eva Martín del Pico, "How to add your first GitHub action". everse.software. http://everse.software/RSQKit/first_github_action .