CI.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. Continuous Integration
  2. ========================
  3. Continuous integration refers to an active code base where multiple developers are integrating their changes continuously into a single cohesive project. One of the biggest keys to making continuous integration work is testing before integrating! Tripal Test Suite makes developing tests using PHPUnit much easier and this tutorial will show you how to ensure those tests are run every time you push to GitHub!
  4. Using GitHub Workflows
  5. ------------------------
  6. First, what is a GitHub Workflow? From GitHub, "a workflow is a configurable automated process made up of one or more jobs". I like to think of it as configuring a bioinformatics pipeline. For the purposes of this tutorial, we are telling GitHub how to execute our PHPUnit tests automatically.
  7. You can learn more about `GitHub Action Workflows from GitHub directly <https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/introduction-to-github-actions>`_. We will cover it breifly here specifically for Tripal PHPUnit testing but the official tutorial is still recommended.
  8. Telling GitHub about your workflow
  9. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. This is as simple as creating your workflow file in the right place. You want to create a new file named `phpunit.yml` at `.github/workflows/` in your repository. GitHub will automatically know this file describes a workflow and will execute the workflow based on the configuration you provide.
  11. Configuring a workflow to run PHPUnit tests
  12. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13. All configuration will be done in the file created in step 1.
  14. First we provide the name, when we want to run the workflow (i.e. on push and PRs) and the basic structure of a workflow:
  15. .. code-block:: yaml
  16. name: PHPUnit
  17. # Controls when the workflow will run.
  18. # Run this workflow every time a new commit is pushed to your repository
  19. on: [push, pull_request]
  20. jobs:
  21. # This key is the name of the job.
  22. run-tests:
  23. # The type of system that the job will run on.
  24. runs-on: ubuntu-latest
  25. We want our tests to be run twice, one for PHP 7.1 and another for PHP 7.1. This is done by specifing a matrix build which can be done as follows:
  26. .. code-block:: yaml
  27. jobs:
  28. # This key is the name of the job.
  29. run-tests:
  30. # The type of system that the job will run on.
  31. runs-on: ubuntu-latest
  32. # Matrix Build for this job.
  33. strategy:
  34. matrix:
  35. php-versions: ['7.1', '7.2']
  36. # Name the matrix build so we can tell them apart.
  37. name: PHPUnit Testing (PHP ${{ matrix.php-versions }})
  38. We also want to tell GitHub to setup a PostgreSQL server for us. This is done using services:
  39. .. code-block:: yaml
  40. jobs:
  41. # This key is the name of the job.
  42. run-tests:
  43. # The type of system that the job will run on.
  44. runs-on: ubuntu-latest
  45. # Service containers to run with `run-tests`
  46. services:
  47. # Label used to access the service container
  48. postgres:
  49. # Docker Hub image
  50. image: postgres
  51. env:
  52. POSTGRES_USER: tripaladmin
  53. POSTGRES_PASSWORD: somesupersecurepassword
  54. POSTGRES_DB: testdb
  55. # Set health checks to wait until postgres has started
  56. options: >-
  57. --health-cmd pg_isready
  58. --health-interval 10s
  59. --health-timeout 5s
  60. --health-retries 5
  61. ports:
  62. # Maps tcp port 5432 on service container to the host
  63. - 5432:5432
  64. Finally we can actually tell GitHub what steps we want to run using this beautiful container we have setup! We want to:
  65. .. code-block:: yaml
  66. jobs:
  67. # This key is the name of the job.
  68. run-tests:
  69. # The type of system that the job will run on.
  70. runs-on: ubuntu-latest
  71. steps:
  72. # 1) Checkout the repository and setup workspace.
  73. - uses: actions/checkout@v2
  74. # 2) Setup PHP according to the version passed in.
  75. - name: Setup PHP
  76. uses: shivammathur/setup-php@v2
  77. with:
  78. php-version: ${{ matrix.php-versions }}
  79. extensions: mbstring, intl, php-pgsql, php-gd, php-xml
  80. ini-values: memory_limit=2G
  81. coverage: xdebug
  82. tools: composer, phpunit
  83. # 3) Install Drush/Drupal/Tripal
  84. - name: Setup Drush, Drupal 7.x, Tripal 3.x
  85. id: tripalsetup
  86. uses: tripal/setup-tripal-action@7.x-3.x-1.0
  87. with:
  88. postgres_user: tripaladmin
  89. postgres_pass: somesupersecurepassword
  90. postgres_db: testdb
  91. # 4) Install Tripal Extension Module.
  92. - name: Install Tripal Extension Module
  93. id: installextension
  94. env:
  95. DRUSH: ${{ steps.tripalsetup.outputs.drush_path }}
  96. DRUPAL_ROOT: ${{ steps.tripalsetup.outputs.drupal_root }}
  97. run: |
  98. mkdir -p $DRUPAL_ROOT/sites/all/modules/example_module
  99. cp -R * $DRUPAL_ROOT/sites/all/modules/example_module
  100. cd $DRUPAL_ROOT
  101. $DRUSH en -y example_module
  102. # 5) Runs the PHPUnit tests.
  103. # https://github.com/mheap/phpunit-github-actions-printer is used
  104. # to report PHPUnit fails in a meaningful way to github in PRs.
  105. - name: PHPUnit Tests
  106. env:
  107. DRUSH: ${{ steps.tripalsetup.outputs.drush_path }}
  108. DRUPAL_ROOT: ${{ steps.tripalsetup.outputs.drupal_root }}
  109. run: |
  110. cd $DRUPAL_ROOT/sites/all/modules/example_module
  111. composer require --dev mheap/phpunit-github-actions-printer --quiet
  112. composer update --quiet
  113. ./vendor/bin/phpunit --printer mheap\\GithubActionsReporter\\Printer
  114. In step 4 above, I have provided an example of what installing your extension module might look like. The run section will need to be modified according to your module and should include downloading and installing any dependencies, applying any patches and installing your module. If your tests require configuration then that should also be included here.
  115. In step 5 we run our PHPUnit tests! All you need to change here is the directory name for your module. The `mheap\\GithubActionsReporter\\Printer` will ensure any errors reported by PHPUnit are shown on your PR and Action summary.
  116. All steps before step 4 should be generic for any extension module! You can find the full configuration file on the `README of the SetupTripalAction <https://github.com/tripal/setup-tripal-action#usage>`_.
  117. Checking your Action
  118. ----------------------
  119. Everytime you push commits to your repository and when you create a pull request, your action will be run. Thus to test your action, commit your phpunit.yml file created above to trigger the GitHub action. Then click on "Actions" at the top of your repository to see it in progress.
  120. If you created a pull request, you will see your workflow action in the checks section at the bottom of your pull request. From here you can click on Details to see the full running of the job.
  121. Adding the Badge to your README
  122. ---------------------------------
  123. Click on Actions at the top of your repository, then click on one of the PHPUnit jobs which have already been submitted. This brings you to the job summary page where you will see a button with three dots in the top right corner. Click on this and then "Create status badge" to get the markdown to add to your README.
  124. .. image:: CI.create-badge.png