multiple listeners. TODO create different pipelines for each listener

This commit is contained in:
2020-03-18 17:36:57 +01:00
parent 6b62b0f3ef
commit 1860b861c0
17 changed files with 818 additions and 43 deletions

View File

@@ -0,0 +1,78 @@
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: jib-gradle
namespace: tekton-pipeline-1
spec:
inputs:
params:
- name: build-type
description: build type used to check to actually execute this step
default: gradle
- name: DIRECTORY
description: The directory containing the app, relative to the source repository root
default: .
- name: CACHE
description: The name of the volume for caching Gradle artifacts, local Maven repository, and base image layers
default: empty-dir-volume
- name: INSECUREREGISTRY
description: Whether to allow insecure registry
default: "false"
resources:
- name: source
type: git
outputs:
resources:
- name: image
type: image
steps:
- name: build-and-push
image: gcr.io/cloud-builders/gradle
script: |
#!/bin/sh
if [ $(inputs.params.build-type) != gradle]
then
exit 0
fi
set -o errexit
# Adds Gradle init script that applies the Jib Gradle plugin.
echo "initscript {
repositories { maven { url 'https://plugins.gradle.org/m2' } }
dependencies { classpath 'gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:+' }
}
rootProject {
afterEvaluate {
if (!project.plugins.hasPlugin('com.google.cloud.tools.jib')) {
project.apply plugin: com.google.cloud.tools.jib.gradle.JibPlugin
}
}
}" > /tekton/home/init-script.gradle
# Runs the Gradle Jib build.
gradle jib \
--stacktrace --console=plain \
--init-script=/tekton/home/init-script.gradle \
-Duser.home=/tekton/home \
-Dgradle.user.home=/tekton/home/.gradle \
-Djib.allowInsecureRegistries=$(inputs.params.INSECUREREGISTRY) \
-Djib.to.image=$(outputs.resources.image.url)
exit 0
workingDir: /workspace/source/$(inputs.params.DIRECTORY)
volumeMounts:
- name: $(inputs.params.CACHE)
mountPath: /tekton/home/.gradle/caches
subPath: gradle-caches
- name: $(inputs.params.CACHE)
mountPath: /tekton/home/.gradle/wrapper
subPath: gradle-wrapper
- name: $(inputs.params.CACHE)
mountPath: /tekton/home/.m2
subPath: m2-cache
- name: $(inputs.params.CACHE)
mountPath: /tekton/home/.cache
subPath: jib-cache
securityContext:
runAsUser: 0
volumes:
- name: empty-dir-volume
emptyDir: {}

View File

@@ -0,0 +1,45 @@
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: build-image
namespace: tekton-pipeline-1
spec:
params:
- name: DOCKERFILE
description: Path to the Dockerfile to build.
default: ./Dockerfile
- name: CONTEXT
description: The build context used by Kaniko.
default: ./
- name: EXTRA_ARGS
default: ""
- name: BUILDER_IMAGE
description: The image on which builds will run
default: gcr.io/kaniko-project/executor:v0.13.0
- name: build-type
description: build type used to check if this step needs to run without full pipeline failure
default: dockerfile
workspaces:
- name: source
mountpath: /source
outputs:
resources:
- name: image
type: image
steps:
- name: build-and-push
workingDir: /workspace/source
image: $(params.BUILDER_IMAGE)
# specifying DOCKER_CONFIG is required to allow kaniko to detect docker credential
# https://github.com/tektoncd/pipeline/pull/706
env:
- name: DOCKER_CONFIG
value: /tekton/home/.docker
command:
- /kaniko/executor
- $(inputs.EXTRA_ARGS)
- --dockerfile=/source/$(params.DOCKERFILE)
- --context=/workspace/source/$(params.CONTEXT) # The user does not need to care the workspace and the source.
- --destination=$(outputs.resources.image.url)
securityContext:
runAsUser: 0

View File

@@ -8,29 +8,28 @@ spec:
volumes:
- name: github-secret
secret:
secretName: $(inputs.params.GitHubSecretName)
inputs:
params:
- name: ExternalDomain
description: "The external domain for the EventListener e.g. `$(inputs.params.EventListenerName).<PROXYIP>.nip.io`"
- name: GitHubUser
description: "The GitHub user"
- name: GitHubRepo
description: "The GitHub repo where the webhook will be created"
- name: GitHubOrg
description: "The GitHub organization where the webhook will be created"
- name: GitHubSecretName
description: "The Secret name for GitHub access token. This is always mounted and must exist"
- name: GitHubAccessTokenKey
description: "The GitHub access token key name"
- name: GitHubSecretStringKey
description: "The GitHub secret string key name"
- name: GitHubDomain
description: "The GitHub domain. Override for GitHub Enterprise"
default: "github.com"
- name: WebhookEvents
description: "List of events the webhook will send notifications for"
default: '[\"push\",\"pull_request\"]'
secretName: $(params.GitHubSecretName)
params:
- name: ExternalDomain
description: "The external domain for the EventListener e.g. `$(params.EventListenerName).<PROXYIP>.nip.io`"
- name: GitHubUser
description: "The GitHub user"
- name: GitHubRepo
description: "The GitHub repo where the webhook will be created"
- name: GitHubOrg
description: "The GitHub organization where the webhook will be created"
- name: GitHubSecretName
description: "The Secret name for GitHub access token. This is always mounted and must exist"
- name: GitHubAccessTokenKey
description: "The GitHub access token key name"
- name: GitHubSecretStringKey
description: "The GitHub secret string key name"
- name: GitHubDomain
description: "The GitHub domain. Override for GitHub Enterprise"
default: "github.com"
- name: WebhookEvents
description: "List of events the webhook will send notifications for"
default: '[\"push\",\"pull_request\"]'
steps:
- name: create-webhook
image: pstauffer/curl:latest
@@ -44,8 +43,8 @@ spec:
- |
set -e
echo "Create Webhook"
if [ $(inputs.params.GitHubDomain) = "github.com" ];then
curl -v -d "{\"name\": \"web\",\"active\": true,\"events\": $(inputs.params.WebhookEvents),\"config\": {\"url\": \"https://$(inputs.params.ExternalDomain)\",\"content_type\": \"json\"}}" -X POST -u $(inputs.params.GitHubUser):$(cat /var/secret/$(inputs.params.GitHubAccessTokenKey)) -L https://api.github.com/repos/$(inputs.params.GitHubOrg)/$(inputs.params.GitHubRepo)/hooks
if [ $(params.GitHubDomain) = "github.com" ];then
curl -v -d "{\"name\": \"web\",\"active\": true,\"events\": $(params.WebhookEvents),\"config\": {\"url\": \"https://$(params.ExternalDomain)\",\"content_type\": \"json\"}}" -X POST -u $(params.GitHubUser):$(cat /var/secret/$(params.GitHubAccessTokenKey)) -L https://api.github.com/repos/$(params.GitHubOrg)/$(params.GitHubRepo)/hooks
else
curl -d "{\"name\": \"web\",\"active\": true,\"events\": $(inputs.params.WebhookEvents),\"config\": {\"url\": \"https://$(inputs.params.ExternalDomain)/\",\"content_type\": \"json\",\"insecure_ssl\": \"1\" ,\"secret\": \"$(cat /var/secret/$(inputs.params.GitHubSecretStringKey))\"}}" -X POST -u $(inputs.params.GitHubUser):$(cat /var/secret/$(inputs.params.GitHubAccessTokenKey)) -L https://$(inputs.params.GitHubDomain)/api/v3/repos/$(inputs.params.GitHubOrg)/$(inputs.params.GitHubRepo)/hooks
curl -d "{\"name\": \"web\",\"active\": true,\"events\": $(params.WebhookEvents),\"config\": {\"url\": \"https://$(params.ExternalDomain)/\",\"content_type\": \"json\",\"insecure_ssl\": \"1\" ,\"secret\": \"$(cat /var/secret/$(params.GitHubSecretStringKey))\"}}" -X POST -u $(params.GitHubUser):$(cat /var/secret/$(params.GitHubAccessTokenKey)) -L https://$(params.GitHubDomain)/api/v3/repos/$(params.GitHubOrg)/$(params.GitHubRepo)/hooks
fi

View File

@@ -0,0 +1,19 @@
---
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: get-build-type
namespace: tekton-pipeline-1
spec:
workspaces:
- name: source
mountpath: /source
results:
- name: build-type
description: Build type that will be used for the project
steps:
- name: check-build-type
image: bash:latest
script: |
#!/usr/bin/env bash
cat /source/build-type | tr '\n' '' | tee /tekton/results/build-type

View File

@@ -0,0 +1,97 @@
---
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: github-set-status
namespace: tekton-pipeline-1
description: |
This task will set the CI as running and add a link to the openshift console
viewer url.
spec:
params:
- name: GITHUB_HOST_URL
description: |
The GitHub host, adjust this if you run a GitHub enteprise.
default: "api.github.com"
type: string
- name: API_PATH_PREFIX
description: |
The API path prefix, GitHub Enterprise has a prefix e.g. /api/v3
default: ""
type: string
- name: REPO_FULL_NAME
description: |
The GitHub repository full name, i.e: tektoncd/catalog
type: string
- name: SHA
description: |
Commit SHA to set the status for.
type: string
- name: TARGET_URL
description: |
The target URL to associate with this status. This URL will be linked
from the GitHub UI to allow users to easily see the source of the
status.
type: string
- name: DESCRIPTION
description: |
A short description of the status.
type: string
- name: CONTEXT
description: |
The GitHub context, A string label to differentiate this status from
the status of other systems. ie: "continuous-integration/tekton"
default: "continuous-integration/tekton"
type: string
- name: STATE
description: |
The state of the status. Can be one of the following `error`,
`failure`, `pending`, or `success`.
type: string
steps:
- name: set-status
env:
- name: GITHUBTOKEN
valueFrom:
secretKeyRef:
name: github
key: token
image: registry.access.redhat.com/ubi8/ubi:latest
script: |
#!/usr/libexec/platform-python
import json
import os
import http.client
status_url = "$(params.API_PATH_PREFIX)" + "/repos/$(params.REPO_FULL_NAME)/" + \
"statuses/$(params.SHA)"
data = {
"state": "$(params.STATE)",
"target_url": "$(params.TARGET_URL)",
"description": "$(params.DESCRIPTION)",
"context": "$(params.CONTEXT)"
}
print("Sending this data to GitHub: ")
print(data)
conn = http.client.HTTPSConnection("$(params.GITHUB_HOST_URL)")
r = conn.request(
"POST",
status_url,
body=json.dumps(data),
headers={
"User-Agent": "TektonCD, the peaceful cat",
"Authorization": "Bearer " + os.environ["GITHUBTOKEN"],
})
resp = conn.getresponse()
if not str(resp.status).startswith("2"):
print("Error: %d" % (resp.status))
print(resp.read())
else:
print("GitHub status '$(params.STATE)' has been set on "
"$(params.REPO_FULL_NAME)#$(params.SHA) ")