GitHub Actions Workflow Syntax: on: delete with Examples
Introduction
GitHub Actions is a robust CI/CD tool that allows developers to automate tasks within their software development lifecycle directly from their GitHub repositories. The core of this automation lies in the workflow files, which are defined using a specific syntax. In this blog post, we will explore the on: delete
event in the GitHub Actions Workflow Syntax, its usage, and provide examples.
Understanding the on: delete
Event
In GitHub Actions, workflows are triggered by specific events. The on: delete
event is triggered whenever a Git branch or tag is deleted in the repository. This event can be particularly useful when you want to automate tasks whenever a branch or tag is deleted, such as cleaning up resources or environments that were associated with that branch or tag.
Usage of the on: delete
Event
The on: delete
event is defined in the workflow file right after the on
keyword. Here’s a basic example of how to use the on: delete
event:
1 2 3 4 5 6 7 8 9 |
name: Branch or Tag Deletion Workflow on: delete jobs: cleanup: runs-on: ubuntu-latest steps: - name: Run a one-line script run: echo A branch or tag was deleted! |
In this example, the workflow is named “Branch or Tag Deletion Workflow”. This workflow is triggered whenever a branch or tag is deleted in the repository. The workflow has one job named cleanup
that runs on the latest version of Ubuntu. This job has one step:
- Run a one-line script: This step runs a script that simply echoes a message indicating that a branch or tag was deleted.
Common Questions and Answers
Q: Can I use the on: delete
event to trigger workflows only when a branch is deleted, not a tag?
A: No, the on: delete
event does not differentiate between a branch or a tag. It triggers the workflow whenever either a branch or a tag is deleted. If you need to differentiate between them, you can do so in the workflow steps using conditional statements.
Q: Can I specify which branches or tags the on: delete
event applies to?
A: No, the on: delete
event applies to all branches and tags. If you want to limit the workflow to specific branches or tags, you would need to do this within the workflow steps using conditional statements.
Q: Can I use the on: delete
event in conjunction with other events?
A: Yes, you can specify multiple events to trigger the workflow. For example, you could have a workflow that runs on both push
and delete
events.
Conclusion
The on: delete
event in GitHub Actions Workflow Syntax is a powerful tool that allows you to automate tasks whenever a branch or tag is deleted in your repository. By understanding and effectively using this event, you can greatly enhance your automation capabilities.