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.
Created a new C# Console App Project in Visual Studio 2019. “HelloWorld-Console”.
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
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.
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.
The pipeline ran just fine and I could see the output from the console app in the logs: ?
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.
Now for every run of the pipeline, I could see an artifact produced, containing the output text file.
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.
have any feedback? let me know in the comments!
Happy Learning!
devsdaily.com
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
“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.
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.