Azure Pipeline MSBuild - msbuild

I am new to the whole azure pipeline. I have a project running on .net framework and want to create YAML with MSBuild in order to perform the following:
Cleaning solution
Restore Nuget Package
Build Solution
Run Unit Test (in different folder)
Package and publish
Can someone please help me?

To use YAML in Azure DevOps Pipeline, you could create a new YAML pipeline from scratch:
To add more tasks in the pipeline, you could click Show assistant and select the tasks you need:
The .yml file may look like below:
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
clean: true
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
Since you are new to Azure DevOps and want to start with YAML, please see our Azure Pipelines documentation for great getting started guides and examples. More information, you could refer to YAML schema documentation. You can also find additional documentation and samples in azure-pipelines-yaml GitHub repo.

Related

Trouble hosting Blazor wasm in Azure App service

I created a blazor wasm app in VS2022 with the option of core hosted. This created 3 projects, client, server and shared.
The server app now contains my web api. I can run the site locally and all works well.
I've created the following Azure DevOps pipeline for build and deployment to an Azure App Service:
trigger:
- master
variables:
vmImageName: "windows-2022"
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: DotNetCoreCLI#2
displayName: "build"
inputs:
command: build
projects: "**/BillByTime.sln"
configuration: "$(buildConfiguration)"
- task: DotNetCoreCLI#2
displayName: "test"
inputs:
command: test
projects: "**/BillByTime.UnitTest.csproj"
arguments: "--configuration Release"
continueOnError: true
- task: DotNetCoreCLI#2
displayName: "publish server app"
inputs:
command: publish
arguments: "--configuration Release --output $(Pipeline.Workspace)/publish_output"
publishWebProjects: False
projects: "**/BillByTime.Server.csproj"
zipAfterPublish: false
- task: ArchiveFiles#2
displayName: "Archive files"
inputs:
rootFolderOrFile: "$(Pipeline.Workspace)/publish_output"
includeRootFolder: false
archiveFile: "$(Pipeline.Workspace)/build$(Build.BuildId).zip"
- publish: "$(Pipeline.Workspace)/build$(Build.BuildId).zip"
displayName: "publish zip"
artifact: "application"
- stage: DeployDev
displayName: Deploy Dev Stage
jobs:
- deployment: DeployDev
displayName: deploy dev
pool:
vmImage: "Windows-Latest"
environment: "dev"
variables:
- name: env
value: "dev"
- name: svcConnection
value: "BizTalkersSvcCon"
strategy:
runOnce:
deploy:
steps:
- download: current
artifact: application
- download: current
artifact: sql-script
- task: AzureWebApp#1
displayName: deploy blazor app
inputs:
azureSubscription: ${{variables.svcConnection}}
appType: "webAppLinux"
appName: appsvc-billbytime-webapi-${{variables.env}}-001
package: $(Pipeline.Workspace)/application/**/*.zip
deployToSlotOrASE: false
resourceGroupName: rg-BillByTime-${{variables.env}}-001
The pipeline runs without error. If I navigate to the url of the app service (https://appsvc-billbytime-webapi-dev-001.azurewebsites.net) I get a 403 Forbidden error.
By making an FTP connection to the App Service, I can see the folder structure and files that have been deployed:
Within /site/wwwroot/BillByTime.Server/wwwroot I see the blazor client app is also deployed.
So, it seems all the files have deployed ok but I seem to be missing some kind of config that would route the browser to /site/wwwroot/BillByTime.Server/wwwroot/index.html ?
Please copy the all the contents under BillByTime.Server and paste to /site/wwwroot.
The reason for this problem is that after the release of the wwwroot folder, there is an extra layer of BillByTime.Server. Normally, under wwwroot, a web.config file is required.
Or you also can change the wwwroot like below:

Having a script run only when a manually triggered job fails in GitLab

I have the following script that pulls from a remote template. The remote template has the following stages: build, test, code_analysis, compliance, deploy.
The deploy step is manually triggered and executed AWS CLI to deploy a SAM project.
I want to add an additional step such that when the deploy step fails, it will execute a script to rollback the cloudformation stack to its last operational state.
I created a "cleanup-cloudformation-stack-failure" job and tried adding "extends: .deploy", but that didn't work.
I then added an additional stage called "cloudformation_stack_rollback" in the serverless-template.yml file and tried to use a mix of rules and when to get it to trigger on failure, but I'm getting errors flagged by GitLab's linter.
Does anyone know what I'm doing wrong?
include:
- remote: 'https://my-gitlab-server.com/ci-templates/-/raw/master/serverless-template.yml'
deploy-qas:
extends: .deploy
variables:
....
PARAMETER_OVERRIDES: "..."
environment: qas
only:
- qas
tags:
- serverless
cleanup-cloudformation-stack-failure:
variables:
STACK_NAME: $CI_PROJECT_NAME-$CI_ENVIRONMENT_NAME
stage: cloudformation_stack_rollback
rules:
- if: '$CI_JOB_MANUAL == true'
when: on_failure
script:
- aws cloudformation continue-update-rollback --stack-name ${STACK_NAME} --resources-to-skip ${STACK_NAME}
You forgot double quotes around true, however you can use Directed Asyclic Graphs to execute jobs conditionally
include:
- remote: 'https://my-gitlab-server.com/ci-templates/-/raw/master/serverless-template.yml'
deploy-qas:
extends: .deploy
variables:
....
PARAMETER_OVERRIDES: "..."
environment: qas
only:
- qas
tags:
- serverless
cleanup-cloudformation-stack-failure:
needs:
- deploy-qas
when: on_failure
variables:
STACK_NAME: $CI_PROJECT_NAME-$CI_ENVIRONMENT_NAME
stage: cloudformation_stack_rollback
script:
- aws cloudformation continue-update-rollback --stack-name ${STACK_NAME} --reso

How do I access a custom variable from an azure build pipeline in an azure release pipeline? [duplicate]

This question already has answers here:
VSTS : Can I access the Build variables from Release definition?
(3 answers)
Closed 2 years ago.
I added the custom variable like this:
- task: Bash#3
displayName: 'naming the artifact'
inputs:
targetType: 'inline'
script: |
an=FE_SNAPSHOT.zip
echo "##vso[task.setvariable variable=artifactName;]$an"
Now I am inside the deploy pipeline and would like to access the variable artifactName in a deploy bash script.
The variable is not listed in the "Initialize Job" step.
UPDATE 1:
The deployment pipeline has a trigger to the build pipeline and the build pipeline is linked as an artifact (in the artifacts section). Maybe this is another problem...
SOLUTION:
The linked ticket contains a part of my solution. The BUILD PIPELINE creates a simple file containing the value/variable I want to store:
- task: Bash#3
displayName: 'naming the artifact'
inputs:
targetType: 'inline'
script: echo "FE_SNAPSHOT.zip" > $(Build.ArtifactStagingDirectory)/artifactName.value
The DEPLOYMENT PIPELINE reads the file and sets the variable:
- task: Bash#3
displayName: 'naming the artifact'
inputs:
targetType: 'inline'
script: |
an=`cat FE_CI_OS/drop/artifactName.value`
echo "##vso[task.setvariable variable=artifactName;]${an}"
Then it can be used in a next task as $(artifactName).
Scope of this variable is a pipeline. And since this variable is set dynamically it it not listed in Initialize Job. It should be avaibale in your next task. Can you add right after this task
- script: echo $(artifactName)
You should get your value displayed.

Azure Pipeline - build and gen swagger doc

My project is an AspNet Core 2.2 Api, I am building it in Azure Pipelines (classic) I want to generate the swagger document during an azure pipeline build - for this I am using Swashbuckle.AspNetCore.Cli and the documents in Retrieve Swagger Directly from a Startup Assembly
I have a Use .NET Core task set to 2.2 at the beginning of the job
I have installed the tools using .NET Core task with custom tool command with arguments install swashbuckle.aspnetcore.cli --version 5.0.0-rc4 --global, this seemed to work; If I run this task again it fails with message that tool is already installed.
Then in my CI Build, I added a .NET Core task with settings
command to Custom
Path to Project(s) to the path csproj file
Custom command to swagger
Arguments to tofile --output $(Build.ArtifactStagingDirectory)/swagger.json $($(Build.ArtifactStagingDirectory)_Dxxxxx.Api.dll v1
I'm getting this error No executable found matching command "dotnet-swagger"
Help!
Here is a working example using the JAR maintained by the Swagger Codegen team.
I used the public microsoft speech-to-text-api-v3 API but feel free to change it.
trigger:
- master
variables:
jar_version: 3.0.29
- job: swagger_client
pool:
vmImage: 'ubuntu-latest'
steps:
- task: JavaToolInstaller#0
inputs:
versionSpec: '11'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
displayName: 'Set-up Java'
- script: |
java -version
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/$(jar_version)/swagger-codegen-cli-$(jar_version).jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar generate \
-i https://westus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/export\?DocumentFormat\=Swagger\&ApiName\=Speech%20to%20Text%20API%20v3.0 \
-l python \
-o lib/python-client
- task: DownloadPipelineArtifact#2
inputs:
patterns: 'python-client/**'
path: $(Build.SourcesDirectory)/lib/python-client

No package found when deploying an Asp.Net Core application to Azure using Azure DevOps

I have the following Publish and Deploy tasks in Azure DevOps:
variables:
buildConfiguration: 'Release'
buildPlatform: 'any cpu'
azureSubscription: 'subscription'
azureAppType: 'Web App on Windows'
webAppName: 'webapp'
steps:
# Other tasks
- task: DotNetCoreCLI#2
displayName: 'Publish'
inputs:
command: publish
publishWebProjects: false
arguments: '--configuration $(buildConfiguration) --output $(build.artifactstagingdirectory)'
zipAfterPublish: true
- task: AzureRmWebAppDeployment#4
displayName: 'Deploy'
inputs:
package: $(System.DefaultWorkingDirectory)/**/MyProj.zip
azureSubscription: '$(azureSubscription)'
appType: '$(azureAppType)'
webAppName: '$(webAppName)'
The publish tasks succeeds but I get the error on the Deploy task:
[error]Error: No package found with specified pattern: /home/vsts/work/1/s/**/MyProj.zip
What am I doing wrong?
Review your YAML:
You publish with the following parameters:
--output $(build.artifactstagingdirectory)
You try to deploy from a completely different location:
package: $(System.DefaultWorkingDirectory)/**/MyProj.zip
Try deploying $(build.artifactstagingdirectory)/**/MyProj.zip.