2017年2月24日星期五

OpenShift_060:使用 Jenkins Pipeline 构建应用

环境:OCP 3.4

1. 下载 Jenkins 镜像 (在 MAC 机器上操作)
参考《离线安装 OCP 3.4 之 下载安装介质

docker save -o jenkins-slave.tar.gz registry.access.redhat.com/openshift3/jenkins-slave-base-rhel7 registry.access.redhat.com/openshift3/jenkins-slave-maven-rhel7 registry.access.redhat.com/openshift3/jenkins-slave-nodejs-rhel7

2. 加载 Jenkins 镜像 (在 Registry 机器上操作)

docker load -i jenkins-slave.tar.gz

docker tag registry.access.redhat.com/openshift3/jenkins-slave-base-rhel7 registry.example.com:5000/openshift3/jenkins-slave-base-rhel7
docker push registry.example.com:5000/openshift3/jenkins-slave-base-rhel7

docker tag registry.access.redhat.com/openshift3/jenkins-slave-maven-rhel7 registry.example.com:5000/openshift3/jenkins-slave-maven-rhel7
docker push registry.example.com:5000/openshift3/jenkins-slave-maven-rhel7

docker tag registry.access.redhat.com/openshift3/jenkins-slave-nodejs-rhel7 registry.example.com:5000/openshift3/jenkins-slave-nodejs-rhel7
docker push registry.example.com:5000/openshift3/jenkins-slave-nodejs-rhel7

3. 创建 myphp 应用,但是去掉自动 build 和 自动 deploy (在 Console 上操作)
参考《离线安装 OCP 3.4 之测试 PHP 应用
点击展开 Show advanced routing, build, deployment and source options,不勾选如下四项:
  • Automatically build a new image when the builder image changes
  • Launch the first build when the build configuration is created
  • New image is available
  • Deployment configuration changes

4. 从 .yml 脚本创建 pipeline

pipeline.yml 脚本内容如下:

apiVersion: v1
kind: BuildConfig
metadata:
  name: myfirstpipeline
  labels:
    name: myfirstpipeline
  annotations:
    pipeline.alpha.openshift.io/uses: '[{"name": "myphp", "namespace": "", "kind": "DeploymentConfig"}]'
spec:
  triggers:
    -
      type: GitHub
      github:
        secret: secret101
    -
      type: Generic
      generic:
        secret: secret101
  runPolicy: Serial
  source:
    type: None
  strategy:
    type: JenkinsPipeline
    jenkinsPipelineStrategy:
      jenkinsfile: "node('maven') {\nstage 'build'\nopenshiftBuild(buildConfig: 'myphp', showBuildLogs: 'true')\nstage 'deploy'\nopenshiftDeploy(deploymentConfig: 'myphp')\nopenshiftScale(deploymentConfig: 'myphp',replicaCount: '2')\n}"
  output:
  resources:
postCommit:

执行后会发现在当前 project 中,创建并启动了 jenkins 和 jenkins-jnlp pod。 

5. 选择 Builds -> Pipeline -> Start Build


查看 pipeline 构建过程,

点击 View log,登录 https://jenkins-applications.apps.example.com,查看构建日志



6. 访问 http://myphp-applications.apps.example.com/

参考文献:
1. https://blog.openshift.com/cicd-with-openshift/
2. https://github.com/VeerMuchandi/pipeline-example
3. https://github.com/OpenShiftDemos/openshift-cd-demo

没有评论: