Working with triggers
A workflow trigger, or simply a trigger, allows you to start a workflow run automatically when certain events occur, like a code push. You might want to configure triggers to free your software developers from having to start workflow runs manually through the CodeCatalyst console.
You can use three types of trigger:
-
Push – A code push trigger causes a workflow run to start whenever a commit is pushed.
-
Pull request – A pull request trigger causes a workflow run to start whenever a pull request is either created, revised, or closed.
-
Schedule – A schedule trigger causes a workflow run to start on a schedule that you define. Consider using a schedule trigger to run nightly builds of your software so that the latest build is ready for your software developers to work on the next morning.
You can use push, pull request, and schedule triggers alone or in combination in the same workflow.
Tip
To see a trigger in action, launch a project with a blueprint. Most blueprints contain
a workflow with a trigger. Look for the Trigger
property in the blueprint's
workflow definition file. For more information about blueprints, see Creating a project with a
blueprint.
Topics
A common trigger configuration
This section describes how to set up triggers for a common software release and branching strategy.
Software release and branching strategy:
-
You have application code in a source repository.
-
Your
main
branch contains finalized code that is always release-ready. -
Your software developers make their changes in feature branches off the
main
branch. -
Your software developers create a pull request asking to merge their feature branch into
main
when their feature is ready.You want this pull request to start a workflow automatically that builds and tests—but does not deploy—the application using the files on the software developer's feature branch.
-
You software developers check the build and the tests to make sure everythying looks good. They then merge the pull request into the
main
branch.You want the merge to automatically start a workflow automatically that builds and deploys your application code.
Proposed workflow/trigger configuration:
Given the software branching strategy outlined previously, you might want to use two workflows:
-
Workflow 1 builds and tests your application when a pull request is created or revised.
-
Workflow 2 builds and deploys your application when a pull request is merged.
Workflow 1 would look like this:
Triggers: - Type: PULLREQUEST Branches: - main Events: - OPEN - REVISION Actions: BuildAction:
instructions-for-building-the-app
TestAction:instructions-for-test-the-app
The previous trigger code automatically starts a workflow run whenever a software
developer creates a pull request (or modifies
one) asking to merge their feature branch to the main
branch.
CodeCatalyst starts a workflow run using the code in the source branch (that is, the
developer's feature branch). The workflow builds and deploys the application.
Workflow 2 would look like this:
Triggers: - Type: PUSH Branches: - main Actions: BuildAction:
instructions-for-building-the-app
DeployAction:instructions-for-deploying-the-app
In the previous trigger code, when a merge to main
occurs, the
PUSH
trigger is activated. CodeCatalyst starts a workflow run using the code
in the main
branch (which now includes the code from the pull request). The
workflow builds and deploys the application.
For instructions on adding triggers to a workflow definition file, see Adding a push, pull, or schedule trigger.
For more examples of triggers and additional explanations, see Examples of triggers.
Trigger considerations when branching
This section describes some of the main considerations when setting up triggers that include branches.
-
Consideration 1: For both push and pull request triggers, if you are going to specify a branch, you must specify the destination (or 'to') branch in the trigger configuration. Never specify the source (or 'from') branch.
In the following example, a push from any branch to
main
activates the workflow.Triggers: - Type: PUSH Branches: - main
In the following example, a pull request from any branch into
main
activates the workflow.Triggers: - Type: PULLREQUEST Branches: - main Events: - OPEN - REVISION
-
Consideration 2: For push triggers, after the workflow is activated, the workflow will run using the workflow definition file and source files in the destination branch.
-
Consideration 3: For pull request triggers, after the workflow is activated, the workflow will run using the workflow definition file and source files in the source branch (even though you specified the destination branch in the trigger configuration).
-
Consideration 4: The exact same trigger in one branch might not run in another branch.
Consider the following push trigger:
Triggers: - Type: PUSH Branches: - main
If the workflow definition file containing this trigger exists in
main
and gets cloned totest
, the workflow will never start automatically using the files intest
(although you could start the workflow manually to have if use the files intest
). Review Considerations 1 and 2 to understand why the workflow will never run automatically using the files intest
.Consider also the following pull request trigger:
Triggers: - Type: PULLREQUEST Branches: - main Events: - OPEN - REVISION
If the workflow definition file containing this trigger exists in
main
, the workflow will never run using the files inmain
. (However, if you create atest
branch off ofmain
, the workflow will run using the files intest
.) Review Considerations 1 and 3 to understand why.
Adding a push, pull, or schedule trigger
Use the following instructions to add a push, pull, or schedule trigger to your workflow.
Examples of triggers
The following examples show how to add different types of triggers in the workflow definition file.
Topics
- Example: A simple code push trigger
- Example: A simple 'push to main' trigger
- Example: A simple pull request trigger
- Example: A simple schedule trigger
- Example: A trigger with a schedule and branches
- Example: A trigger with a schedule, a push, and branches
- Example: A trigger with a pull and branches
- Example: A trigger with a pull, branches, and a 'CLOSED' event
- Example: A trigger with a push, branches, and files
Example: A simple code push trigger
The following example shows a trigger that starts a workflow run whenever code is pushed to any branch in your source repository.
When this trigger is activated, CodeCatalyst starts a workflow run using the files in the branch that you're pushing to (that is, the destination branch).
For example, if you push a commit to main
, CodeCatalyst starts a workflow
run using the workfow definition file and other source files on
main
.
As another example, if you push a commit to feature-branch-123
,
CodeCatalyst starts a workflow run using the workfow definition file and other source
files on feature-branch-123
.
Triggers: - Type: PUSH
Note
If you want a workflow run to start only when you push to main
,
see Example: A simple 'push
to main' trigger.
Example: A simple 'push to main' trigger
The following example shows a trigger that starts a workflow run whenever code is
pushed to the main
branch—and only the
main
branch—in your source repository.
Triggers: - Type: PUSH Branches: - main
Example: A simple pull request trigger
The following example shows a trigger that starts a workflow run whenever a pull request is created or revised in your source repository.
When this trigger is activated, CodeCatalyst starts a workflow run using the workflow definition file and other source files in the branch that you're pulling from (that is, the source branch).
For example, if you create a pull request with a source branch called
feature-123
and a destination branch called main
,
CodeCatalyst starts a workflow run using the workfow definition file and other source
files on feature-123
.
Triggers: - Type: PULLREQUEST Events: - OPEN - REVISION
Example: A simple schedule trigger
The following example shows a trigger that starts a workflow run at midnight (UTC+0) every Monday through Friday.
When this trigger is activated, CodeCatalyst starts a single workflow run for each branch in your source repository that contains a workflow definition file with this trigger.
For example, if you have three branches in your source repository,
main
, release-v1
, feature-123
, and each
of these branches contains a workflow definition file with the trigger that follows,
CodeCatalyst starts three workflow runs: one using the files in main
, another
using the files in release-v1
, and another using the files in
feature-123
.
Triggers: - Type: SCHEDULE Expression: "0 0 ? * MON-FRI *"
For more examples of cron expressions you can use in the Expression
property, see Expression.
Example: A trigger with a schedule and branches
The following example shows a trigger that starts a workflow run at 6:15 pm (UTC+0) every day.
When this trigger is activated, CodeCatalyst starts a workflow run using the files in
the main
branch, and starts additional runs for each branch that begins
with release-
.
For example, if you have branches named main
,
release-v1
, bugfix-1
, and bugfix-2
in your
source repository, CodeCatalyst starts two workflow runs: one using the the files in
main
, and another using the files in release-v1
. It
does not start workflow runs for the bugfix-1
and
bugfix-1
branches.
Triggers: - Type: SCHEDULE Expression: "15 18 * * ? *" Branches: - main - release\-.*
For more examples of cron expressions you can use in the Expression
property, see Expression.
Example: A trigger with a schedule, a push, and branches
The following example shows a trigger that starts a workflow run at midnight
(UTC+0) every day, and whenever code is pushed to the main
branch.
In this example:
-
A workflow run starts at midnight every day. The workflow run uses the workflow definition file and other source files in the
main
branch. -
A workflow run also starts whenever you push a commit to the
main
branch. The workflow run uses the workflow definition file and other source files in the destination branch (main
).
Triggers: - Type: SCHEDULE Expression: "0 0 * * ? *" Branches: - main - Type: PUSH Branches: - main
For more examples of cron expressions you can use in the Expression
property, see Expression.
Example: A trigger with a pull and branches
The following example shows a trigger that starts a workflow run whenever someone
opens or modifies a pull request with a destination branch called main
.
Although the branch specified in the Triggers
configuration is
main
, the workflow run will use the workflow definition file and
other source files in the source branch (which is the branch
you're pulling from).
Triggers: - Type: PULLREQUEST Branches: - main Events: - OPEN - REVISION
Example: A trigger with a pull, branches, and a 'CLOSED' event
The following example shows a trigger that starts a workflow run whenever a pull
request is closed on a branch that starts with main
.
In this example:
-
When you close a pull request with a destination branch that starts with
main
, a workflow run starts automatically using the workflow definition file and other source files in the (now closed) source branch. -
If you've configured your source repository to delete branches automatically after a pull request is merged, these branches will never have the chance to enter the
CLOSED
state. This means that merged branches will not activate the pull requestCLOSED
trigger. The only way to activate theCLOSED
trigger in this scenario is to close the pull request without merging it.
Triggers: - Type: PULLREQUEST Branches: - main.* Events: - CLOSED
Example: A trigger with a push, branches, and files
The following example shows a trigger that starts a workflow run whenever a change
is made to the filename.txt
file, or any file in the src
directory, on the main
branch.
When this trigger is activated, CodeCatalyst starts a workflow run using the workflow
definition file and other source files in the main
branch.
Triggers: - Type: PUSH Branches: - main FilesChanged: - filename.txt - src\/.*