Execute script on Azure Windows VM via Azure pipeline - azure

I am using third party software for master data management. This software does not have API's to perform deployment across environment. It has command line utility which needs to be installed on machine and call the commands to perform the installation. I am looking to automate this solution using Azure Devops pipeline. What I am trying to do is as below
Create windows VM in Azure and install command line utility on it.
Store script in some folder in the VM.
Use Azure pipeline to call this script which is stored in my VM.
I don't even know if it possible to do such things. I tried looking on internet on how to call script stored in VM via Azure pipeline but didn't find any useful link.
If any one has done such activity or have an idea how can it be achieved please help.

this can help you
Running the command line in a Azure Virtual Machine is fully supported in Azure Pipelines. You could install a self-hosted agent in the VM.
Before that, I recommend that you could create a new agent pool for the self-hosted agents. Please go to the organization settings -> Agent pools-> click the "Add pool" -> choose the "self-hosted" type.
Then, you could refer to this document to complete the installation. When you configure the agent, you could choose the new created agent pool.
After installation, you could create a pipeline, choose the new created agent pool and add a Command Line task to run the command line. Of course, if you have many agents in the same agent pool, you could also set the demands in the pipeline to specify one agent.
reference : Is it possible to run a command line in a Azure Virtual Machine from Azure DevOps pipeline?

Related

Provisioning an Azure VM agent using a Jenkins Multipipeline

I have a Jenkins instance running on an AWS EC2. Rather than use the built-in nodes(i have disabled them), I need to provision an Azure VM agent, perform builds and execute tests on that agent. Using the https://plugins.jenkins.io/azure-vm-agents/ via a freestyle job I was able to successfully provision an Azure VM agent on demand. However I an unable to achieve the same via a Multibranch pipeline ( there is a seperate repo for Jenkins File and one for the code repo) (plugin https://plugins.jenkins.io/remote-file/). It does not seem to kick of the Azure VM agent and fails waiting for an agent. If i enable the built-in node, it works. So it appears that via the pipeline the azure-vm-agents is not triggering for provisioning. . There appears as if a link For how to select agent in pipeline, refer to this doc. within the https://plugins.jenkins.io/azure-vm-agents/ page does not lead anywhere. Any thoughts much appreciated.

Assigning a vm for azure pipline task rather than creating a new vm's for each run

As a beginner of DevOps, I would like to know how to use one VM for azure pipeline runs. When starting the run of the azure pipeline task it always gives a fresh VM from azure.
For caching and file saving purposes, I want to use a reserved VM for pipeline run.
Appreciate your suggestions and support.
Check the pic, In the Azure DevOps, we could run the pipeline via Hosted agent and Self-Host agent.
Azure Pipelines provides a pre-defined agent pool named Azure Pipelines, this is hosted agent and each time you run a pipeline, you get a fresh virtual machine. The virtual machine is discarded after one use.
For caching and file saving purposes, I want to use a reserved VM for pipeline run.
We could refer to this doc to install the self-hosted agent, it will save the cache.
You can setup a 'self hosted agent'. That would be your own VM, which you have total control over. I'm not sure whether this will be any cheaper than hosted agents.
I've used a self-hosted agent a while ago, and saved some money booting the VM only when needed. After a while it would shutdown again.
Source: Self-hosted agents

create azure devops windows self hosted agent remotely

Need to create azure devops windows self-hosted agents programmatically (Need to install Remotely for the existing VM).
I have PS script and it is working well if i run it (as admin) inside the virtual machine and agent is getting installed successfully. But i want to create this agent for the existing VM without logging in (Remotely i need to install the agent), because i will need to just add the ARM template in Azure devops pipeline and that should create the Agent for the existing VM.
As this needs to be installed remotely and securely, i shouldn't use custom script due to some restrictions.. It would be really great if we can achieve this installation remotely without custom script and using any other automated way.
Any information will be helpful, thanks.
I have found out a way using Run-Command. This is another helpful option to run the PS scripts remotely on a Virtual machine. In addition, this executes the script with elevated permissions which is really helpful in many scenarios.
Source: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command#powershell
This can be performed using via Portal, Powershell and CLI as well.

Provision a VM as part of an Azure Devops build pipeline

I have a build pipeline that is working pretty well currently in Azure DevOps. As part of the pipline/build process, I create an artifact, which is published and reachable. After that, I'd like to do the following:
Create/Start Up a new VM (Windows)
Grab the now published artifact, unzip it and run the executable within
Run the integration tests
Close the VM
I've looked around the Azure documentation but cannot find much that discusses this sort of solution. Please help!
There is nothing built-in (like a readymade task create a vm), so you can use any way to create a VM in Azure. Azure powershell, Azure Cli, ARM Templates, SDK calls. whatever works for you.
You would need to open ssh\winrm to talk to that vm to deploy stuff to it. thats about it. You can find lots of examples on how to create a VM online. VSTS got tasks for Azure Powershell\Cli\ARM Tempaltes so you dont need to handle auth.
You can create a VM using ARM templates with the task 'Azure Resource Group Deployment'
With a separate task 'Powershell on target machine' you can run a powershell script on the target VM, if you put the downloading, unzipping and running of this exe in this script you should be able to perform the tasks you need.
You could also look into the 'invoke-azurermvmruncommand' powershell command, this allows you to run a powershell script in the vm. https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/invoke-azurermvmruncommand?view=azurermps-6.11.0

Continuous Dilvery as Windows Service and Web Api using TFS Build on Azure VM

I have TFS 2015 and i was able to automated the build process from the branch and get the files from the drop folder as shown below:
It has release for multiple projects like Web API and Windows Service
I want Azure VM on which i want to automate the deployment process - continuous delivery.
Deploy the Web API on IIS on Azure VM
Deploy the Windows Services On Azure VM.
Run Scripts SQL.
I have credentials of Azure VM. How i can perform the three above steps.
I have worked on a similar problem in the past so can probably help you out (MSFT, if it helps).
Web Api on IIS on Azure VM
This is almost completely automated in the form of WinRM - IIS Web App Deployment task that you can find and add in your release definition. The link provides complete instructions on what parameters to provide and tweaks to be done for Azure VM compared to on-premise ones. There are a few prerequisites to running this task, like installing and configuring IIS on the VM which the documentation discusses in detail. As a necessary input to this task, you need to provide the web deploy package which I am assuming was generated as your build output. If not, you can refer to this SO post to get the required output. If you have parameters like connection strings that you wish to modify at deploy time, using a parameters.xml file in the above task.
Windows Service on Azure VM
There is no completely automated task for this requirement, but it is pretty straight-forward. It can be achieved by using the PowerShell on Target Machines task along with Azure File Copy task. For the first task, all that is required as input is the .exe of the windows service that you wish to deploy, which should be generated as the output of your build process (build artifacts). Much of the remote machine inputs for this task is similar to the previous one so you should not have any problem there. You will need to check-in the Powershell script that does the actual windows service installation, in your source code as part of the same windows service project (copy local = True). This will ensure that as the build output, you will have access to the powershell script which you can use in the second task. Azure File Copy is required to copy your powershell script to the Azure VM so that the Powershell task can execute it. Let's assume you copied the powershell script to a folder C:\Data\ on the Azure VM.
$serviceName = "MyWindowsService"
$exeFullName = "path\\to\\your\\service.exe"
$serviceDisplayName = "MyWindowsService"
$pss = New-Service $serviceName $exeFullName -DisplayName $serviceDisplayName
-StartupType Automatic
Add this content to the checked in powershell file and name it installWindowsService.ps1. Then in the powershell task provide the path of the powershell file to execute as C:\Data\installWindowsService.ps1.
Run SQL Scripts on Azure VM
I haven't personally worked on this so the best I can do is point you in the right direction. If you are using DACPAC for your SQL deployment, you can use the WinRM - SQL Server Database Deployment task. If you just intend to execute scripts, use the remote powershell task from above and refer this post that will help you with running SQL commands through powershell script
Seems you want the CD release process picks up the artifacts published by your CI build and then deploys them to your IIS servers/Windows Services on Azure VM.
If you've just completed a CI build, then you should create a new release definition that's automatically linked to the build definition.
Open the Releases tab of the Build & Release hub, open the + drop-down in the list of release definitions, and choose Create release definition.
For 2, write a powershell script to handle this, ensure build outputs
were available to copy from the ‘Drop’ folder on the build and that
they are copied to C:\xxx\ on the target VM(s). More detail steps
please refer this blog.
For 3, you could use Azure SQL Database Deployment task. Either
select the SQL Script file on the automation agent or on a UNC path
that is accessible to the automation agent. Or directly enter the
InLine SQL Script to run against the Azure SQL Server Database. Also take a look at the tutorial.
Maybe not all the task is fully Compatible with TFS2015 version, you could upgrade your TFS version to get more new features or customize your own build/release task to handle it.

Resources