如何通过jenkins配置文件提供openshift环境变量?

时间:2019-04-29 18:15:09

标签: jenkins environment-variables openshift

我正在尝试通过jenkinsfile更新openshift中的环境变量。我可以通过以下命令来更新环境变量,但是该命令的问题是我必须在jenkinsfile中输入所有变量,因此它不安全,并且jenkinsfile有时看起来很长很丑。

“命令” oc set env dc / prj -e te1 = tes -e tes2 = tes2 Ntes = ntes“”

通常,对于其他服务器,我曾经在jenkins的配置文件中添加所有环境变量,然后将配置文件直接传递给服务器。

只是想知道是否有人做了什么事情,可以使用jenkins配置文件通过jenkinsfile将所有环境变量传递给openshift容器。

谢谢

2 个答案:

答案 0 :(得分:0)

如果您想像以前一样使用文件中的方法,则可以将其分为两个步骤:

  1. 从文件创建configmap: oc create configmap my-config --from-file=path/to/bar
  2. 通过configmap设置环境变量: oc set env --from=configmap/my-config dc/prj

答案 1 :(得分:0)

我更喜欢使用 Openshift模板进行部署或进行更改。这种方式有助于对基础结构进行编码,基本上是用于版本控制的基础结构代码,并有助于在环境之间复制相同的内容。

例如,考虑以下部署模板

$ cat deployment.yaml 
apiVersion: v1
items:
- apiVersion: v1
  kind: DeploymentConfig
  metadata:
    annotations:
      openshift.io/generated-by: OpenShiftNewApp
    creationTimestamp: null
    labels:
      app: "${APPLICATION_NAME}"
    name: "${APPLICATION_NAME}"
  spec:
    replicas: 1
    selector:
      app: "${APPLICATION_NAME}"
      deploymentconfig: "${APPLICATION_NAME}"
    strategy:
      resources: {}
      rollingParams:
        intervalSeconds: 1
        maxSurge: 25%
        maxUnavailable: 25%
        timeoutSeconds: 600
        updatePeriodSeconds: 1
      type: Rolling
    template:
      metadata:
        annotations:
          openshift.io/generated-by: OpenShiftNewApp
        creationTimestamp: null
        labels:
          app: "${APPLICATION_NAME}"
          deploymentconfig: "${APPLICATION_NAME}"
      spec:
        containers:
        - env:
           - name: param1
             value: value1
           - name: param2
             value: value2
          image: "${DOCKER_IMAGE}"
          imagePullPolicy: Always
          name: "${APPLICATION_NAME}"
          ports:
            - containerPort: PORT1
              protocol: TCP
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: ${HEALTHCHECK_PATH}
              port: ${LIVENESS_PORT} 
              scheme: HTTPS
            initialDelaySeconds: 300
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 30
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: ${HEALTHCHECK_PATH}
              port: ${READINESS_PORT}
              scheme: HTTPS
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 30
          resources:
            limits:
              cpu: ${CPU_MAX}
              memory: ${MEMORY_MAX}
            requests:
              cpu: ${CPU_DEFAULT}
              memory: ${MEMORY_DEFAULT}
          terminationMessagePath: /dev/termination-log
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        securityContext: {}
        terminationGracePeriodSeconds: 30
    test: false
    triggers:
    - type: ConfigChange
  status: {}
kind: List
metadata: {}

上面的模板中有 2个变量,并且使用命令oc apply -f deployment.yaml创建了部署配置

- name: param1
  value: value1
- name: param2
  value: value2

现在,要在Openshift中更新环境变量,我建议像这样在模板中添加新变量:

- name: param1
  value: value1
- name: param2
  value: value2
- name: param3
  value: value3

使用相同的命令 oc apply -f deployment.yaml更新新的环境变量,或者可以使用相同的命令来更新现有参数。

Openshift仅在配置发生更改的情况下才可以更新配置。这样,不仅可以更新您的环境变量,而且可以更新任何其他配置,例如CPU,内存,副本,泊坞窗映像等。

希望这会有所帮助!