You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To control when a GitHub Action runs, you can configure the triggers in the workflow file. GitHub Actions can be triggered by various events like pushes, pull requests, manual invocation, schedules, and more. You can choose the events that suit your workflow. Here’s how you can configure the action to run only under specific conditions:
2
+
3
+
### Key Triggers to Control When a GitHub Action Runs:
4
+
5
+
1.**Manual Trigger (`workflow_dispatch`)**:
6
+
- This allows you to run the action manually from the GitHub Actions tab, instead of running automatically on every push.
7
+
8
+
```yaml
9
+
on:
10
+
workflow_dispatch:
11
+
```
12
+
13
+
2. **Push Events on Specific Branches**:
14
+
- If you only want the action to run on specific branches (e.g., `main`), you can configure the action to only run on pushes to those branches.
15
+
16
+
```yaml
17
+
on:
18
+
push:
19
+
branches:
20
+
- main # Only run on pushes to the 'main' branch
21
+
```
22
+
23
+
3. **Pull Request Events**:
24
+
- You can trigger the action to run only when a pull request is opened, updated, or merged.
25
+
26
+
```yaml
27
+
on:
28
+
pull_request:
29
+
branches:
30
+
- main # Only trigger on pull requests to 'main' branch
31
+
```
32
+
33
+
4. **Run on Specific Files**:
34
+
- If you only want the action to run when certain files change (e.g., files in a `docs/` folder), you can use the `paths` filter:
35
+
36
+
```yaml
37
+
on:
38
+
push:
39
+
branches:
40
+
- main
41
+
paths:
42
+
- 'docs/**' # Only run when files in the 'docs/' directory change
43
+
```
44
+
45
+
5. **Scheduled Runs (`schedule`)**:
46
+
- You can schedule the action to run periodically (e.g., daily or weekly) using cron syntax.
47
+
48
+
```yaml
49
+
on:
50
+
schedule:
51
+
- cron: '0 0 * * 1' # Runs every Monday at midnight UTC
52
+
```
53
+
54
+
6. **Tag or Release Events**:
55
+
- You can trigger actions based on new tags or releases in the repository:
56
+
57
+
```yaml
58
+
on:
59
+
push:
60
+
tags:
61
+
- 'v*' # Trigger when pushing a version tag like 'v1.0.0'
62
+
release:
63
+
types: [published] # Trigger on publishing a release
64
+
```
65
+
66
+
### Combining Multiple Triggers
67
+
68
+
You can also combine multiple triggers if you want the action to run under different conditions, like on manual dispatch or when a tag is pushed.
69
+
70
+
```yaml
71
+
on:
72
+
push:
73
+
branches:
74
+
- main
75
+
workflow_dispatch:
76
+
pull_request:
77
+
branches:
78
+
- main
79
+
release:
80
+
types: [published]
81
+
```
82
+
83
+
### Conditional Execution with `if`
84
+
85
+
In addition to controlling when the action is triggered, you can add conditions to individual steps to control whether they run based on specific conditions like commit messages, file changes, or environment variables.
0 commit comments