Skip to content

Commit 969ec98

Browse files
author
Rahul MR
committed
Github repo trigger
1 parent 5ccf80b commit 969ec98

35 files changed

+190
-13
lines changed

AIO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@
3434
- Sample illustration of deployment on to *OCI Container instance* using OCI DevOps. https://github.com/oracle-devrel/oci-devops-examples/blob/main/oci-deployment-examples/oci-devops-deploy-containerinstance
3535
- Sample illustration of verifying the *integrity of Helm chart* before the deployment https://github.com/oracle-devrel/oci-devops-examples/blob/main/oci-deployment-examples/oci-deployment-with-helm-attestation
3636
- Scanning code for vulnerabilities for Maven packages - https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-devops-vulnerability-audit-management
37+
- Trigger build-pipeline based on GitHub repo file updates. - https://github.com/oracle-devrel/oci-devops-examples/blob/main/oci-coderepo-examples/oci-devops-trigger-from-github-repo
3738
- Trigger build-pipeline from OCI Code repo on file modifications . - https://github.com/oracle-devrel/oci-devops-examples/blob/main/oci-coderepo-examples/oci-devops-coderepo-filebasedtrigger

README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,11 @@ The service also provides private Git repositories to store your code and it sup
1717
- [⚡ OCI Source Code Management service - sample](./oci-coderepo-examples/README.md)
1818
- [⚡ OCI Devops Projects,Policies and Groups - sample](./oci-config-examples/README.md)
1919

20-
### Recent samples.
21-
- 🛎[Sample illustration of deployment on to *OCI Container instance* using OCI DevOps.]( https://github.com/oracle-devrel/oci-devops-examples/blob/main/oci-deployment-examples/oci-devops-deploy-containerinstance
22-
)
23-
- 🛎️[Sample illustration of verifying the *integrity of Helm chart* before the deployment](https://github.com/oracle-devrel/oci-devops-examples/blob/main/oci-deployment-examples/oci-deployment-with-helm-attestation)
24-
- 🛎️[A sample to use `Custom Shapes` within OCI Build pipelines]( https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-build-custom-shapes)
25-
- 🛎️[A sample to run OCI AI Anomaly detection from OCI Build-pipeline.](https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-devops-anomaly-detection)
26-
27-
2820
## All samples (A to Z sorted order)
29-
3021
- [🌟 All the published samples of OCI-devops](AIO.md)
31-
3222
## How to Use the repo.
33-
3423
### Option 1 - Use OCI Code Editor to Open the repo & Switch to Individual readme for details.
3524

36-
3725
[![name](images/open-in-code-editor.png)](https://cloud.oracle.com/?region=home&cs_repo_url=https://github.com/oracle-devrel/oci-devops-examples.git&cs_open_ce=true&cs_readme_path=README.md&cs_branch=main)
3826

3927

@@ -75,7 +63,7 @@ A sample to clone a build-sample with oci sonarqube integration.
7563
This project is open source. Please submit your contributions by forking this repository and submitting a pull request! Oracle appreciates any contributions that are made by the open source community.
7664

7765
## License
78-
Copyright (c) 2022 Oracle and/or its affiliates.
66+
Copyright (c) 2023 Oracle and/or its affiliates.
7967

8068
Licensed under the Universal Permissive License (UPL), Version 1.0.
8169

oci-coderepo-examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All about OCI devops coderepo samples ..
1414
* [Integrate Bitbucket.org repo with OCI Devops .](./oci-devops-coderepo-with-bitbucketcloud/)
1515
* [Integrate GITHUB.com repo with OCI Devops .](./oci-devops-coderepo-with-github/)
1616
* [Integrate GITLAB.com repo with OCI Devops .](./oci-devops-coderepo-with-gitlab/)
17+
* [Trigger Buildpipeline based on GitHub repo updates.](./oci-devops-trigger-from-github-repo)
1718

1819
</details>
1920

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
## Sample of **File-based Trigger Trigger** - Trigger an OCI DevOps Build pipeline with an external code repo (Github).
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+
### External Connection.
10+
With external connection the OCI DevOps build pipeline and triggers can connect to external repositories such as GitHub, GitLab, Bitbucket Cloud, Visual Builder Studio, Bitbucket Server, and GitLab Server.
11+
12+
In this example, we will be using a repo from the Github repo. To establish and use the external repo from Github we will be using Github PAT stored under OCI Vault.
13+
14+
### File-Based Trigger
15+
For triggering a build run based on file changes, the following two options are provided:
16+
17+
**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.
18+
19+
**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.
20+
21+
22+
#### Objectives
23+
24+
- Create GitHub PAT and store it within OCI Vault.
25+
- Create an external connection within OCI DevOps.
26+
- Create a build pipeline with an external code repo.
27+
- Create a devops trigger.
28+
- Test and validate trigger conditions.
29+
30+
31+
* Specific instruction to clone only this example.
32+
33+
```
34+
$ git init oci-devops-trigger-from-github-repo
35+
$ cd oci-devops-trigger-from-github-repo
36+
$ git remote add origin https://github.com/oracle-devrel/oci-devops-examples
37+
$ git config core. sparsecheckout true
38+
$ echo "oci-coderepo-examples/oci-devops-trigger-from-github-repo/*">>.git/info/sparse-checkout
39+
$ git pull --depth=1 origin main
40+
41+
```
42+
43+
### Procedure
44+
45+
#### OCI Notifications.
46+
47+
- Create an OCI notification topic - https://docs.oracle.com/en-us/iaas/Content/Notification/Tasks/managingtopicsandsubscriptions.htm#createTopic
48+
49+
50+
#### OCI Identity setups.
51+
52+
- Create a dynamic group and add the below rules. -
53+
54+
```java
55+
ALL {resource.type = 'devopsbuildpipeline', resource.compartment.id = 'COMPARMENT OCID'}
56+
ALL {resource.type = 'devopsrepository', resource.compartment.id = 'COMPARMENT OCID'}
57+
ALL {resource.type = 'devopsconnection',resource.compartment.id = 'compartmentOCID'}
58+
```
59+
60+
- Create a policy with the below statements.
61+
62+
```java
63+
Allow dynamic-group "NAME OF THE DynamicGroup" to manage repos in compartment "COMPARTMENT NAME"
64+
Allow dynamic-group "NAME OF THE DynamicGroup" to use ons-topics in compartment "COMPARTMENT NAME"
65+
Allow dynamic-group "NAME OF THE DynamicGroup" to read secret-family in compartment "COMPARTMENT NAME"
66+
```
67+
- If the user is not part of the `Tenancy Administrator` group, the user group needs an additional policy statement to validate the external connection. It is an optional policy statement.
68+
```java
69+
Allow group "Name of the User Group Name" to use DevOps-connection in compartment "COMPARTMENT NAME"
70+
```
71+
#### Github Initial setup.
72+
- Login to GitHub and create a new repo.
73+
- Push the repo content here to the Github repo.
74+
![](images/oci-gh-repo.png)
75+
- Refer [Github official documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) and create a personal access token, it can be a classical token or a fine-grained-token for specific repo.
76+
- Make a note of GitHub PAT.
77+
#### OCI Vault setup.
78+
- Create a vault - https://docs.oracle.com/en-us/iaas/Content/KeyManagement/Tasks/managingvaults_topic-To_create_a_new_vault.htm#createnewvault
79+
- Create a master key - https://docs.oracle.com/en-us/iaas/Content/KeyManagement/Tasks/managingkeys_topic-To_create_a_new_key.htm#createnewkey
80+
- Create a secret and add the GitHub PAT value.
81+
![](images/oci-vault-secret.png)
82+
#### OCI DevOps setups.
83+
- Create a DevOps project and associate it with the notification topic - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_project.htm
84+
![](images/oci-devops-project.png)
85+
- Ensure to enable logging for the projects.
86+
- Create an `External Connection`. - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_connection.htm
87+
- Open the navigation menu and click Developer Services. Under DevOps, click Projects.
88+
- Select a project and from the left-side menu, click External Connections.
89+
- Click Create External Connection.
90+
![](images/oci-create-ec.png)
91+
- Provide a `name` and `description`.Select the type `Github`.
92+
![](images/oci-create-ec-step1.png)
93+
- Select the `Vault` created and the `secret` which contains the PAT.
94+
- Click `Validate connection` and verify the connection is valid
95+
![](images/oci-connection-validation.png)
96+
- If the validation is successful, then a success message is displayed. Otherwise, a failure message is displayed. If the validation fails, you must generate a PAT and store your PAT securely in an OCI vault. You can then update the vault secret by editing the connection details.
97+
- Create a `Build pipeline`.
98+
![](images/oci-create-buildpipeline.png)
99+
- With in build pipeline add a `managed build stage`.
100+
- Provide a name and description for the stage.
101+
- Provide path `build-pipeline/build_spec.yaml` as the path for `build spec file path`.
102+
![](images/oci-managed-buildstage-config.png)
103+
- Select the primary code repository, select the connection type as `Github` and the `External connection` created.
104+
- Select the appropriate GitHub repo and branch.
105+
![](images/oci-build-stage-details.png)
106+
- Once the repo is selected create the stage. The build pipeline will look as below.
107+
![](images/oci-buildpipeline.png)
108+
#### OCI DevOps trigger conditions with External connection.
109+
In Oracle Cloud Infrastructure (OCI) DevOps, a build run can be automatically triggered when you commit your changes to a code repository. In the DevOps service, you can create your private code repositories or connect to external code repositories and trigger the build
110+
* Open the navigation menu and click Developer Services. Under DevOps, click Projects.
111+
* On the DevOps Projects page, select a project.
112+
* On the details page of the project, from the left side, click Triggers.
113+
* Click Create Trigger.
114+
![](images/oci-create-trigger.png)
115+
- Enter a name select the external connection as Github and select the connection created.
116+
![](images/oci-trigger-details-1.png)
117+
- With in action select the `Build pipeline` and event type as `push`.
118+
![](images/oci-trigger-buildpipeline.png)
119+
![](images/oci-trigger-event-selection.png)
120+
- Select the `branch`,` files to include` and `files to exclude`.In this sample, we will add the trigger for path `build-pipelines/build_spec.yaml` and exclude all *.md file changes.
121+
![](images/oci-add-build-conditions.png)
122+
- Add the actions and create the trigger. Copy the URL and secrets that will be prompted on the screen.
123+
![](images/oci-trigger-secrets.png)
124+
125+
#### GitHub Webhook configurations.
126+
- Follow GitHub official documentation to create the webhook against the repo - https://docs.github.com/en/webhooks/using-webhooks/creating-webhooks
127+
![](images/oci-github-webhook.png)
128+
- Within webhook use the URL received and secrets from OCI DevOps trigger as Payload URL and Secret respectively. Use `application/json as the content type.
129+
![](images/oci-github-webhook-config.png)
130+
- As soon as the webhook is created, a test ping event will be triggered from GitHub using the payload URL and secrets. The details can be found in the `recent deliveries` tab against the webhook created.
131+
![](images/oci-gh-recent-deliveries.png)
132+
![](images/oci-gh-ping-check.png)
133+
134+
#### Test & Validate the Triggers
135+
- Update the file `build-pipeline/build_spec.yaml` add an echo statement to the end of the steps and commit to the repo.
136+
![](images/oci-update-bs.png)
137+
- Check for the recent deliveries under webhook for the `push ' action.
138+
![](images/oci-gh-second-delivery.png)
139+
- Verify the request and response to validate the trigger.
140+
![](images/oci-gh-second-del-request-response.png)
141+
- With in build pipeline, check for build history and you should see a new build run.
142+
![](images/oci-build-history-details.png)
143+
- To validate the file-based trigger file exclusion, edit and add some comments to the `README.md`.Commit the changes back to the repo.
144+
![](images/oci-update-rm.png)
145+
- Verify the webhook under `recent deliveries`.It should show a response code for `202` as there is no valid condition (due to exclusion) to trigger a build run.
146+
![](images/oci-update-rm-webhook-response.png)
147+
- Incas of a failure of webhook, validate it via request and response to know more.
148+
![](images/oci-gh-failed-deliveries.png)
149+
150+
Contributors
151+
===========
152+
- Author: Rahul M R.
153+
- Collaborators: NA
154+
- Last release: September 2023
155+
Back to examples.
156+
----
157+
- 🍿 [Back to OCI Devops Coderepo sample](./../README.md)
158+
- 🏝️ [Back to OCI Devops sample](./../../README.md)
159+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: 0.1
2+
component: build
3+
timeoutInSeconds: 6000
4+
runAs: root
5+
shell: bash
6+
env:
7+
# these are local variables to the build config
8+
variables:
9+
code_repo_name: "oci-java-micronaut-devops-example"
10+
11+
# the value of a vaultVariable is the secret-id (in OCI ID format) stored in the OCI Vault service
12+
# you can then access the value of that secret in your build_spec.yaml commands
13+
vaultVariables:
14+
# EXAMPLE_SECRET: "YOUR-SECRET-OCID"
15+
16+
# exportedVariables are made available to use as parameters in sucessor Build Pipeline stages
17+
# For this Build to run, the Build Pipeline needs to have a BUILDRUN_HASH parameter set
18+
exportedVariables:
19+
- BUILDRUN_HASH
20+
21+
steps:
22+
- type: Command
23+
name: "Define unique image tag"
24+
timeoutInSeconds: 40
25+
command: |
26+
export BUILDRUN_HASH=`echo ${OCI_BUILD_RUN_ID} | rev | cut -c 1-7`
27+
echo "BUILDRUN_HASH: " $BUILDRUN_HASH
28+
echo "Testing Trigger From Github"
97.8 KB
Loading
85.1 KB
Loading
249 KB
Loading
50.7 KB
Loading
138 KB
Loading

0 commit comments

Comments
 (0)