pipeline with PHP codesniffer

Haim Paperman January 23, 2019

I have a pipeline that installs PHP codesniffer and runs a sniff against my codebase.

When I run this sniff on my server, it outputs all the errors.

When I run this sniff from bitbucket pipelines it fails the entire pipeline after bumping into the first phpcs error.

I would expect it to continue the phpcs script and show all errors and then move on to the next step of my pipeline.

 

 - step:
name: PHP CodeSniffer
caches:
- composer
script:
- set +e
- composer install
- vendor/bin/phpcs --standard=PSR2 Api/ Helper/ Model/ Plugin/ Setup/

1 answer

0 votes
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 4, 2019

Hi Haim,

When Pipelines receives a non-zero exit code, the step and pipeline are marked as failed. It is likely that sniff is returning such an exit code.

Can you attach the error message you're seeing?

You may find using an after-script useful. This will allow you to set up commands that run after the main script has finished. If the command fails the pipeline will still continue. Some testing commands return a non-zero command which may not matter in your pipeline.

Thanks,

Phil

Haim Paperman February 5, 2019

@Philip Hodder thanks for the help!

  • I added an after script, with just an echo, it did the after script but it still didn't go to the next step.

+ vendor/bin/phpcs --standard=PSR2 Api/ Helper/ Model/ Plugin/ Setup/

FILE: ...assian/pipelines/agent/build/Model/System/Message/Podisabled.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
33 | WARNING | Line exceeds 120 characters; contains 132 characters
----------------------------------------------------------------------

Time: 60ms; Memory: 6MB

+ echo "after script has run!"
after script has run!
Skipping cache upload for failed step
Searching for test report files in directories named [test-results, failsafe-reports, test-reports, surefire-reports] down to a depth of 4
Finished scanning for test reports. Found 0 test report files.
Merged test suites, total number tests is 0, with 0 failures and 0 errors.

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 5, 2019

I meant that you should add the phpcs command inside the after script.

Alternatively, you can add the following flag to your phpcs command: 

vendor/bin/phpcs --config-set ignore_warnings_on_exit 1 --standard=PSR2 Api/ Helper/ Model/ Plugin/ Setup/

 

Haim Paperman February 6, 2019

@Philip Hodder

 

Thanks again for your help - real newbie here and I'm really struggling to get it right.
I added the `--config-set ignore_warnings_on_exit 1` flag and it did indeed move on to the next step.

1. The PHPCS step is marked as passed in the pipelines - even though we know it didn't.
I'm guessing that since we told it to ignore warnings so it just marks it as passed.

2. The next step being PHPMD, and I'm back to my original problem. That step failed.

 

What am I doing wrong?

How can I get a pipeline to run through PHPCS, PHP unit test, PHPMD etc and do all of them in one pipeline and give me the actual results for fail or pass on each step?

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 6, 2019

Sorry, I didn't see PHPMD in the example you posted above? Do you have more configuration than what was in your initial post, if so can you paste it in? :)

Pipelines will automatically stop execution once a single command has failed. So it's an all or nothing deal.

One way to run multiple steps without hiding the result of the command could be to use parallel steps. But that depends on how your build process works.

pipelines:
default:
parallel:
- step:
script:
- # Run PHPCS
- step:
script:
- # Run PHPMD
- step:
script:
- # Run Unit tests
Haim Paperman February 6, 2019

@Philip Hodder any way I can DM you?

 

Here's the full pipeline - the PRIVATE_KEY & PUBLIC_KEY are my magento access keys that I didn't want to paste here


image: alexcheng/magento2
pipelines:
branches:
master:
- step:
name: Composer install
caches:
- composer
script:
- echo "{\"http-basic\":{\"repo.magento.com\":{\"username\":\"PUBLIC_KEY\",\"password\":\"PRIVATE_KEY\"}}}" > auth.json
- composer install --prefer-dist --optimize-autoloader
- step:
name: PHP CodeSniffer
caches:
- composer
script:
- composer install
- vendor/bin/phpcs --config-set ignore_warnings_on_exit 1 --standard=PSR2 Api/ Helper/ Model/ Plugin/ Setup/
- step:
name: PHP Mess Detector
caches:
- composer
script:
- composer install
- php vendor/bin/phpmd Api/,Helper/,Model/,Plugin/,Setup/ text cleancode,codesize,controversial,design,naming,unusedcode
- step:
name: PHP Copy/Paste Detector
caches:
- composer
script:
- composer install
- php vendor/bin/phpcpd Api/ Helper/ Model/ Plugin/ Setup/

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 6, 2019

Unfortunately not. But, I don't think I need direct access to your pipeline/keys to debug this. And having this available on Community will be helpful for anyone with similar issues in the future.

Thanks for sharing your YAML. Given the steps don't share state here, you should be able to run these steps in parallel.

Trying using the following configuration I've included here.

image: alexcheng/magento2
pipelines:
branches:
master:
parallel:
- step:
name: Composer install
caches:
- composer
script:
- echo "{\"http-basic\":{\"repo.magento.com\":{\"username\":\"PUBLIC_KEY\",\"password\":\"PRIVATE_KEY\"}}}" > auth.json
- composer install --prefer-dist --optimize-autoloader
- step:
name: PHP CodeSniffer
caches:
- composer
script:
- composer install
- vendor/bin/phpcs --config-set ignore_warnings_on_exit 1 --standard=PSR2 Api/ Helper/ Model/ Plugin/ Setup/
- step:
name: PHP Mess Detector
caches:
- composer
script:
- composer install
- php vendor/bin/phpmd Api/,Helper/,Model/,Plugin/,Setup/ text cleancode,codesize,controversial,design,naming,unusedcode
- step:
name: PHP Copy/Paste Detector
caches:
- composer
script:
- composer install
- php vendor/bin/phpcpd Api/ Helper/ Model/ Plugin/ Setup/

If that's roughly what you need, you can also remove the "ignore_warnings_on_exit 1" flag, as it seems to not be what you need.

Haim Paperman February 7, 2019

@Philip Hodder That still didn't work :( 

Codesniffer is only returning one out of many errors. (If i run code sniffer on my server with the exact same parameter, I get many more errors)

Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 3, 2019

Hi @Haim Paperman 

Sorry for the delay, I've been on annual leave.

Which file does it error on? Is it the first one that your server identifies?

Can I also get you to check that all the files are getting checked if we disable the failing exit code? I'm wondering if PHP CodeSniffer is only checking the first file parameter.

- step:
name: PHP CodeSniffer
caches:
- composer
script:
- composer install
- vendor/bin/phpcs --config-set ignore_warnings_on_exit 1 --config-set ignore_errors_on_exit 1 --standard=PSR2 Api/ Helper/ Model/ Plugin/ Setup/

This will make your step always green. But I'd like to see if CodeSniffer identifies all the files, or only the contents of Api/

Thanks,

Phil

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events