|
| 1 | +## Sample of **File-based Trigger Trigger** - Trigger an OCI DevOps Build pipeline based on a file/s update. |
| 2 | + |
| 3 | + |
| 4 | +In Oracle Cloud Infrastructure (OCI) DevOps, a build run can be automatically triggered when you commit your changes to a code repository. |
| 5 | + |
| 6 | +You can control the trigger action by specifying the modified files in your repository to be included or excluded during the build run. The file-based trigger action is applicable only for the **Push** event. |
| 7 | + |
| 8 | + |
| 9 | +### File-Based Trigger |
| 10 | +For triggering a build run based on file changes, the following two options are provided: |
| 11 | + |
| 12 | +**Files to include**: By default, changes to all files in the repository are included when a build run is triggered. The Files to include option allows you to specify a list of files and directories in the repository that you have changed and for which you want to trigger a build run. Files are specified using glob patterns. Trigger action is based on the changes affecting at least one of the included files. |
| 13 | + |
| 14 | +**Files to exclude**: By default, changes to all files in the repository are included when a build run is triggered. The Files to exclude option allows you to specify a list of files and directories in the repository that you want to exclude from the build run. Files are specified using glob patterns. Changes affecting only the excluded files do not trigger a build. If files are specified for both include and exclude, then the exclude filter is applied to the output of the include filter. |
| 15 | + |
| 16 | + |
| 17 | +#### Objectives |
| 18 | + |
| 19 | +- Create an OCI DevOps project and code repo. |
| 20 | +- Create an OCI DevOps build pipeline and trigger. |
| 21 | +- Setup file include /exclude conditions and test. |
| 22 | + |
| 23 | +* Specific instruction to clone only this example. |
| 24 | + |
| 25 | + ``` |
| 26 | + $ git init oci-devops-coderepo-filebasedtrigger |
| 27 | + $ cd oci-devops-coderepo-filebasedtrigger |
| 28 | + $ git remote add origin https://github.com/oracle-devrel/oci-devops-examples |
| 29 | + $ git config core. sparsecheckout true |
| 30 | + $ echo "oci-coderepo-examples/oci-devops-coderepo-filebasedtrigger/*">>.git/info/sparse-checkout |
| 31 | + $ git pull --depth=1 origin main |
| 32 | +
|
| 33 | + ``` |
| 34 | +
|
| 35 | +### Procedure |
| 36 | +
|
| 37 | +#### OCI Notifications. |
| 38 | +
|
| 39 | +- Create an OCI notification topic - https://docs.oracle.com/en-us/iaas/Content/Notification/Tasks/managingtopicsandsubscriptions.htm#createTopic |
| 40 | +
|
| 41 | +
|
| 42 | +#### OCI Identity setups. |
| 43 | +
|
| 44 | +- Create a dynamic group and add below rules. - |
| 45 | +
|
| 46 | +```java |
| 47 | +ALL {resource.type = 'devopsbuildpipeline', resource.compartment.id = 'COMPARMENT OCID'} |
| 48 | +ALL {resource.type = 'devopsrepository', resource.compartment.id = 'COMPARMENT OCID'} |
| 49 | +``` |
| 50 | + |
| 51 | +- Create a policy with the below statements. |
| 52 | + |
| 53 | +```java |
| 54 | +Allow dynamic-group "NAME OF THE DynamicGroup" to manage repos in compartment "COMPARTMENT NAME" |
| 55 | +Allow dynamic-group "NAME OF THE DynamicGroup" to use ons-topics in compartment "COMPARTMENT NAME" |
| 56 | +``` |
| 57 | +#### OCI DevOps setups. |
| 58 | + |
| 59 | +- Create a DevOps project and associate it with the notification topic - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_project.htm |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +- Ensure to enable logging for the projects. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +- Create an OCI Code repo - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_repo.htm |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +- Push the content to the code repo created. You may use [SSH based authorization](https://docs.oracle.com/en-us/iaas/Content/devops/using/clone_repo.htm#ssh_auth) or [HTTPS based authorization](https://docs.oracle.com/en-us/iaas/Content/devops/using/clone_repo.htm#https_auth). |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +- Create a new build pipeline - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_buildpipeline.htm |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +- Within the build pipeline, add a Managed Build stage - https://docs.oracle.com/en-us/iaas/Content/devops/using/add_buildstage.htm |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +- Provide a name and description. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +- Use select within Primary code repository and select the OCI Code repo created. |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +- Click Add and save the stage. |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +- Create a new DevOps trigger - https://docs.oracle.com/en-us/iaas/Content/devops/using/trigger_build.htm |
| 98 | + |
| 99 | +- Provide a name and optional description. |
| 100 | +- |
| 101 | + |
| 102 | + |
| 103 | +- Select the OCI code repository as the Source connection. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +- Select the code repository. |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +- Click on Add action and select the build pipeline created. |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +- Select the Event as Push. Fill the Build run conditions. |
| 116 | + |
| 117 | + |
| 118 | +#### OCI DevOps trigger conditions. |
| 119 | + |
| 120 | +- **Note** - Build run conditions |
| 121 | + |
| 122 | + - For the Push event, you can trigger the build run by specifying the source branch name and the files to be included or excluded in the build run. |
| 123 | + - (Optional) Enter the source repository branch on which the selected event triggers the build run. |
| 124 | + - If you select one of the Pull Request events, then you can also select the target code repository branch. |
| 125 | + - (Optional) Enter files to include for the trigger action. Files are specified using glob patterns. |
| 126 | + - For example, src/**/*.txt includes all text files in the "src" folder. |
| 127 | + - (Optional) Enter files to exclude the trigger action. Files are specified using glob patterns. |
| 128 | + - For example, **.html excludes all HTML files. For more examples and details. |
| 129 | + - Refer more for **Glob Patterns** - https://docs.oracle.com/en-us/iaas/Content/devops/using/glob-patterns.htm |
| 130 | + |
| 131 | +- In this sample, we will be adding the below conditions. |
| 132 | + - Source branch: main / It's optional. |
| 133 | + - File to include : *.json and dire1/*.json / Update ona file with extension .json will trigger the build pipeline. |
| 134 | + - Files to exclude : *.yaml / For a sample, we will exclude yaml file updates |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +- Click create and save the trigger. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +#### Test the conditions . |
| 144 | + |
| 145 | +- Using cloud shell or local machine, add a new value to [file. json](file. json) |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +- This will trigger a build run and once completed, you may see the changes in the executions logs. |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +- To test the exclusion, update a value with the file. yaml and validate that there is NO execution. |
| 154 | +- You can set up different trigger conditions and validate. |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +- Add new conditions, as an example here we are including all the files starting with `f` or `r` with any `extension` with the parent directory and excluding the `*.md` file. Once set up, update the files, push them to the code repo and validate the conditions. |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +Contributors |
| 164 | +=========== |
| 165 | + |
| 166 | +- Author: Rahul M R. |
| 167 | +- Collaborators: NA |
| 168 | +- Last release: September 2022 |
| 169 | + |
| 170 | +Back to examples. |
| 171 | +---- |
| 172 | + |
| 173 | +- 🍿 [Back to OCI Devops Coderepo sample](./../README.md) |
| 174 | +- 🏝️ [Back to OCI Devops sample](./../../README.md) |
| 175 | + |
0 commit comments