Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 8. Triggering and modifying builds
The following sections outline how to trigger builds and modify builds using build hooks.
8.1. Build triggers Link kopierenLink in die Zwischenablage kopiert!
When defining a
BuildConfig
BuildConfig
- Webhook
- Image change
- Configuration change
8.1.1. Webhook triggers Link kopierenLink in die Zwischenablage kopiert!
Webhook triggers allow you to trigger a new build by sending a request to the OpenShift Container Platform API endpoint. You can define these triggers using GitHub, GitLab, Bitbucket, or Generic webhooks.
Currently, OpenShift Container Platform webhooks only support the analogous versions of the push event for each of the Git-based Source Code Management (SCM) systems. All other event types are ignored.
When the push events are processed, the OpenShift Container Platform control plane host confirms if the branch reference inside the event matches the branch reference in the corresponding
BuildConfig
oc new-app
oc new-build
For all webhooks, you must define a secret with a key named
WebHookSecretKey
For example here is a GitHub webhook with a reference to a secret named
mysecret
type: "GitHub"
github:
secretReference:
name: "mysecret"
The secret is then defined as follows. Note that the value of the secret is base64 encoded as is required for any
data
Secret
- kind: Secret
apiVersion: v1
metadata:
name: mysecret
creationTimestamp:
data:
WebHookSecretKey: c2VjcmV0dmFsdWUx
8.1.1.1. Prevent cluster failure due to webhooks Link kopierenLink in die Zwischenablage kopiert!
To prevent potential cluster failure and ensure pods can always start, you must configure third-party admission webhooks to exclude infrastructure namespaces. Implementing specific selectors and adopting a
ValidatingAdmissionPolicy
When possible, use a
ValidatingAdmissionPolicy
If you use admission webhooks take the following precautions:
- Configure the webhook to exclude OpenShift Container Platform and Kubernetes infrastructure namespaces.
- Configure webhook timeouts to 10 seconds or less to provide a safety buffer for the system-enforced 13-second limit.
-
Set the value to
failurePolicyfor non-critical webhooks so that requests can proceed if the webhook is unavailable.Ignore
8.1.1.2. Recovering an unstable cluster due to admission webhooks Link kopierenLink in die Zwischenablage kopiert!
If a misconfigured admission webhook causes your cluster to fail, you must delete the webhook configuration to restore functionality.
Procedure
Back up the webhook configuration. Choose either
orValidatingWebhookConfigurationfor theMutatingWebhookConfigurationvalue.<webhook_configuration>oc get <webhook_configuration> <webhook_name> -o yaml > webhook-backup.yamlDelete the webhook.
oc delete <webhook_configuration> <webhook_name>Fix the webhook configuration to exclude infrastructure namespaces when you reapply it.
Example
apiVersion: admissionregistration.k8s.io/v1 kind: MutatingWebhookConfiguration metadata: name: machine-api webhooks: - name: default.machine.machine.openshift.io rules: - apiGroups: [""] apiVersions: ["v1"] operations: ["CREATE", "UPDATE"] resources: ["pods"] scope: "*" clientConfig: service: namespace: machine-api-operator-webhook name: openshift-machine-api path: "/validate" admissionReviewVersions: ["v1"] sideEffects: None timeoutSeconds: 5 namespaceSelector: matchExpressions: - key: kubernetes.io/metadata.name operator: NotIn values: - openshift - openshift-apiserver - openshift-authentication - openshift-monitoring - kube-system - kube-public - kube-node-lease - defaultWhere
is the type of webhook configuration you are using. Valid values arekindorValidatingWebhookConfiguration.MutatingWebhookConfiguration
8.1.1.3. Using GitHub webhooks Link kopierenLink in die Zwischenablage kopiert!
GitHub webhooks handle the call made by GitHub when a repository is updated. When defining the trigger, you must specify a secret, which is part of the URL you supply to GitHub when configuring the webhook.
Example GitHub webhook definition:
type: "GitHub"
github:
secretReference:
name: "mysecret"
The secret used in the webhook trigger configuration is not the same as
secret
X-Hub-Signature
The payload URL is returned as the GitHub Webhook URL by the
oc describe
Example output
https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/github
Prerequisites
-
Create a from a GitHub repository.
BuildConfig
Procedure
To configure a GitHub Webhook:
After creating a
from a GitHub repository, run:BuildConfig$ oc describe bc/<name-of-your-BuildConfig>This generates a webhook GitHub URL that looks like:
Example output
<https://api.starter-us-east-1.openshift.com:443/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/github- Cut and paste this URL into GitHub, from the GitHub web console.
-
In your GitHub repository, select Add Webhook from Settings
Webhooks. - Paste the URL output into the Payload URL field.
-
Change the Content Type from GitHub’s default to
application/x-www-form-urlencoded.application/json Click Add webhook.
You should see a message from GitHub stating that your webhook was successfully configured.
Now, when you push a change to your GitHub repository, a new build automatically starts, and upon a successful build a new deployment starts.
NoteGogs supports the same webhook payload format as GitHub. Therefore, if you are using a Gogs server, you can define a GitHub webhook trigger on your
and trigger it by your Gogs server as well.BuildConfig
Given a file containing a valid JSON payload, such as
, you can manually trigger the webhook withpayload.json:curl$ curl -H "X-GitHub-Event: push" -H "Content-Type: application/json" -k -X POST --data-binary @payload.json https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/githubThe
argument is only necessary if your API server does not have a properly signed certificate.-k
The build will only be triggered if the
ref
ref
source.git
BuildConfig
8.1.1.4. Using GitLab webhooks Link kopierenLink in die Zwischenablage kopiert!
GitLab webhooks handle the call made by GitLab when a repository is updated. As with the GitHub triggers, you must specify a secret. The following example is a trigger definition YAML within the
BuildConfig
type: "GitLab"
gitlab:
secretReference:
name: "mysecret"
The payload URL is returned as the GitLab Webhook URL by the
oc describe
Example output
https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/gitlab
Procedure
To configure a GitLab Webhook:
Describe the
to get the webhook URL:BuildConfig$ oc describe bc <name>-
Copy the webhook URL, replacing with your secret value.
<secret> - Follow the GitLab setup instructions to paste the webhook URL into your GitLab repository settings.
Given a file containing a valid JSON payload, such as
, you can manually trigger the webhook withpayload.json:curl$ curl -H "X-GitLab-Event: Push Hook" -H "Content-Type: application/json" -k -X POST --data-binary @payload.json https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/gitlabThe
argument is only necessary if your API server does not have a properly signed certificate.-k
8.1.1.5. Using Bitbucket webhooks Link kopierenLink in die Zwischenablage kopiert!
Bitbucket webhooks handle the call made by Bitbucket when a repository is updated. Similar to the previous triggers, you must specify a secret. The following example is a trigger definition YAML within the
BuildConfig
type: "Bitbucket"
bitbucket:
secretReference:
name: "mysecret"
The payload URL is returned as the Bitbucket Webhook URL by the
oc describe
Example output
https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/bitbucket
Procedure
To configure a Bitbucket Webhook:
Describe the 'BuildConfig' to get the webhook URL:
$ oc describe bc <name>-
Copy the webhook URL, replacing with your secret value.
<secret> - Follow the Bitbucket setup instructions to paste the webhook URL into your Bitbucket repository settings.
Given a file containing a valid JSON payload, such as
, you can manually trigger the webhook withpayload.json:curl$ curl -H "X-Event-Key: repo:push" -H "Content-Type: application/json" -k -X POST --data-binary @payload.json https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/bitbucketThe
argument is only necessary if your API server does not have a properly signed certificate.-k
8.1.1.6. Using generic webhooks Link kopierenLink in die Zwischenablage kopiert!
Generic webhooks are invoked from any system capable of making a web request. As with the other webhooks, you must specify a secret, which is part of the URL that the caller must use to trigger the build. The secret ensures the uniqueness of the URL, preventing others from triggering the build. The following is an example trigger definition YAML within the
BuildConfig
type: "Generic"
generic:
secretReference:
name: "mysecret"
allowEnv: true
- 1
- Set to
trueto allow a generic webhook to pass in environment variables.
Procedure
To set up the caller, supply the calling system with the URL of the generic webhook endpoint for your build:
Example output
https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/genericThe caller must invoke the webhook as a
operation.POSTTo invoke the webhook manually you can use
:curl$ curl -X POST -k https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/genericThe HTTP verb must be set to
. The insecurePOSTflag is specified to ignore certificate validation. This second flag is not necessary if your cluster has properly signed certificates.-kThe endpoint can accept an optional payload with the following format:
git: uri: "<url to git repository>" ref: "<optional git reference>" commit: "<commit hash identifying a specific git commit>" author: name: "<author name>" email: "<author e-mail>" committer: name: "<committer name>" email: "<committer e-mail>" message: "<commit message>" env:1 - name: "<variable name>" value: "<variable value>"- 1
- Similar to the
BuildConfigenvironment variables, the environment variables defined here are made available to your build. If these variables collide with theBuildConfigenvironment variables, these variables take precedence. By default, environment variables passed by webhook are ignored. Set theallowEnvfield totrueon the webhook definition to enable this behavior.
To pass this payload using
, define it in a file namedcurland run:payload_file.yaml$ curl -H "Content-Type: application/yaml" --data-binary @payload_file.yaml -X POST -k https://<openshift_api_host:port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/genericThe arguments are the same as the previous example with the addition of a header and a payload. The
argument sets the-Hheader toContent-Typeorapplication/yamldepending on your payload format. Theapplication/jsonargument is used to send a binary payload with newlines intact with the--data-binaryrequest.POST
OpenShift Container Platform permits builds to be triggered by the generic webhook even if an invalid request payload is presented, for example, invalid content type, unparsable or invalid content, and so on. This behavior is maintained for backwards compatibility. If an invalid request payload is presented, OpenShift Container Platform returns a warning in JSON format as part of its
HTTP 200 OK
8.1.1.7. Displaying webhook URLs Link kopierenLink in die Zwischenablage kopiert!
You can use the following command to display webhook URLs associated with a build configuration. If the command does not display any webhook URLs, then no webhook trigger is defined for that build configuration.
Procedure
-
To display any webhook URLs associated with a , run:
BuildConfig
$ oc describe bc <name>
8.1.2. Using image change triggers Link kopierenLink in die Zwischenablage kopiert!
As a developer, you can configure your build to run automatically every time a base image changes.
You can use image change triggers to automatically invoke your build when a new version of an upstream image is available. For example, if a build is based on a RHEL image, you can trigger that build to run any time the RHEL image changes. As a result, the application image is always running on the latest RHEL base image.
Image streams that point to container images in v1 container registries only trigger a build once when the image stream tag becomes available and not on subsequent image updates. This is due to the lack of uniquely identifiable images in v1 container registries.
Procedure
Define an
that points to the upstream image you want to use as a trigger:ImageStreamkind: "ImageStream" apiVersion: "v1" metadata: name: "ruby-20-centos7"This defines the image stream that is tied to a container image repository located at
. The<system-registry>/<namespace>/ruby-20-centos7is defined as a service with the name<system-registry>running in OpenShift Container Platform.docker-registryIf an image stream is the base image for the build, set the
field in the build strategy to point to thefrom:ImageStreamstrategy: sourceStrategy: from: kind: "ImageStreamTag" name: "ruby-20-centos7:latest"In this case, the
definition is consuming thesourceStrategytag of the image stream namedlatestlocated within this namespace.ruby-20-centos7Define a build with one or more triggers that point to
:ImageStreamstype: "ImageChange"1 imageChange: {} type: "ImageChange"2 imageChange: from: kind: "ImageStreamTag" name: "custom-image:latest"- 1
- An image change trigger that monitors the
ImageStreamandTagas defined by the build strategy’sfromfield. TheimageChangeobject here must be empty. - 2
- An image change trigger that monitors an arbitrary image stream. The
imageChangepart, in this case, must include afromfield that references theImageStreamTagto monitor.
When using an image change trigger for the strategy image stream, the generated build is supplied with an immutable docker tag that points to the latest image corresponding to that tag. This new image reference is used by the strategy when it executes for the build.
For other image change triggers that do not reference the strategy image stream, a new build is started, but the build strategy is not updated with a unique image reference.
Since this example has an image change trigger for the strategy, the resulting build is:
strategy:
sourceStrategy:
from:
kind: "DockerImage"
name: "172.30.17.3:5001/mynamespace/ruby-20-centos7:<immutableid>"
This ensures that the triggered build uses the new image that was just pushed to the repository, and the build can be re-run any time with the same inputs.
You can pause an image change trigger to allow multiple changes on the referenced image stream before a build is started. You can also set the
paused
ImageChangeTrigger
BuildConfig
type: "ImageChange"
imageChange:
from:
kind: "ImageStreamTag"
name: "custom-image:latest"
paused: true
In addition to setting the image field for all
Strategy
OPENSHIFT_CUSTOM_BUILD_BASE_IMAGE
If a build is triggered due to a webhook trigger or manual request, the build that is created uses the
<immutableid>
ImageStream
Strategy
8.1.3. Identifying the image change trigger of a build Link kopierenLink in die Zwischenablage kopiert!
As a developer, if you have image change triggers, you can identify which image change initiated the last build. This can be useful for debugging or troubleshooting builds.
Example BuildConfig
apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
name: bc-ict-example
namespace: bc-ict-example-namespace
spec:
# ...
triggers:
- imageChange:
from:
kind: ImageStreamTag
name: input:latest
namespace: bc-ict-example-namespace
- imageChange:
from:
kind: ImageStreamTag
name: input2:latest
namespace: bc-ict-example-namespace
type: ImageChange
status:
imageChangeTriggers:
- from:
name: input:latest
namespace: bc-ict-example-namespace
lastTriggerTime: "2021-06-30T13:47:53Z"
lastTriggeredImageID: image-registry.openshift-image-registry.svc:5000/bc-ict-example-namespace/input@sha256:0f88ffbeb9d25525720bfa3524cb1bf0908b7f791057cf1acfae917b11266a69
- from:
name: input2:latest
namespace: bc-ict-example-namespace
lastTriggeredImageID: image-registry.openshift-image-registry.svc:5000/bc-ict-example-namespace/input2@sha256:0f88ffbeb9d25525720bfa3524cb2ce0908b7f791057cf1acfae917b11266a69
lastVersion: 1
This example omits elements that are not related to image change triggers.
Prerequisites
- You have configured multiple image change triggers. These triggers have triggered one or more builds.
Procedure
In
to identify thebuildConfig.status.imageChangeTriggersthat has the latest timestamp.lastTriggerTimeThis
ImageChangeTriggerStatusThen you use the `name` and `namespace` from that build to find the corresponding image change trigger in `buildConfig.spec.triggers`.-
Under , compare timestamps to identify the latest
imageChangeTriggers
Image change triggers
In your build configuration,
buildConfig.spec.triggers
BuildTriggerPolicy
Each
BuildTriggerPolicy
type
type
BuildTriggerPolicy
For image change triggers, the value of
type
ImageChange
imageChange
ImageChangeTrigger
-
: This field, which is not shown in the example, is deprecated in OpenShift Container Platform 4.8 and will be ignored in a future release. It contains the resolved image reference for the
lastTriggeredImageIDwhen the last build was triggered from thisImageStreamTag.BuildConfig -
: You can use this field, which is not shown in the example, to temporarily disable this particular image change trigger.
paused -
: You use this field to reference the
fromthat drives this image change trigger. Its type is the core Kubernetes type,ImageStreamTag.OwnerReference
The
from
kind: For image change triggers, the only supported value is ImageStreamTag. namespace
ImageStreamTag
name
ImageStreamTag
Image change trigger status
In your build configuration,
buildConfig.status.imageChangeTriggers
ImageChangeTriggerStatus
ImageChangeTriggerStatus
from
lastTriggeredImageID
lastTriggerTime
The
ImageChangeTriggerStatus
lastTriggerTime
name
namespace
buildConfig.spec.triggers
The
lastTriggerTime
ImageChangeTriggerStatus
ImageChangeTriggerStatus
name
namespace
buildConfig.spec.triggers
8.1.4. Configuration change triggers Link kopierenLink in die Zwischenablage kopiert!
A configuration change trigger allows a build to be automatically invoked as soon as a new
BuildConfig
The following is an example trigger definition YAML within the
BuildConfig
type: "ConfigChange"
Configuration change triggers currently only work when creating a new
BuildConfig
8.1.4.1. Setting triggers manually Link kopierenLink in die Zwischenablage kopiert!
Triggers can be added to and removed from build configurations with
oc set triggers
Procedure
To set a GitHub webhook trigger on a build configuration, use:
$ oc set triggers bc <name> --from-githubTo set an imagechange trigger, use:
$ oc set triggers bc <name> --from-image='<image>'To remove a trigger, add
:--remove$ oc set triggers bc <name> --from-bitbucket --remove
When a webhook trigger already exists, adding it again regenerates the webhook secret.
For more information, consult the help documentation with by running:
$ oc set triggers --help
8.2. Build hooks Link kopierenLink in die Zwischenablage kopiert!
Build hooks allow behavior to be injected into the build process.
The
postCommit
BuildConfig
The current working directory is set to the image’s
WORKDIR
The hook fails if the script or command returns a non-zero exit code or if starting the temporary container fails. When the hook fails it marks the build as failed and the image is not pushed to a registry. The reason for failing can be inspected by looking at the build logs.
Build hooks can be used to run unit tests to verify the image before the build is marked complete and the image is made available in a registry. If all tests pass and the test runner returns with exit code
0
The
postCommit
8.2.1. Configuring post commit build hooks Link kopierenLink in die Zwischenablage kopiert!
There are different ways to configure the post build hook. All forms in the following examples are equivalent and run
bundle exec rake test --verbose
Procedure
Shell script:
postCommit: script: "bundle exec rake test --verbose"The
value is a shell script to be run withscript. Use this when a shell script is appropriate to execute the build hook. For example, for running unit tests as above. To control the image entry point, or if the image does not have/bin/sh -ic, use/bin/shand/orcommand.argsNoteThe additional
flag was introduced to improve the experience working with CentOS and RHEL images, and may be removed in a future release.-iCommand as the image entry point:
postCommit: command: ["/bin/bash", "-c", "bundle exec rake test --verbose"]In this form,
is the command to run, which overrides the image entry point in the exec form, as documented in the Dockerfile reference. This is needed if the image does not havecommand, or if you do not want to use a shell. In all other cases, using/bin/shmight be more convenient.scriptCommand with arguments:
postCommit: command: ["bundle", "exec", "rake", "test"] args: ["--verbose"]This form is equivalent to appending the arguments to
.command
Providing both
script
command
8.2.2. Using the CLI to set post commit build hooks Link kopierenLink in die Zwischenablage kopiert!
The
oc set build-hook
Procedure
To set a command as the post-commit build hook:
$ oc set build-hook bc/mybc \ --post-commit \ --command \ -- bundle exec rake test --verboseTo set a script as the post-commit build hook:
$ oc set build-hook bc/mybc --post-commit --script="bundle exec rake test --verbose"