I wanted to run some tasks once a day. The idea being we run application to check from any drift/changes to configuration etc. Luckily this is simple in Azure devops.
We create a YAML pipeline with no trigger and create a cronjob style schedule instead as below
trigger: none
schedules:
- cron: "0 7 * * *"
displayName: Daily
branches:
include:
- main
always: true
pool:
vmImage: 'ubuntu-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '10.x'
- script: dotnet build ./tools/TestDrift/TestDrift.csproj -c Release
displayName: Test for drift
- script: |
dotnet ./tools/TestDrift/bin/Release/net10.0/TestDrift.dll
displayName: Run Test for drift
- task: PublishTestResults@1
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: ./tools/TestDrift/bin/Release/net10.0/drift-results.xml
failTaskOnFailedTests: true
In this example we’re publishing test results. Azure devops supports several formats, see the testResultsFormat variable. We’re just creating an XML file named drift-results.xml with the following format
<testsuite tests="0" failures="0">
<testcase name="check site" />
<testcase name="check pipeline">
<failure message="pipeline check failed" />
</testcase>
</testsuite>
In C# we’d do something like
var suite = new XElement("testsuite");
var total = GetTotalTests();
var failures = 0;
var testCase = new XElement("testcase",
new XAttribute("name", "check pipeline")
);
// run some test
var success = RunSomeTest();
if(!success)
{
failures++;
testCase.Add(new XElement("failure",
new XAttribute("message", "Some test name")
));
}
suite.Add(testCase);
// completed
suite.SetAttributeValue("tests", total);
suite.SetAttributeValue("failures", failures);
var exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var outputPath = Path.Combine(exeDir, "tls-results.xml");
File.WriteAllText(outputPath, suite.ToString());
Using one of the valid formats, such as the JUnit format, will also result in Azure pipeline build showing a Test tab with our test results listed.