GitHub Actions provide a powerful automation platform for workflows in your repository. Often, you might need to execute certain tasks or steps only on specific branches, such as the release- branch. Here, we’ll explore how to configure GitHub Actions to run specific steps exclusively on the release- branch using conditional expressions.
Setting up GitHub Actions for release- Branch
- Create a Workflow File: In your repository, navigate to the
.github/workflowsdirectory (create it if it doesn’t exist) and add a new YAML file. For example, name itrelease.yml. - Defining the Workflow: Inside the
release.ymlfile, configure the GitHub Actions workflow using YAML syntax:
name: Release Branch Jobs
on:
push:
branches:
- 'release-*'
jobs:
your_job_name:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run step on release branch
if: startsWith(github.ref, 'refs/heads/release-')
run: |
echo "This step runs only on the release branch"
# Add your desired commands here
In this configuration:
- The workflow triggers on a
pushevent to branches matching the patternrelease-*. - A job named
your_job_nameis defined, specifying it to run onubuntu-latest. - The
Run step on release branchstep includes a condition (if) that checks if the branch starts withrefs/heads/release-. Only if this condition is met, the specified commands within therunblock will execute.
- Customize Steps: You can add and customize the steps within the
Run step on release branchsection to perform actions specifically tailored to yourrelease-branch.
Saving and Testing the Workflow
- Save the Changes: Save the
release.ymlfile in the.github/workflowsdirectory. - Commit and Push: Commit the changes to your repository and push them to trigger the GitHub Actions workflow. Make sure that your
release-branch is correctly named and pushes are made to this branch to observe the workflow execution.
Conclusion
With this setup, your GitHub Actions workflow will execute specific steps only when there is a push to a branch starting with release-. This approach allows you to run customized actions or tasks specifically on your release- branch, segregating tasks as needed and enhancing the automation of your repository workflow.
Feel free to modify the job name, steps, and commands within the workflow to suit your project’s requirements.
GitHub Actions offer a robust way to automate various tasks and streamline your development processes, ensuring efficiency and control in your code deployment workflows.
Want to Listen to This Guide While Working?
Try Speechify — the AI voice reader that helps DevOps engineers consume technical documentation while working in the terminal.