Azure DevOps Pipeline with selfhosted agent fails to run tasks - c#

I am trying to set up an Azure devops pipeline that runs on a virtual machine in Azure. I'm doing the build in azure and the download the artifact to the VM for deployment. However, when I try to run tasks like web.config transform or file copy, I get an error like this:
##[error]The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.
##[debug]System.InvalidOperationException: The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell.
This quite odd since the VM is running windows.
I have made sure to install the latest version of powershell

After you use a self-hosted agent for Windows, Make sure you have referenced windows as a host pool in your YAML pipeline. Also, If you’re using the azure PowerShell task, Make sure you have installed the Powershell module in your VM acting as a self-hosted agent.
I created one windows VM (Windows Server 2019 Datacenter Gen2) in Azure Portal and authorized it to work as a self-hosted agent in Azure DevOps with the PAT token like below:-
Self hosted agent is online in Azure DevOps:-
Note- It is a part of Default Pool in my Azure DevOps org
Now, I ran an Azure-Powershell inline script with this Agent in Azure DevOps :-
Yaml pipeline code:-
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
name: Default
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
displayName: 'Run a multi-line script'
- task: AzurePowerShell#5
inputs:
azureSubscription: 'SID subscription(xxxxxx365-f598-44d6-xxxx-e2b6e97cb2a7)'
ScriptType: 'InlineScript'
Inline: 'Get-AzResource | Format-Table'
azurePowerShellVersion: 'LatestVersion'
Note:- I have added pool as Default where my agent pool is connected
in Azure DevOps, Make sure you use the correct pool here in your YAML
code:-
pool:
name: Default
The Azure Powershell script ran successfully like below:-
Now, when I ran another SQL Inline script with my pool set to another agent OR ubuntu-image, I got same error code as yours:-
trigger:
- main
pool:
vmImage: ubuntu-latest
Pipeline Output:-
When I changed the pool to windows, the error was resolved and the task ran successfully like below:-
trigger:
- main
pool:
vmImage: windows-latest
Pipeline Output:-
Make sure you’re selecting the correct pool and the Powershell module is installed properly inside that VM which is acting as an Azure DevOps agent.
Reference:-
The current operating system is not capable of running this task. That typically means the task was written for Windows only. For example, written for Windows Desktop PowerShell. – AzureFileCopy#4 | What I Broke – Programming and Web Development

Try installing Windows Desktop PowerShell on the virtual machine and see if that resolves the issue.

Related

ASP.NET Core 3.1 web app publishes from Visual Studio does not work when deployed from Azure DevOps release pipeline

I have a fully working web api written in dotnet core 3.1. I've been following the TimCoRetailManager series.
The application builds with no errors and works as intended when running in Visual Studio. I have a free azure subscription. I setup a F1 free web app service on linux and two basic SQL server databases, all in the same resource group with the firewall set to allow access to other azure services to connect. I can publish the web api and the databases. I can connect to the databases with azure data studio and things look fine. I updated the config on the app service so it's using the azure secrets instead of my dev environment secrets. The API works as intended when published from Visual Studio, i can get to the swagger ui, and life is good.
On to Azure DevOps. When I use a release pipeline from Azure DevOps, the pipeline gives a success message. However, when I navigate to the API (has a simple MVC landing page + swagger) it just has an "Application Error :(" page. I found the logs on the Azure App service and they have this...
Ok 2022-03-01T21:44:43.8098649 \/ \/ \/
Ok 2022-03-01T21:44:43.8098677 A P P S E R V I C E O N L I N U X
Ok 2022-03-01T21:44:43.8098704
Ok 2022-03-01T21:44:43.809873 Documentation: http://aka.ms/webapp-linux
Ok 2022-03-01T21:44:43.8098757 Dotnet quickstart: https://aka.ms/dotnet-qs
Ok 2022-03-01T21:44:43.8098784 ASP .NETCore Version: 3.1.21
Ok 2022-03-01T21:44:43.809881 Note: Any data outside '/home' is not persisted
Ok 2022-03-01T21:44:44.3985103 Running oryx create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -defaultAppFilePath /defaulthome/hostingstart/hostingstart.dll -bindPort 8080 -userStartupCommand 'dotnet TRMApi.dll'
Ok 2022-03-01T21:44:44.4498212 Cound not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
Ok 2022-03-01T21:44:44.4499139 Could not find operation ID in manifest. Generating an operation id...
Ok 2022-03-01T21:44:44.4500119 Build Operation ID: 74cf02f2-a6c2-44a8-9077-95f1bab9d974
Ok 2022-03-01T21:44:45.5344493
Ok 2022-03-01T21:44:45.5358264 Agent extension
Ok 2022-03-01T21:44:45.5358438 Before if loop >> DotNet Runtime
Ok 2022-03-01T21:44:46.2152908 DotNet Runtime 3.1Writing output script to '/opt/startup/startup.sh'
Ok 2022-03-01T21:44:46.6953451 Running user provided startup command...
Ok 2022-03-01T21:44:46.7448258 It was not possible to find any installed .NET Core SDKs
Ok 2022-03-01T21:44:46.7453653 Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
Ok 2022-03-01T21:44:46.7457829 https://aka.ms/dotnet-download
The build pipeline appears to be working as intended. It produces four artifacts: a folder for the web api with a zip for deployment, a folder for the database stuff notably with a dacpac, and a folder for the front end with a zip file in there, too. The release pipeline isn't publishing all of this though. I set the package to $(System.DefaultWorkingDirectory)/_Standard Continuous Integration Build/ApiDrop/TRMApi.zip.
The release pipeline has the following settings:
Azure subscription: selected free trial and authenticated it
App Type: Web App on Linux
App Service name: selected api's name from drop down
Startup command: blank
Agent Job
Agent: Azure Pipelines
Agent Specification: windows-2019
Artifact download: continuous integration build > selected all artifacts
Deploy Azure App Service step
Task version: 4.*
Connection type: Azure Resource Manager
Azure subcription: read only, prefilled by app service selection earlier
App service type: also read only, prefilled
App service name: also read only, prefilled
Package or folder: $(System.DefaultWorkingDirectory)/_Standard Continuous Integration Build/ApiDrop/TRMApi.zip
Runtime Stack: 3.1 (DOTNETCORE|3.1)
Any tips or suggestions? I'm really scratching my head on this. I'm watching videos on pluralsight and youtube and it seems like the process "just works" for the content creators, but I'm not able to repeat the results.
Edit: Including screenshot of how artifact is configured in release pipeline.
FWIW, I have the exact same problem from the exact same code base.
I also have the build pipeline artifact set up correctly.
I initially got caught up on the message "It was not possible to find any installed .NET Core SDKs".
I went to the app service "Advanced tools" and then used Bash and issued the commands:
dotnet --list-sdks
dotnet --list-runtimes
Initially, it wasnt reporting any sdk's.
This led to me adding an additional Use.NET Core pipeline task at the beginning of the pipeline with the following parameters
Display Name:
Use .NET Code sdk 3.1.415
Package to install:
SDK (contains runtime)
Version:
3.1.415
Compatible Visual Studio version:
16.7.21
Path to Install .Net core:
$(Agent.ToolsDirectory)/dotnet
I chose the above versions which seemed to match what was reported being available on the linux box.
However, none of this seemed to make any difference, as it still fails with the same error as you. I did want to share what I have tried so far, but wasnt enough to solve it.
On a whim, I spun up a Windows-based App server (Free tier) to deploy to as well. I modified my Release pipeline and added a second "Deploy to App Service" step. This second one deploys to the Windows App Service.
Once I did this, the app runs successfully on the Windows App Service after the second deployment step, but not on the Linux app service after the first deployment step.
It turns out the issue was due to a Build pipeline issue.
It was building the API project using a Visual Studio Build (i.e. MsBuild) project step.
In order to target linux, this needs to be replaced with a
dotnet publish (or a dotnet build followed by a publish) with the parameters "-r linux-x64" plus the appropriate output folder.
Build pipelines and release pipelines do not use the same system default working directory. So I think your release pipeline is probably not getting the artifacts in the build pipeline.
There is an easy way to pass artifacts generated in build pipelines to release pipelines:
In your build pipeline, Use publish build artifact task or publish pipeline artifact task to publish your artfact.
In your release pipeline, click "Add an artifact". Then select "Build" as source type and fill in the information about your build. Note the value of "Source alias", which you need to use as the name for artifacts in release pipeline.
Go to the "Azure App Service Deploy" task. In "Package or folder", click browse button, and you can find your artifacts path there.

Azure Webjob: Git clone directly to remote

Im running a Windows-based App Service.
Im running a backup of a fairly large git repo and I'm wanting to clone directly to Azure Blob/File/S3. Cloning locally leads to an out-of-memory exception. I realise that you can mount azure storage, but this is just for Linux based apps.
Im using LibGit2Sharp ti run the Repository.Clone function. I've tried mapping a network drive on my Azure Webjob, but I get an "Access Denied"
When you create azure web app, if you choose option of Publish is Code, like below. It don't support net use command. These webapp are running in standard sandbox env.
Solution:
Create your web app like below. Docker Container will solve your problem.
Although the container webapp also runs in the sandbox environment, you can customize many configurations.
In local, I test net use command, which is same as windows container app.

How to run already developed ASP.NET Core app on Ubuntu?

What is the easiest way to run my existing ASP.NET Core application on Ubuntu? I have found this: https://learn.microsoft.com/en-us/aspnet/core/publishing/linuxproduction but I am stuck on this:
I have published the application and copied it to my Ubuntu, but I have no idea how can I "run the app". Any help will be really appreciated.
It's really as simple as executing:
dotnet path/to/your/application.dll
However, for a website you really want to manage that with some sort of init system. The doc file you link to tells you how to start your application using Systemd.
Create a service definition file e.g. /etc/systemd/system/myapp.service
Edit the file to look like this, replacing the relevant parts where necessary:
[Unit]
Description=Example .NET Web API Application running on Ubuntu
[Service]
WorkingDirectory=/var/path/to/your/app
ExecStart=/usr/bin/dotnet /var/path/to/your/app/hellomvc.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
Enable the service like this:
systemctl enable myapp.service
Start the service:
systemctl start myapp.service
Check if your service is running:
systemctl status myapp.service
If you have another init system, the instructions will of course be quite different.
Note: This only starts the app running on your machine. If you intend to serve it to the public, then it is highly recommended that you use a proxy such as Nginx as Microsoft has not yet certified Kestrel as an edge server.

Unable to run selenium in Jenkins [duplicate]

When I run my selenium test (mvn test) from jenkins (windows) I see only the console output. I don't see the real browsers getting opened . How can I configure jenkins so that I can see the browsers running the test?
I had the same problem, i got the solution after many attempts.
This solution works ONLY on windows XP
If you are using jenkins as a windows service you need to do the following :
1) In windows service select the service of jenkins
2) Open properties window of the service -> Logon-> enable the checkbox "Allow service to interact with desktop"
After then you should reboot the service jenkins
Hope this help you :)
UPDATE:
Actually, I'm working on a an automation tool using Selenium on Windows 10, I've installed Jenkins ver. 2.207 as windows application (EXE file), it's running as windows service and ALL drivers (Chrome, FireFox, IE) are visible during test executions WITHOUT performing a mere configuration on the System or Jenkins
I got the solution. I ran jenkins from command prompt as "java -jar jenkins.war" instead of the windows installer version. Now I can see my browser based tests being executed.
If you are already doing what #Sachin suggests in a comment (i.e. looking at the machine where Jenkins actually runs) and still do not see the browsers, then your problem may be the following:
If you run Jenkins as a service in the background it won't open apps in the foreground. You may either try to run it not as a service in the foreground, or run it as a Local System account and check Allow the service to interact with desktop option. In the latter case you may get into permission problems, though.
Update: To make sure this answer is understood properly by others: Jenkins Windows 'native' installation is not really native. It's a wrapper around Java that runs it as a service.
To interact with desktop GUI, you should launch slave agent via JNLP:
https://wiki.jenkins-ci.org/display/JENKINS/Distributed+builds#Distributedbuilds-LaunchslaveagentviaJavaWebStart
After adding the node in Jenkins (configured as Java Web Start launch), just make a startup batch script on the node machine:
java -jar slave.jar -jnlpUrl http://{Your Jenkins Server}:8080/computer/{Your Jenkins Node}/slave-agent.jnlp
(slave.jar can be downloaded from http://{Your Jenkins Server}:8080/jnlpJars/slave.jar)
See more answers here:
How to run GUI tests on a jenkins windows slave without remote desktop connection?
In the case of Windows 7 you should not install jenkins as windows application (because in this recent version, Microsoft decided to give services their own hidden desktop even you enable the functionality "interact with desktop" in jenkins service), you may have to deploy it from a war file as follows:
1) Download jenkins.war from Jenkins official site
2) Deploy it by the command prompt : java -jar {directoryOfJenkinsFile}/jenkins.war
3) Now you can access jenkins administration on http:// localhost:8080
Hope that helps you !
this is an issue for Jenkins. on Windows it is possible to access logon user's session (screen) under system account. to make the UI testing visible, Jenkins needs to bypass UAC (user access
control) at background. this solution works for me with my own service running as system account.
I also faced the same issue earlier in my local machine (Windows 10).
My test was running perfectly from the NetBeans but when I moved to Jenkins it was only running in console mode. I was unable to view the UI.
So for that, you just need to make your local machine as a Jenkins slave by creating a new slave node in your Jenkins and select that node to execute the Jenkins job.
If jenkins installed by windows installer it is showing only Console out put only. To see browsers download jenkins.war file and run java -jar jenkins.war from command line.
Go through this site:
http://learnseleniumtesting.com/jenkins-and-continuous-test-execution/
If you have the following situation,
You are able to login to the remote machine
You don't see the Jenkins agent window
This slave machine is accessed by many users then try the following,
then try the following suggestion.
Login to slave machine
Go to Task manager
Users
Logout all the users
Then login again.
This worked for me.

TFS CI build to Azure Web App (App Service) fails due to file locking (msvcr100.dll)

Problem
Getting a deployment error when trying to publish to an Azure Web App from TFS CI. A file is locked and this prevents the build from updating.
Symptoms
Publishing manually (Web Deploy publish from within Visual Studio) usually succeeds.
Stopping the Web App and publishing allows it to succeed, however this defeats the point of our CI if we need need to stop and start the Web App each time.
CI publish to Web roles and Worker roles don't appear to have this issue, we only get it on publishing to Web Apps (formerly Web Sites, the current Azure Portal term is now App Service).
Only publishing from a CI build via TFS fails consistently in this way.
Error
Web deployment task failed. (Web Deploy cannot modify the file
'msvcr100.dll' on the destination because it is locked by an external
process. In order to allow the publish operation to succeed, you may
need to either restart your application to release the lock, or use
the AppOffline rule handler for .Net applications on your next publish
attempt. Learn more at:
http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_FILE_IN_USE.)
The information at the link isn't very helpful.
ERROR_FILE_IN_USE
Diagnosis – A destination file cannot be overwritten or deleted because it is currently in use.
Resolution – Make sure that the destination file is not in use before performing a sync. If you are syncing content to a web site
hosted on IIS 7 or later (using the appHostConfig, iisApp, or
contentPath providers), consider taking the application offline during
the sync by enabling the appOffline rule.
Attempted resolutions
We were using New Relic - have since removed New Relic and this issue still persists. The binary appears to be a Microsoft library but it's unclear how it is relevant to the application (it's not referenced).
Some other SO questions have addressed similar issues with publishing but none of these relate to getting this issue from TFS CI.
azurew website continious deployment - Web Deploy cannot modify the file 'XXX' on the destination because it is locked by an external process
How to take web app offline while publishing?
One answer in the above question suggests using the EnableMSDeployAppOffline configuration in the publish profile, and adding this configuration works OK for doing a publishing manually from within VS but it doesn't fix the problem when publishing automatically from TFS/CI.
Edit
How to take web app offline while publishing? deals with taking the app offline using the EnableMSDeployAppOffline configuration - unfortunately this config only seems to be supported when doing WebDeploy through Visual Studio (not CI).
You can use the Web Deploy v3 in CI to deploy your web app.
In Web Deploy V3, we added support to automatically take an ASP.Net
application offline before publishing to it. This is useful if a user
wants to ensure that their application does not have a lock on a file
(e.g. SQL CE sdf files which only allow one connection to the file at
a time) being overwritten, or if they want to ensure that visitors to
their site cannot affect the publish process. When the publish process
is completed, the App_Offline.htm file will be removed and the site
will be online again.
Or you can add a PowerShell script like following to deploy the web app to Azure:
param($websiteName, $packOutput)
$website = Get-AzureWebsite -Name $websiteName
# get the scm url to use with MSDeploy. By default this will be the second in the array
$msdeployurl = $website.EnabledHostNames[1]
$publishProperties = #{'WebPublishMethod'='MSDeploy';
'MSDeployServiceUrl'=$msdeployurl;
'DeployIisAppPath'=$website.Name;
'Username'=$website.PublishingUsername;
'Password'=$website.PublishingPassword}
Write-Output "Stopping web app..."
Stop-AzureWebsite -Name $websiteName
Write-Output "Publishing web app..."
$publishScript = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\Publish\Scripts\default-publish.ps1"
. $publishScript -publishProperties $publishProperties -packOutput $packOutput
Write-Output "Starting web app..."
Start-AzureWebsite -Name $websiteName
Reference from: https://msdn.microsoft.com/en-us/Library/vs/alm/Build/azure/deploy-aspnet5
The new "Deploy AzureRM Web App" task has an option to take the app offline which will prevent this error.
See screenshot for checkbox

Categories

Resources