美文网首页
pipeline方式jenkins+phing - master

pipeline方式jenkins+phing - master

作者: 芒鞋儿 | 来源:发表于2021-02-24 00:58 被阅读0次
  1. 搭建并启动 jenkins
  2. 进入jenkins server, install php, phing,phpunit
    (debian 下,使用apt-get, 进入的方式用root 进入,
    docker 方式用root 进入:
docker exec -u 0 -it <container id> /bin/bash

如果是server, 使用ssh 方式root权限的user进入)

  1. source中配置好phing 所使用的build.xml
    如果由phing 驱动phpunit,则也需要配置好phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- ユニットテスト -->
<project name="test-repo-by-phing" default="build">
  <property name="outputDir" value="report" override="false"/>

  <!-- 初期設定 -->
  <target name="prepare">
    <echo msg="初期設定" />
  </target>


  <!-- ユニットテスト -->
  <target name="test">
    <exec executable= "vendor/bin/phpunit">
      <arg line= "--colors=auto --coverage-clover ${outputDir}/coverage.xml --log-junit ${outputDir}/phpunit.xml" />
    </exec>
  </target>

  <target name="build" depends="prepare, test" />

</project>

phpunit.xml 省略

  1. 配置jenkins
    new item-> job name->github url-> pipeline script
pipeline {
    agent any
    triggers {
        // 毎日22時に実行(変更箇所)
        pollSCM('H/15 * * * *')
    }
    // 環境変数を設定します
    environment {
        //Gitlabのプロジェクトのgit URL (変更箇所)
        SCMURL = 'https://github.com/xieheng0915/test-repo-for-jenkins.git'
    }
    // 処理
    stages {
        // ビルド
        stage('checkout') {
            steps {
                // ソースのチェックアウト
                git url: SCMURL
            }
        }
        // ユニットテスト実行
        stage('test-phing') {
            steps {
                // composerのユニットテストイベントを呼び出す
                sh 'phing'
            }
        }
        
    }
    post {
        // stagesの処理が成功した場合に実行する処理
        success {
            // ワークスペースをクリーン
            cleanWs()
        }
    }
}

_ 注意1:这里的配置post action为清除workspace, 该workspace 在jenkins server下 /var/jenkins_home/workspace/<your job name> _
_ 注意2: pipeline方式下默认pull github 的master branch,如果需要其他branch 需要加入branch 选项。_
结果如下:

第三次的pipeline 执行结果成功

但是如果去jenkins server下workspace 查看,没有report

  1. 如果需要保留report,把上面cleanWs()注释掉
    再进入workspace 就可以看到结果:
root@2187f55e86c3:/var/jenkins_home/workspace# ls phing-pipeline-job
app  build.xml  composer.json  composer.lock  phpunit.xml  pipeline_script  README.md  report  results  test
root@2187f55e86c3:/var/jenkins_home/workspace# cd phing-pipeline-job
root@2187f55e86c3:/var/jenkins_home/workspace/phing-pipeline-job# ls report
phpunit.xml
root@2187f55e86c3:/var/jenkins_home/workspace/phing-pipeline-job# ls results
tmp.md
root@2187f55e86c3:/var/jenkins_home/workspace/phing-pipeline-job# cd report/
root@2187f55e86c3:/var/jenkins_home/workspace/phing-pipeline-job/report# ls
phpunit.xml
root@2187f55e86c3:/var/jenkins_home/workspace/phing-pipeline-job/report# cat phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="App Tests" tests="6" assertions="6" errors="0" failures="0" skipped="0" time="0.008745">
    <testsuite name="CalculatorTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/CalculatorTest.php" tests="5" assertions="5" errors="0" failures="0" skipped="0" time="0.008303">
      <testsuite name="CalculatorTest::testAddNumbers" tests="4" assertions="4" errors="0" failures="0" skipped="0" time="0.006963">
        <testcase name="testAddNumbers with data set #0" class="CalculatorTest" classname="CalculatorTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/CalculatorTest.php" line="23" assertions="1" time="0.006751"/>
        <testcase name="testAddNumbers with data set #1" class="CalculatorTest" classname="CalculatorTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/CalculatorTest.php" line="23" assertions="1" time="0.000097"/>
        <testcase name="testAddNumbers with data set #2" class="CalculatorTest" classname="CalculatorTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/CalculatorTest.php" line="23" assertions="1" time="0.000057"/>
        <testcase name="testAddNumbers with data set #3" class="CalculatorTest" classname="CalculatorTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/CalculatorTest.php" line="23" assertions="1" time="0.000058"/>
      </testsuite>
      <testcase name="testThrowExceptionIfNonNumbericIsPassed" class="CalculatorTest" classname="CalculatorTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/CalculatorTest.php" line="32" assertions="1" time="0.001340"/>
    </testsuite>
    <testsuite name="GumballMachineTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/GumballMachineTest.php" tests="1" assertions="1" errors="0" failures="0" skipped="0" time="0.000442">
      <testcase name="testTurnWheel" class="GumballMachineTest" classname="GumballMachineTest" file="/Users/xieheng/gitspace/azurespace/test-repo-for-jenkins/test/GumballMachineTest.php" line="10" assertions="1" time="0.000442"/>
    </testsuite>
  </testsuite>
</testsuites>
root@2187f55e86c3:/var/jenkins_home/workspace/phing-pipeline-job/report# 

这篇是关于如何建api token 的

遗留课题:
1.如何把结果保留并显示到jenkins页面上?

  1. 如何保留结果并自动清理ws?

测试用代码:
https://github.com/xieheng0915/test-repo-for-jenkins

相关文章

网友评论

      本文标题:pipeline方式jenkins+phing - master

      本文链接:https://www.haomeiwen.com/subject/ojrjfltx.html