如何在OpenShift中获取BUILD_NUMBER并传递至应用Pod?
Got it, let's walk through how to get that build number from your OpenShift build pipeline into your app pods—this is exactly the kind of thing folks ask about when moving from Jenkins to OpenShift's native tooling. Here are the most practical methods you can use:
1. Inject BUILD_NUMBER directly into your container image during build
OpenShift automatically sets a BUILD_NUMBER environment variable in every build pod, and you can bake this right into your image so it's available to your app at runtime.
For Source-to-Image (S2I) builds
Update your BuildConfig to add the environment variable to the build strategy:
apiVersion: build.openshift.io/v1 kind: BuildConfig metadata: name: your-app-build spec: strategy: sourceStrategy: env: - name: BUILD_NUMBER valueFrom: fieldRef: fieldPath: metadata.annotations['openshift.io/build.number'] # rest of your BuildConfig...
This makes BUILD_NUMBER available in the build container. If your app reads environment variables directly, it'll carry over to the runtime pod. If you need to persist it (like in a config file), add a post-build hook to write it out:
spec: postBuild: script: | echo "BUILD_NUMBER=$BUILD_NUMBER" > /opt/app-root/src/build-info.env
For Docker builds
Use Dockerfile ARG and ENV to capture the build number:
# Define build-time argument ARG BUILD_NUMBER # Set as runtime environment variable ENV APP_BUILD_NUMBER=${BUILD_NUMBER}
Then pass the argument from your BuildConfig:
apiVersion: build.openshift.io/v1 kind: BuildConfig metadata: name: your-app-docker-build spec: strategy: dockerStrategy: buildArgs: - name: BUILD_NUMBER valueFrom: fieldRef: fieldPath: metadata.annotations['openshift.io/build.number'] # rest of your BuildConfig...
2. Reference the build number in your Deployment/DeploymentConfig
If you don't want to bake the build number into the image (maybe you want to reuse the same image across environments), you can pull it dynamically into your deployment config.
Use a ConfigMap updated by build hooks
First, add a post-build hook to your BuildConfig that creates/updates a ConfigMap with the build number:
spec: postBuild: script: | oc create configmap app-build-info \ --from-literal=build.number=$BUILD_NUMBER \ -n $(NAMESPACE) \ --dry-run=client -o yaml | oc apply -f -
Then update your DeploymentConfig/Deployment to pull the value from this ConfigMap:
apiVersion: apps.openshift.io/v1 kind: DeploymentConfig metadata: name: your-app-deployment spec: template: spec: containers: - name: your-app-container env: - name: BUILD_NUMBER valueFrom: configMapKeyRef: name: app-build-info key: build.number # rest of your DeploymentConfig...
Every time a build completes, the ConfigMap gets updated, and new pods will pick up the latest build number.
3. Use Tekton Pipelines (OpenShift Pipelines) for CI/CD workflows
If you're using OpenShift's native Tekton Pipelines instead of traditional BuildConfigs, you can pass the build number through pipeline tasks:
- In your build task, expose the build number as a result:
apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: build-app-task spec: params: - name: build-number type: string default: $(context.pipelineRun.name) # Or use a custom sequential number results: - name: app-build-number description: The build number for the app value: $(params.build-number) steps: - name: build-image image: quay.io/openshift-pipeline/s2i-nodejs:latest script: | # Your build logic here, using $(params.build-number) as needed
- In your pipeline, pass this result to the deployment task:
apiVersion: tekton.dev/v1beta1 kind: Pipeline metadata: name: app-ci-pipeline spec: tasks: - name: build-app taskRef: name: build-app-task - name: deploy-app taskRef: name: deploy-app-task params: - name: build-number value: $(tasks.build-app.results.app-build-number)
- In the deployment task, inject the build number into your Deployment as an environment variable.
Quick note
OpenShift's build system automatically adds the annotation openshift.io/build.number to every Build object—this is the source of truth for the sequential build number, equivalent to Jenkins' $BUILD_NUMBER. All the methods above pull from this value one way or another.
内容的提问来源于stack exchange,提问作者Vladimir Voitekhovski

