How to make PHPUnit test and then, if successfully passed - deploy on remote server?

Hello!

Basically, trying to use Github Actions to make CI/CD on a basic skeleton Symfony project.

Current workflow:

Step 1. Checkout the code (use the standard action for checkout) - works splendidly

Step 2. run “bin/phpunit” in the runner environment - fails with insufficient access error, I am guessing it should be made executable first, to be able to run it? or maybe some other error. Did anyone succeed with running PHPUnit tests?

Step 3. make a condition that output of phpunit test contains “OK” string - documentation says there is an option to use “contains” function, but I am having trouble how to make exact syntax for this condition in the workflow

Step 3 Part 2. connect via SSH to remote host and execute a command - also no idea how to make it possible. it seems to be that I need to run either docker command or something? I am wondering if I can directly use SSH connection here? What is everyone using to make actual deployment? Most workflows in examples are CI workflows, not finding anything for CD workflows.

Current workflow, if abbreviated of details is like this:

name: CI/CD

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: test
      run: bin/phpunit
    - name: deploy if success tests
      if: steps.test.output.contains("OK")
      run: |
        ssh site.com
        cd /var/www/site && git pull origin master

Currently have the following workflow:

name: CI/CD
on:
  push:
    branches:
    - master

jobs:
  test:
    name: checkout and test
    runs-on: ubuntu-latest
    steps:
    - name: checkout project
      uses: actions/checkout@v1
    - name: composer install missing files
      run: composer install
    - name: run tests
      run: /usr/bin/php -f bin/phpunit > output.txt
    - name: save output
      uses: actions/upload-artifact@v1
      with:
        name: output
        path: output.txt
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    needs: test
    steps:
    - name: retrieve output
      uses: actions/download-artifact@v1
      with:
        name: output
    - name: check results
      run: |
        export PHPUNIT_OUTPUT = grep -q OK "output.txt"
    - name: conditional
      if: {{$PHPUNIT_OUTPUT==1}}
      run: echo test

As you can see - I solved the problem of running PHPUnit.

What I now need - somehow process the output of the PHPunit, which was stored in output.txt, and if it contains “OK” string - get a step with conditional “if” part.

Cannot figure out how to do that.

Can anyone help? I am all out of ideas what the syntax for that could be.

Regarding step 2, yes I have a working PHPUnit test suite in one of my pipelines, it can be done.

If you are getting a permission error, you indeed probably need to make the file executable. Either make the file executable in your repository:

git update-index --chmod=+x bin/phpunit

And commit/push that. Alternatively, make it executable in your workflow:

steps:
  - name: test
    run: |
      chmod +x bin/phpunit
      bin/phpunit

Or just prepend it with the PHP binary (assuming you’re running inside a PHP container):

run: php bin/phpunit

Regarding that - I was already able to run the PHPUnit tests.

In the post above - I split the workflow into 2 separate jobs - checkout & test and deploy if tests run successfully.

Currently - I am struggling with how to make the conditional deployment job.

My current idea is that I will have to create my own custom action for that, because all kinds of different attempts to get “if:(expression)” condition failed for me. Maybe I don’t get something in this whole syntax and have to go read about the background of the runners.

Personally I also split up my workflow in one that does the testing and one that does the actual deployment.

In my case, whenever a pull request is made to the master branch, the branch protection settings require the tests workflow to succeed before it can be merged. When it is merged, it it assumed to be deployable.

In my deployment workflow, I just added a condition to each step that checks if the PR was merged (because the workflow listens to ANY closed PR event, including ones that aren’t merged after all) and that works perfectly:

on:
  pull_request:
    types: [closed]
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        if: github.event.pull_request.merged
        uses: actions/checkout@v1
        with:
          ref: master

All the following steps also check for the github.event.pull_request.merged condition, so the entire workflow only runs whenever a PR was merged after tests have passed (because it cannot merge without this due to the branch protection settings).