|
| 1 | +# Tekton Pipeline/Task Example |
| 2 | + |
| 3 | +An example of using `promote` in a Tekton Pipeline to promote a service's config to a GitOps repository. Creation of the PipelineRun (using `service-promote-pipeline-run.yaml`) will drive the pipeline to clone, build and push the service and then promote the config from the local clone into your staging/test GitOps repository. |
| 4 | + |
| 5 | +Creation of a TaskRun (using `promote-run.yaml`) will then further promote from one GitOps repo to another, e.g. from staging to production. |
| 6 | + |
| 7 | +## Template Files |
| 8 | + |
| 9 | +- `auth.yaml`: Creates secrets for a GitHub repository and Docker registry, an access token for the GitHub repository and the ServiceAccount |
| 10 | +- `resources.yaml`: Creates PipelineResources for GitHub and Docker repositories |
| 11 | + |
| 12 | +## Other Files |
| 13 | +- `service-promote.yaml`: This is the Tekton Task used for promoting from one repository to another. It creates a PullRequest and this represents the promotion from one environment to another (for example, from development to production - in this case represented as repositories) |
| 14 | +- `service-promote-pipeline.yaml`: Creates a pipeline that executes `build-task.yaml` and `service-promote.yaml` |
| 15 | +- `service-promote-pipeline-run.yaml`: Creates a PipelineRun that executes the `service-promote-pipeline` - This will build the code and promote it to a repository you have specified |
| 16 | +- `promote.yaml`: Creates a pull request from one repository to another repository |
| 17 | +- `promote-run.yaml`: Creates a TaskRun that executes a task promoting from a testing repository to a production repository |
| 18 | +- `build-task.yaml`: This task builds a git source into a docker image and pushes to a docker registry |
| 19 | +- `gitconfig`: Data file for the configmap - includes a GitHub user ID and email address |
| 20 | + |
| 21 | +## Pre-requisites |
| 22 | + |
| 23 | +- You will need two repositories for this example, one to promote from and one to promote to |
| 24 | +- For the repository to promote from, an example can be forked from here: https://github.com/akihikokuroda/promote-demo |
| 25 | +- For the repository to promote to, an example can be forked from here: |
| 26 | +https://github.com/akihikokuroda/gitops-test |
| 27 | + |
| 28 | +## Create Tekton Resources |
| 29 | + |
| 30 | +- Create a new namespace: |
| 31 | +```shell |
| 32 | +kubectl create ns <namespace> |
| 33 | +``` |
| 34 | + |
| 35 | +- Modify your Kubernetes context to use this namespace by default: |
| 36 | +```shell |
| 37 | +kubectl config set-context --current --namespace=<namespace> |
| 38 | +``` |
| 39 | + |
| 40 | +- Apply the resources folder: |
| 41 | +```shell |
| 42 | +kubectl apply -f resources |
| 43 | +``` |
| 44 | + |
| 45 | +- Edit all files in the template folder to contain real values. Entries of the form `REPLACE_ME.x` must be replaced with the value you wish to use, i.e at occurences such as `REPLACE_ME.IMAGE_NAME`, `REPLACE_ME.GITHUB_ORG/REPLACE_ME.GITHUB_REPO` etc... There are eight instances to replace in this folder. |
| 46 | + |
| 47 | +- Apply the templates folder: |
| 48 | +```shell |
| 49 | +kubectl apply -f templates |
| 50 | +``` |
| 51 | +- Edit `gitconfig` to contain your GitHub username and email address |
| 52 | + |
| 53 | +- Create a configmap: |
| 54 | +```shell |
| 55 | +kubectl create configmap promote-configmap --from-file=gitconfig |
| 56 | +``` |
| 57 | +This will store your GitHub username and email address in key-value pairs that can be used in the PipelineRun. |
| 58 | + |
| 59 | +- Edit `service-promote-pipeline-run.yaml` to contain the name of the repository you want to promote to |
| 60 | + |
| 61 | +## Execute Pipeline |
| 62 | + |
| 63 | +`service-promote-pipeline-run` is designed to build your microservice from its development repository and then promote the new configuration to a GitOps repository (representing a different environment, for example development, staging, test or production). |
| 64 | + |
| 65 | +- Create `service-promote-pipeline-run`: |
| 66 | +```shell |
| 67 | +kubectl create -f service-promote-pipeline-run.yaml |
| 68 | +``` |
| 69 | + |
| 70 | +## Promote to Prod |
| 71 | + |
| 72 | +You can use `promote-run.yaml` to run a subsequent promote from one GitOps repository to another (e.g staging to prod) after merging the pull request on your first GitOps repository. For this you will need a third repository, and for this you can clone: https://github.com/a-roberts/gitops-repo-testing |
| 73 | + |
| 74 | +- Edit `promote-run.yaml` to contain the URL of the repository you want to promote from, and the URL of the repository you want to promote to |
| 75 | + |
| 76 | +- Create the `promote-run` TaskRun: |
| 77 | +```shell |
| 78 | +kubectl create -f promote-run.yaml |
| 79 | +``` |
| 80 | + |
| 81 | +You can view the progress of your PipelineRun using: |
| 82 | +```shell |
| 83 | +kubectl get pod <name of pod> |
| 84 | +``` |
| 85 | +Or alternatively view its progress in the Tekton Dashboard. |
| 86 | + |
| 87 | +The PipelineRun will clone the code from the inital repository locally, build it and promote it to the final repository. This will open a pull request which you will be able to view in the repository you chose to promote to. |
| 88 | + |
0 commit comments