per1234 / arduino-ci-script

Bash script for continuous integration of Arduino projects
MIT License
24 stars 7 forks source link
arduino continuous-integration test test-automation testing tests travis-ci

arduino-ci-script

Bash script for continuous integration of Arduino projects. This is currently targeted for use with Travis CI but it could be easily adapted to other purposes.

Build Status

Table of contents

Installation

The script can be used in multiple ways:

Clone the latest release

Include the latest release of the script in your project by adding the following lines to your build configuration file:

  # Clone the script repository
  - git clone --depth 1 https://github.com/per1234/arduino-ci-script.git "${HOME}/scripts"
  - cd "${HOME}/scripts"
  # Get new tags from the remote
  - git fetch --tags
  # Checkout the latest tag
  - git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
  - source "${HOME}/scripts/arduino-ci-script.sh"

Local copy

If you're passing a token to the script's publish report functions then best security practices would be to use a static copy of the script so you can be sure of the commands the token is used with:

Usage

See https://github.com/per1234/WatchdogLog/blob/master/.travis.yml for an example of the script in use.

Please configure your continuous integration system to make the minimum number of downloads and sketch verifications necessary to effectively test your code. This will prevent wasting Arduino and Travis CI's bandwidth and keep the build durations short.

set_script_verbosity SCRIPT_VERBOSITY_LEVEL

Control the level of verbosity of the script's output in the Travis CI log. Verbose output can be helpful for debugging but in normal usage it makes the log hard to read and may cause the log to exceed Travis CI's maximum log size of 4 MB, which causes the job to be terminated. The default verbosity level is 0.

set_application_folder APPLICATION_FOLDER
set_sketchbook_folder SKETCHBOOK_FOLDER
set_board_testing BOARD_TESTING

Turn on/off checking for problems with the board definition that generate a warning message during sketch verification but don't ordinarily cause it to fail, such as missing bootloader file. If this is turned on and a problem is detected the build_sketch command will return a non-zero exit status. This feature is off by default.

set_library_testing LIBRARY_TESTING

Turn on/off checking for problems with libraries that generate a warning message during sketch verification but don't ordinarily cause it to fail, such as missing or invalid items in the library.properties file. If this is turned on and a problem is detected the build_sketch command will return a non-zero exit status. This feature is off by default.

Special version names:
install_ide [IDEversionList]

Install a list of Arduino IDE version(s).

install_ide startIDEversion [endIDEversion]

Install a range of version(s) of the Arduino IDE.

install_package

"Manually" install the hardware package from the current repository. Packages are installed to $SKETCHBOOK_FOLDER/hardware. Assumes the hardware package is located in the root of the download or repository and has the correct folder structure.

install_package packageURL

"Manually" install a hardware package downloaded as a compressed file. Packages are installed to $SKETCHBOOK_FOLDER/hardware. Assumes the hardware package is located in the root of the file and has the correct folder structure.

install_package packageURL [branchName]

"Manually" install a hardware package by cloning from a Git repository. Packages are installed to $SKETCHBOOK_FOLDER/hardware. Assumes the hardware package is located in the root of the repository and has the correct folder structure.

install_package packageID [packageURL]

Install a hardware package using the Arduino IDE (Boards Manager). Only the Arduino AVR Boards package is included with the Arduino IDE installation. Packages are installed to $HOME/.arduino15/packages. You must call install_ide before this function. This feature is only available with Arduino IDE 1.6.4 and newer.

install_library

Install the library from the current repository. Assumes the library is in the root of the repository. The library is installed to the libraries subfolder of the sketchbook folder.

install_library libraryName

Install a library that is listed in the Arduino Library Manager index. The library is installed to the libraries subfolder of the sketchbook folder. You must call install_ide before this function. This feature is only available with Arduino IDE 1.6.4 and newer installed.

install_library libraryURL [newFolderName]

Download a library in a compressed file from a URL. The library is installed to the libraries subfolder of the sketchbook folder.

install_library libraryURL [branchName [newFolderName]]

Install a library by cloning a Git repository. The library is installed to the libraries subfolder of the sketchbook folder. Assumes the library is located in the root of the repository.

set_verbose_output_during_compilation verboseOutputDuringCompilation

Turn on/off arduino verbose output during compilation (same as the IDE's File > Preferences > Show verbose output during: > compilation). This will show all the commands arduino runs during the process rather than just the compiler output. This is usually not very useful output and only clutters up the log. This feature is off by default.

check_sketch_structure searchPath

Check sketches to ensure they have the correct structure.

check_library_structure basePath [depth]

Check libraries to ensure they have the correct structure. This will also run check_sketch_structure on all sketches bundled with the library.

check_library_properties searchPath [maximumSearchDepth]

Check library.properties library metadata files for errors.

check_keywords_txt searchPath [maximumSearchDepth]

Check keywords.txt files for errors.

check_library_manager_compliance libraryPath

Make some additional checks for compliance with the requirements for adding a library to the Library Manager index. This function should be used in combination with check_library_structure and check_library_properties to ensure full compliance with the requirements.

check_code_formatting strictness excludedPathList targetPath

Check code formatting for compliance with the Arduino code style. The Artistic Style formatter tool is used for this check. If it's not already installed, it will be installed to the astyle subfolder of the folder specified to set_application_folder. Note that in the Travis CI job logs, the check_code_formatting output is "folded" to make it easier to browse. You can click the triangle in the left margin of the command to unfold the output.

build_sketch sketchPath boardID allowFail IDEversion
build_sketch sketchPath boardID allowFail [IDEversionList]
build_sketch sketchPath boardID allowFail startIDEversion endIDEversion

Verify/compile sketch(es). build_sketch will echo the arduino exit status to the log, which is documented at https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc#exit-status. Note that in the Travis CI job logs, the compilation output is "folded" to make it easier to browse. You can click the triangles in the left margin to unfold.

display_report

Echo a tab separated report of all verification results to the log. The report is located in the ${HOME}/arduino-ci-script_report folder and will be named according to the build number and job number. Note that Travis CI runs each build of the job in a separate virtual machine so if you have multiple jobs you will have multiple reports. The only way I have found to generate a single report for all tests is to run them as a single job. This means not setting multiple matrix environment variables in .travis.yml's env array. See: https://docs.travis-ci.com/user/environment-variables. The report consists of one line per verification:

publish_report_to_repository REPORT_GITHUB_TOKEN repositoryURL reportBranch reportFolder doLinkComment

Add the report to a repository. See the instructions for publishing job reports for details.

publish_report_to_gist REPORT_GITHUB_TOKEN REPORT_GIST_URL doLinkComment

Add the report to the report gist. See the instructions for publishing job reports for details.

Publishing job reports

The script offers the option of publishing the job result reports to a repository or GitHub gist by using the publish_report_to_repository or publish_report_to_gist functions. This makes it easier to view the reports or to import them into a spreadsheet program. You also have the option of having the link to the reports automatically added in a comment to the commit that triggered the build. This requires some configuration, which is described in the instructions below.

NOTE: For security reasons, reports for builds of pull requests from a fork of the repository can not be published. If the owner of that fork wants to publish reports they can create a GitHub token (and gist if using publish_report_to_gist) and configure the Travis CI settings for their fork of the repository following these instructions.

Creating a GitHub personal access token

This is required for either publishing option.

  1. Sign in to your GitHub account.
  2. Click your avatar at the top right corner of GitHub > Settings > Developer settings > Personal access tokens > Generate new token.
  3. Check the appropriate permissions for the token:
    1. If using publish_report_to_gist check gist.
    2. If using publish_report_to_repository or setting the doLinkComment argument of publish_report_to_gist check public_repo (for public repositories only) or repo (for private and public repositories).
  4. Click the Generate token button.
  5. When the generated token is displayed click the clipboard icon next to it to copy the token.
  6. Open the settings page for your repository on the Travis CI website.
  7. In the Environment Variables section enter REPORT_GITHUB_TOKEN in the Name field and the token in the Value field. Make sure the Display value in build log switch is in the off position.
  8. Click the Add button.

An alternative to using a Travis CI hidden environment variable as described above is to define the GitHub personal access token as an encrypted environment variable: https://docs.travis-ci.com/user/environment-variables/#Encrypting-environment-variables.

Creating a gist

This is required for use of the publish_report_to_gist function.

  1. Open https://gist.github.com/
  2. Sign in to your GitHub account.
  3. Type an appropriate name in the Filename including extension... field. Gists sort files alphabetically so the filename should be something that will sort before the report filenames, which start at travis_ci_job_report_00001.001.tsv.
  4. Add some text to the file contents box.
  5. Click Create secret gist if you don't want the gist to be discoverable (it can still be read by anyone who knows the URL), or Create public gist to make it discoverable.
  6. Copy the URL of the gist.
  7. Open the settings page for your repository on the Travis CI website.
  8. In the Environment Variables section enter REPORT_GIST_URL in the Name field and the URL of the gist in the Value field. You can turn the Display value in build log switch to the on position. The gist URL is not secret and this will provide more information in the log.
  9. Click the Add button.

Troubleshooting

Script hangs after an arduino command

The Arduino IDE will usually try to start the GUI whenever there is an error in the command. Since the Travis CI build environment does not support this it will just hang for ten minutes until Travis CI automatically cancels the job. This means you get no useful information on the cause of the problem.

Verbose output

Verbose output results in a harder to read log so you should leave it off or minimized when possible but it can be useful for troubleshooting. Note that turning on verbose output for a large build may cause the log to exceed 4 MB, which causes Travis CI to terminate the job.

Problematic IDE versions

Some older versions of the Arduino IDE have bugs or limitations that may cause problems if used with this script:

Contributing

Pull requests or issue reports are welcome! Please see the contribution rules for instructions.