Automatically Run GitHub Actions Workflow On New Branch or Tag Creation
Introduction
In the realm of DevOps, automation and continuous integration/continuous deployment (CI/CD) are the keys to smooth and efficient software delivery. GitHub Actions is a popular tool for implementing these processes, providing developers with a powerful platform to automate and perform tasks right from their GitHub repositories. In this blog post, we’ll delve into how to automatically run your GitHub Actions workflows whenever a new branch or tag is created.
Understanding GitHub Actions
GitHub Actions is a CI/CD tool developed and maintained by GitHub. It is event-driven, which means you can run a series of commands whenever a specified event occurs. These events could be anything from pushing commits, opening pull requests, to creating branches or tags.
The workflows run in response to these events are defined in YAML files inside the .github/workflows
directory of your repository. This file is where you will specify when and how to run your workflows.
Setting Up Your Workflow
Before we jump into the step-by-step guide, it’s worth noting that you should have a basic understanding of YAML syntax and GitHub Actions. If you’re new to GitHub Actions, I recommend going through their official documentation first.
Step 1: Create a Workflow YAML File
The first step is to create a new YAML file inside your repository’s .github/workflows
directory. This file will hold the configuration for your workflow. You can name it anything, but for this guide, let’s name it new-branch-tag-workflow.yml
.
1 |
name: New Branch or Tag Workflow |
The name
field provides a name for your workflow. This name is displayed on your repository’s actions page.
Step 2: Define the on
Field
Next, we need to define the on
field, which specifies the events that trigger the workflow.
1 2 |
on: create: |
In this case, we are using the create
event, which is triggered whenever a branch or tag is created.
Step 3: Define the jobs
Field
The jobs
field is where you define what your workflow does. In this example, we’ll simply print a message using the echo
command.
1 2 3 4 5 6 |
jobs: build: runs-on: ubuntu-latest steps: - name: Print a message run: echo "A new branch or tag was created!" |
This defines a single job named build
that runs on the latest version of Ubuntu. The job has a single step that prints a message.
Complete Workflow
Combining the steps above, our complete workflow file looks like this:
1 2 3 4 5 6 7 8 9 10 11 |
name: New Branch or Tag Workflow on: create: jobs: build: runs-on: ubuntu-latest steps: - name: Print a message run: echo "A new branch or tag was created!" |
Whenever a new branch or tag is created, this workflow will be triggered and print the message “A new branch or tag was created!”.
Conclusion
GitHub Actions is a powerful tool for automating tasks in your repository. By using the create
event, you can set up workflows that automatically run whenever a new branch or tag is created. This can help streamline your development process and ensure your code is always in a good state.
Remember, this is just a simple example, and real-world workflows would typically be more complex, involving building applications, running tests, or deploying code. The possibilities with GitHub Actions are nearly endless.