Correct workflow for jenkins php app with php unit

Hi All

I would like to include php unit testing with laravel in one of the jenkins steps. I was wanting to know what is best practise.

  1. My Jenkins is hosted on a EC2 instance with php 7.3 installed.
  2. I have many different branches from bitbucket that i would need to perform testing on. Some of them use different versions of php.
  3. Composer install fails on some projects because of the PHP requirement.

I would like to do testing on commits? maybe to a candidate branch on feature merges? im not sure of best approach to do testing in general.

The project i am currently bust with is a docker image with php 8. Do i spin up this image with jenkins and perform testing? Do i build a new image the whole time on a candidate commit and do testing where it is hosted?

Hope this makes sense to a devops in what im trying to ask.

Thanks

I don’t want you to feel ignored, but I also think this deserves a longer answer which I’ll try to do in the morning if nobody else replies.

Caveat: I use github with its plugins and don’t know the bitbucket terms.

This sounds very much like you want multi branch pipelines. Which allows you to have one job that handles different branches, and cleanup and merges without having to configure each one. You can apply regexes and stuff to filter out what you don’t want

This is kinda all over the place, but I think your saying you have different PHP requirements on each branch. Does your jenkins have access do docker? then this is easy. If not, assuming linux, something like asdf works really well. If no docker and windows, then I’m out of my element.

Easiest way is to have docker manage your dependency either by 1) building the docker image and using that to run your tests, or by 2) providing the PHP version in Jenkinsfile.

pipeline {
  agent { dockerfile true }
  stages {
    stage('stage1') { 
      sh('php dothething.php')
    }
  }
}
pipeline {
  agent { docker { image 'php:7' } }
  stages {
    stage('stage1') { 
      sh('php dothething.php')
    }
  }
}

Again, caveat: linux and github, you’ll have to adjust accordingly (ex bat instead of sh for windows)

Hope that gets you started

Hey!

Thanks for your time in your detailed reply.

Yes, i have docker running in the EC2 instance alongside Jenkins so Jenkins has access. I have just installed the Docker plugin and the Docker pipeline project.

So you’re saying that i can just do this if i have a docker file?
agent { dockerfile true }

I am currently doing this which means i need an existing image to be created. Would be great if i can just use my bucket Dockerfile and then Jenkins can spin it up to do tests…


  stages {
    stage('Building') {
        steps {
            echo 'build the new nginx and php images'
        }
    }
    stage('Testing') {
      agent {
        docker {
          image '<image url: latest>'
          registryUrl '<url>'
          registryCredentialsId '<login details>'
        //   args '-u root:sudo'
        }

      }
      steps {
        echo 'Running PHP 8.1 tests inside the container...'
        sh 'php -v'
        // echo 'Installing project composer dependencies...'
        sh 'cd ${WORKSPACE} && composer install --no-progress'
        echo 'Running PHPUnit tests...'
        sh 'php ${WORKSPACE}/artisan test --coverage-html ${WORKSPACE}/report/clover --coverage-clover ${WORKSPACE}/report/clover.xml --log-junit ${WORKSPACE}/report/junit.xml'
        sh 'chmod -R a+w $PWD && chmod -R a+w ${WORKSPACE}'
        junit 'report/*.xml'
      }

And yes. Its a multi branch but i just spun up a test now to check if all is working.

When copy the pipeline into my “multi branch” project then

agent { dockerfile true } will work out the box. Thanks for showing me this.

Im not devops. Im a programmer thats had to learn this + AWS, ECS, Cloudformation for the company. It has been a steep but fun curve.