Builds

So far you’ve built the image on your own computer and pushed it. OpenShift can also build images itself, from your Git repository. That means anyone can deploy a change by pushing to Git, and builds happen in the same environment every time.

A build from Git#

oc new-build creates a BuildConfig (the instructions for a build) and an image stream for its output. Point it at your repository, and tell it to use the Containerfile:

oc new-build https://github.com/<your-account>/example-for-you.git --strategy=docker --name=example-for-you

OpenShift clones the repository, runs the Containerfile and pushes the result to the internal registry, as example-for-you:latest. The build starts straight away; follow it with:

oc logs -f buildconfig/example-for-you

Start another build at any time with:

oc start-build example-for-you --follow

The repository needs to be reachable by the cluster: public, or with a secret holding credentials (oc new-build --source-secret=...). OpenShift Local can reach GitHub as long as your computer can.

Deploying each new build#

To redeploy automatically whenever a build produces a new image, add a trigger to the Deployment, linking its container to the image stream:

oc set triggers deployment/example-for-you --from-image=example-for-you:latest -c example-for-you

Now each successful build rolls out the new image.

Building on every push#

A webhook can start a build every time you push to GitHub. oc new-build creates the BuildConfig with webhook triggers; see their URLs with:

oc describe buildconfig/example-for-you

The GitHub webhook URL ends with <secret>/github. The secret is part of the URL, so only GitHub (and you) can trigger builds. Show it with:

oc get buildconfig/example-for-you -o jsonpath='{.spec.triggers[?(@.type=="GitHub")].github.secret}'

In your GitHub repository, go to Settings, Webhooks, Add webhook, paste the URL with the secret filled in, and choose the content type application/json.

GitHub has to be able to reach the cluster’s API for this to work, so it works with a cluster on the internet, such as the Developer Sandbox, but not OpenShift Local on your own computer. Locally, run oc start-build after pushing instead.

note

Earlier notes for this guide used a hand-written BuildConfig and a GitHub personal access token as the webhook secret. oc new-build writes a correct BuildConfig for you, and the webhook secret is just a random value: it shouldn’t be a GitHub token.