Running Native Tests in Azure Pipeline with lib_deps to private repositories
I want to share my Azure DevOps Pipeline. My goal was to have a pipeline that executes all native tests and publishes a test and coverage report. My biggest problem was the dependency management. We are using the lib_deps entry in the ini file to manage external but also internal dependencies. The external dependencies are no problem but the internal need some kind of authentication. Since we are not using ssh to access our repositories, I needed a way to authenticate the git clone request from PlatformIO against our Azure DevOps organization.
The solution that I found is to configure git to use alternative URLs in the pipeline. For this, I inject the AccessToken in the URL during run time.
- script: |
git config --global credential.helper store
git config --global URL."https://${USER}:${PAT}@dev.azure.com".insteadOf "https://<Organization>@dev.azure.com"
displayName: 'Configure Git credentials for Azure DevOps'
env:
PAT: $(System.AccessToken)
USER: buildUser
You can use a personal access token (PAT) or the token from the system. If you are using the system one, you need to disable the “Protect access to repositories in YAML pipelines” in the pipeline settings of your project. The username can be set to any dummy name because it is not necessary for the authentication.
The following pipeline will run the tests and publish everything as reports
If a test fails, the pipeline will be marked as failed.
trigger:
- none
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
persistCredentials: true
# 1. install Python
- task: UsePythonVersion@0
inputs:
versionSpec: '3.x'
# 2. install PlatformIO
- script: |
pip install -U platformio
displayName: 'Install PlatformIO'
# 3. install Coverage-Tools
- script: |
sudo apt-get update
sudo apt-get install -y gcovr lcov
displayName: 'Install coverage tools'
# 4. set Git-Credentials
- script: |
git config --global credential.helper store
git config --global url."https://${USER}:${PAT}@dev.azure.com".insteadOf "https://<Organization>@dev.azure.com"
displayName: 'Configure Git credentials for Azure DevOps'
env:
PAT: $(System.AccessToken)
USER: buildUser
# 5. JUnit-Report for Azure DevOps
- script: |
mkdir -p test-results
platformio test -e native --without-uploading --junit-output-path test-results/results.xml
# fix encoding
sed -i "1s/encoding='utf8'/encoding='UTF-8'/" test-results/results.xml
displayName: 'Running Tests'
# 6. Generate Cobertura from gcov/gcda
- script: |
gcovr -r . --xml --xml-pretty -o coverage_cobertura.xml \\
--filter 'src'
displayName: 'Generate Cobertura XML with gcovr'
- task: PublishTestResults@2
condition: succeededOrFailed()
displayName: "Publish Test Results"
inputs:
testResultsFiles: 'test-results/results.xml'
testRunTitle: 'PlatformIO Native Tests'
failTaskOnFailedTests: true
- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: coverage_cobertura.xml
My ini looks like this:
[env:native]
platform = native
lib_archive = false
test_build_src = yes
test_framework = unity
build_flags =
-Wall
-lgcov
--coverage
-fprofile-arcs
lib_deps=
git+https://<Organization>@dev.azure.com/<Organization>/<Project>/_git/<Reposetory>
I hope this will help any one in the future.