|
| 1 | +# Automerge |
| 2 | + |
| 3 | +The `promote` task initiates change by creating Pull Requests. Sometimes a team will want to merge these manually, and sometimes automation can assist. A development team may want changes to an early, shared 'dev' environment to be merged automatically, whereas changes into 'prod' will more likely require manual approval. This directory contains sample Tekton tasks that demonstrate ways to automatically merge Pull Requests. |
| 4 | + |
| 5 | +We have two subdirectories: 'standalone' and 'webhooks'. In the former case, a Tekton PipelineRun must be created manually. In the latter, the [Tekton Dashboard Webhooks Extension](https://github.com/tektoncd/experimental/tree/master/webhooks-extension) is used to start an automerge PipelineRun in response to a PullRequest arriving at a GitOps repository. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +This example is for more advanced users. Start with the [tekton-example](../tekton-example/README.md) if you're new to the tool. You should have a good understanding of the tool, its purposes and syntax before working through the 'automerge' topic. In addition to the `services` binary you will need the following tools installed: |
| 10 | + |
| 11 | +- [`kubectl`](https://kubernetes.io/docs/tasks/tools/install-kubectl/) |
| 12 | +- [`tkn`](https://github.com/tektoncd/cli) if you're not using webhooks |
| 13 | +- [`docker`](https://docs.docker.com/get-docker/) |
| 14 | + |
| 15 | +*Note* The 'standalone' code was developed on Docker Desktop and does not yet include the Role-Based Access Control configuration necessary for it to run on OpenShift or other locked-down environments. The 'webhook' code was developed on OpenShift but used a ServiceAccount that had a generous Role attached to it. Full RBAC support should be added to this example under https://github.com/rhd-gitops-example/services/issues/77. |
| 16 | + |
| 17 | +## Setup - both cases |
| 18 | + |
| 19 | +Our samples currently work with GitHub. They use the `hub` CLI to create a merge commit that when pushed, will merge the associated Pull Request. See [here](https://hub.github.com/hub-merge.1.html) for more details. |
| 20 | + |
| 21 | +## gitconfig |
| 22 | + |
| 23 | +Since we are creating git commits within the Tekton tasks, we need to know the username and email address to associate with them, as in our ['tekton-example'](../tekton-example/README.md). So, edit `gitconfig` and |
| 24 | + |
| 25 | +```sh |
| 26 | +kubectl create configmap promoteconfigmap --from-file=gitconfig |
| 27 | +``` |
| 28 | + |
| 29 | +## Dockerfile |
| 30 | + |
| 31 | +The 'hub' CLI runs in a Docker container within a Tekton Pipeline. We've provided a sample Dockerfile. If you are taking the 'webhooks' path you can comment out the section that installs `yq`. Then, |
| 32 | + |
| 33 | +```sh |
| 34 | +docker login |
| 35 | +docker build -t YOUR_DOCKER_HUB_ID/hub-test . |
| 36 | +docker push YOUR_DOCKER_HUB_ID/hub-test |
| 37 | +``` |
| 38 | + |
| 39 | +## Create a Pull Request |
| 40 | + |
| 41 | +1. Fork our example gitops repository, https://github.com/rhd-gitops-example/gitops-example-dev. |
| 42 | +1. Create a 'promotion' Pull Request either manually or using our ['tekton-example'](../tekton-example/README.md). For example you can use code of the form, |
| 43 | + |
| 44 | +```sh |
| 45 | +services promote --from promote-demo --to https://github.com/YOUR_GITHUB_ID/gitops-example-dev.git --service promote-demo |
| 46 | +``` |
| 47 | + |
| 48 | +## Generate a GitHub token |
| 49 | + |
| 50 | +You will need a GitHub token with repository access in order to merge Pull Requests. |
| 51 | + |
| 52 | +## Standalone case |
| 53 | + |
| 54 | +Edit the files in the 'standalone/templates' directory. |
| 55 | + |
| 56 | +- In `automerge-task.yaml` replace `YOUR_DOCKER_HUB_ID` with your DockerHub id. |
| 57 | +- In `git-resource.yaml` replace `YOUR_GITHUB_ID` with your GitHub id. |
| 58 | +- In `github-secret.yaml` replace `[your-github-token-with-repo-access]` with your GitHub token. |
| 59 | +- In pull-request.yaml replace YOUR_PULL_REQUEST_URL with your pull request URL, e.g. `https://github.com/mnuttall/gitops-example-dev/pull/22` |
| 60 | + |
| 61 | +Apply all the Tekton resources: |
| 62 | + |
| 63 | +```sh |
| 64 | +kubectl apply -f standalone/resources |
| 65 | +kubectl apply -f standalone/templates |
| 66 | +``` |
| 67 | + |
| 68 | +Finally start the Tekton pipeline: |
| 69 | + |
| 70 | +```sh |
| 71 | +tkn pipeline start automerge-pipeline -r source-repo=gitops-repo -r pr=pull-request -p github-config=promoteconfigmap -p github-secret=github-secret --showlog |
| 72 | +``` |
| 73 | + |
| 74 | +The pipeline will do a dry run to test that the yaml in the Pull Request is good, then merge the Pull Request and delete the branch associated with it. |
| 75 | + |
| 76 | +## Tekton Dashboard Webhooks Extension |
| 77 | + |
| 78 | +See the [Getting Started](https://github.com/tektoncd/experimental/blob/master/webhooks-extension/docs/GettingStarted.md) guide for setup guidelines. Our example uses GitHub Enterprise (GHE) and expects webhooks to be delivered to a cluster that is routeable from GHE. |
| 79 | + |
| 80 | +### Secrets and Service Accounts |
| 81 | + |
| 82 | +Set up your Service Account and Secret as per the guide above. In this case you should have, |
| 83 | + |
| 84 | +- A ServiceAccount configured for use by Tekton. |
| 85 | +- A Tekton-compatible Secret patched onto that that ServiceAccount containing your GitHub token. |
| 86 | + |
| 87 | +This secret is used in two related ways. We check the source repository out using a Tekton Git PipelineResource. This sets up `~/.gitconfig` with the credentials needed for `git push` to work. It gets these credentials from the `accessToken` field of the relevant secret patched onto the ServiceAccount running the Tekton Task. We then extract the same field and export it into the `GITHUB_TOKEN` environment variable to make `hub merge` work. Instructions for creating this secret are in the Getting Started document linked above. You should have resources of the form, |
| 88 | + |
| 89 | +```yaml |
| 90 | +--- |
| 91 | +apiVersion: v1 |
| 92 | +data: |
| 93 | + password: [base64-encoded token] |
| 94 | + username: [base64-encoded email address] |
| 95 | +kind: Secret |
| 96 | +metadata: |
| 97 | + annotations: |
| 98 | + tekton.dev/git-0: https://github.ibm.com # For example |
| 99 | + labels: |
| 100 | + serviceAccount: test-sa # As configured in the Webhooks Extension |
| 101 | + name: github-repo-access-secret |
| 102 | + |
| 103 | +--- |
| 104 | +apiVersion: v1 |
| 105 | +kind: ServiceAccount |
| 106 | +metadata: |
| 107 | + name: test-sa |
| 108 | +secrets: |
| 109 | +- name: github-repo-access-secret |
| 110 | +``` |
| 111 | +
|
| 112 | +### Edit templates |
| 113 | +
|
| 114 | +Next edit the `webhooks/templates/*` files. |
| 115 | + |
| 116 | +- In automerge-task.yaml, |
| 117 | + - replace `YOUR_DOCKER_HUB_ID` with your DockerHub id. |
| 118 | + - replace `YOUR_GHE` with your GitHub Enterprise domain. |
| 119 | +- In automerge-tt.yaml, replace YOUR_TEKTON_SERVICE_ACCOUNT with the name of your ServiceAccount used by Tekton. |
| 120 | + |
| 121 | +### Apply configuration |
| 122 | + |
| 123 | +Apply your Tekton config as usual: |
| 124 | + |
| 125 | +```sh |
| 126 | +kubectl apply -f webhooks/resources |
| 127 | +kubectl apply -f webhooks/templates |
| 128 | +``` |
| 129 | + |
| 130 | +### Set up webhook, and test |
| 131 | + |
| 132 | +Using the Tekton Dashboard webhooks extension, associate the `automerge-pipeline` with your GitOps repository. Now when a PR is raised against that repo you should see three PipelineRuns created for `automerge-pipeline`. |
| 133 | + |
| 134 | +- The first is triggered when the branch for the PullRequest is created. This runs the `echo "do nothing"` section in `automerge-task`. |
| 135 | +- The second run executes the bulk of `automerge-task`: a merge commit is created and pushed, and its associated branch deleted. |
| 136 | + |
| 137 | +TODO: |
| 138 | +A discrepancy occurs between running this task against GitHub and GitHub Enterprise. On GHE, while the commit is merged the PR remains open whereas on GitHub, it is shown as merged. A commented out section in `automerge-task` notes this, which will be investigated under https://github.com/rhd-gitops-example/services/issues/76. |
| 139 | + |
| 140 | +- Finally the third run executes `echo "kubectl apply -k env"`. Were you to remove the `echo` then this would result in the updated configuration being deployed. |
0 commit comments