How to run Console Applications on Azure Pipelines

Learn how to run console apps on Azure DevOps without hosting!

I came across multiple similar questions on StackOverflow for running a Console Application on Microsoft Azure. Answers mostly suggesting running the console app on WebJobs. Here I’m sharing another way of running console apps on Microsoft Azure DevOps, with Azure Pipelines.

[Azure]: Run your console app on Azure Pipelines

If you have a subscription to Azure DevOps, you will be able to run your console app. Upload your console app and create a pipeline to run a script. Add the command and necessary arguments if any and run the pipeline.

Note: At the time of writing this post I'm not sure of any downside of this approach, will update here if I find one.

What did I do?

In Azure DevOps, under one of demo Organizations I created a test project (“TestProject”), and initialized the empty repository with README.md file.

empty repository initialized with README.md file

Created a new C# Console App Project in Visual Studio 2019. “HelloWorld-Console”.

HelloWorld-Console app

Published the executable. While publishing, I changed the deployment mode to “Self-contained” and enabled “Produce single file” option. It produced 2 files in the publish folder:

  • HelloWorld-Console.exe, and
  • HelloWorld-Console.pdb

publish options
publish output

since .pdb file file contains debugging info, I had my concerns only with the executable (.exe) file.

I tried uploading the file in the Azure DevOps repository directly from the browser but the browser has a limitation of 20 MB max per file. So I chose to upload it using the git tools.

I cloned the repository on my machine, copied the executable (.exe) in the repository from the publish folder, staged the changes, committed and pushed it to remote repository.

Uploading the Console App to Azure DevOps repository

Finally, I setup a pipeline with a “Command Line Script” task to run the executable. Since it’s a Windows executable file, I set the Agent specification to “Windows-2019”.

In the “Script” block I just entered the file name of console app as it didn’t require any additional parameter to run.

Pipeline with “Command Line Script” task
Agent Specification set to “Windows-2019” for the pipeline

The pipeline ran just fine and I could see the output from the console app in the logs: ?

Command Line Task output in the pipeline log

In the pipeline, I extended the command to save the console app output to a text file. I added one more task to publish the output file in artifacts directory. I kept the artifact’s name as “pipeline-run-$(Build.BuildNumber)” in order to have different folder for every run, containing the output text file.

Extended the Command Line Script’s script to save the output in helloworld-output.txt

added “Publish Pipeline Artifact” task to publish the console app output file in Artifacts folder.

Now for every run of the pipeline, I could see an artifact produced, containing the output text file.

successful run with an artifact
Artifact
the output text file contains the expected “Hello World!”

So, with that I’m wrapping this post but you can always extend the pipeline with tasks to share the console app’s output to email, or other channels. This way you can run your console apps without an App Service/WebJob/Azure Functions. The Azure DevOps platform will run the console app for you for free I guess, until they start charging. ?

Further, you can extend the pipeline with scheduled trigger to run it on scheduled time as well.

? Did I miss anything or you have any feedback? Please help me fix it by dropping a line at [email protected], thanks!

Happy Learning!
/Sunny Sharma
devsdaily.com

3 thoughts on “How to run Console Applications on Azure Pipelines”

  1. I have done something similar like this but instead of using command line I used DotnetRun command for my exe or project dll file and the output is printed. But, the next stage is taking input in console to run the program. How that can be achieved?
    for example

    for(int i=0;i<100;i++)
    {

    Console.Writeline("Hello word"+i);
    }

    Console.ReadLine();

    Now in the above code …Loop works properly but in pipeline program gets stuck as the point when we use Console.Readline(). We need to cancel the run in that case.

    I woud apppreciate If you can shade some ligth in this.

    Regards,
    Bhushan

    Reply
    • “Console.ReadLine(); ” shouldn’t be a part of the automation. But I’m interested to understand your use case. Could you please explain how you intend to supply the input values in this case.

      Reply
  2. This is really helpful info. If we wanted to build this console app as part of the pipeline and it included an appsettings.json file, how would you recommend going about that? Basically, our console app is designed to test the results of the other code files in this repo, so when those other files change, we want this console app to be built and then run. I have the build pipeline created, and I’ve added all key-value pairs from appsettings.json into the pipeline variables, but I’m not sure how to instruct my build pipeline to use my pipeline variables instead of looking for an appsettings.json file.

    Reply

Leave a Comment