How build artifacts are managed in Azure DevOps pipeline? - azure

I am setting up a new Azure DevOps pipeline for my webapp. I can see that the build artifacts are saved in a path called $Build.ArtifactStagingDirectory. Where this path variable is pointing to and how long I can access my artifact?
Also, Does Azure DevOps versions(keep track of all the artifacts built over a time) the artifacts built? If no, how to version my build artifacts?

In the agent there are 3 folders: a, b and s. The variable $(Build.ArtifactStagingDirectory) point to folder a (artifacts), so the path is c:\agent\_work\1\a (if the agent location is c:\agent, the 1 can be another number also, according to how many builds there are, this number is incremental).
The artifacts not saved there! when you build the code all the code and the artifacts exist on folder s (sources), the best practice is to copy only the artifacts to folder a and then use the task "Publish build artifacts", in this task you take the artifacts from folder a and put them on Azure DevOps storage or in your file share (if you use self-hosted agent).
From the Azure DevOps storage / file share the artifacts exist according to your retention policy.
If you save the artifacts in Azure DevOps you can access your artifacts from the build summary page or create a release pipeline. if you save them in a file share you can just access them there or in the release pipeline.

You need to publish artifacts using task as by default they are not published. If you are using yaml just add
# Publish Build Artifacts
# Publish build artifacts to Azure Pipelines/TFS or a file share
- task: PublishBuildArtifacts#1
inputs:
#pathtoPublish: '$(Build.ArtifactStagingDirectory)'
#artifactName: 'drop'
#publishLocation: 'Container' # Options: container, filePath
#targetPath: # Required when publishLocation == FilePath
#parallel: false # Optional
#parallelCount: # Optional
There is configuration how long to keep those artifacts as i remember by default is 30 days and 3 or 5 last builds

You can control how many days you want to keep each build (default 30 days) before deleting it (See
Build and release retention policies). When a build is deleted, the published artifacts also gets deleted.
To version your build artifacts you can use the build number which will keep track.
Where the $Build.ArtifactStagingDirectory is pointing to depends on which publish location you are choosing. See Artifacts in Azure Pipelines

Related

Unable to deploy a .NET core app from DevOps to Azure using publish profile

I am creating a new Azure Pipeline to deploy .NET Core API app to an App Service in Azure. I am using connection type=publish profile in the pipeline, which is asking for a .pubxml file.
I have already committed the .pubxml file in my Azure repository.
But while giving the path for getting this .pubxml file, I am suspecting that I can't get .pubxml file through build artifacts. Is there any way to get the .pubxml file from azure repos to release pipeline?
I tried with $(Build.SourcesDirectory)\${{ parameters.Location }}\Properties\PublishProfiles\fcmapapi_Dev%20-%20Web%20Deploy.pubxml but no luck.
Any suggestions?
Step 1: Add Azure Repos / Build artifact with an alias to your release pipeline.
https://learn.microsoft.com/en-us/azure/devops/pipelines/release/artifacts?view=azure-devops#artifact-sources
https://learn.microsoft.com/en-us/azure/devops/pipelines/release/artifacts?view=azure-devops#artifact-sources---tfvc-git-and-github
Add "Build" resources as an artifact:
Add "Azure Repository" as an artifact:
Once, you have added both, the classic release pipeline will look like this:
Step 2: Use the three dots to browse the files / packages.

Is it possible to download azure pipeline artifact to local folder within pipeline?

I have an Azure pipeline in which I create an artifact (a folder around 400 MB)
...
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifact: 'PublishArtifact'
publishLocation: 'pipeline'
Is it possible to download it to my local folder (let's say: C:\test) with a command within this same pipeline?
(I can download it manually if I go to Pipelines -> 'The latest build' -> Under Related, I have published artifact, but I want to do this automatically within pipeline with some command)
If you want to do it during pipeline execution you should install self hosted agent on your machine. Here you have links:
Self-hosted Linux agents
Self-hosted Windows agents
Then you need to condogure pipeline to use this agent.
Another option would be FTP upload. In this way you need to configure you machine as FTP server and use for instance this task. In this approach you can still use MS Hosted agent.
I think this should be possible if you add your computer as a deployment group (See Provision deployment groups). Then your build could trigger a classic Release pipeline where you can copy the artifact to a folder within you local machine
You can use Copy file task as explained in this doc
Copy file task

Download result of Azure Release Pipeline

I have a release pipeline that combines two build pipelines artifacts to create the full release. I need to be able to download the result of this task after is done.
I run the Archive Task to zip the results but I don't know how to save it somewhere where I can download it using the Azure Pipeline agent.
Is there a task that can trigger that as a download or can I save it as an Artifact?
Thank you
Yeah, what you have to do is publish and then consume artifact in your Release pipeline. Here you have documentation
steps:
- publish: $(System.DefaultWorkingDirectory)/bin/WebApp
artifact: WebApp
Id you use Yaml then you should use this:
steps:
- download: current
artifact: WebApp
If you use Classic releases you needs to confgure it on designer.
Please use this task in your pipeline:
and then configure your release here:
Just realized that if I do this with my On-Prem agent I can get the file form the agent's file structure.
Would be nice though if I could get the file straight from Azure instead.
Thank you.

Defining which project should be deployed to an Azure Functions app from source control

We have a collection of Azure Function Apps in c# net core. Each App contains a small number of Azure Functions. All Function Apps reside in a single git repository.
We would like some of our environments to deploy automatically from source (e.g. bitBucket or gitHub).
How do we configure the project so that Azure knows which project in source relates to which created Function App?
I have searched around this problem for a number of days and have not seen any results that sit outside of "it just works" so can only assume that we are missing something fundamental.
I'd recommend using Azure DevOps (formerly VSTS) to deploy to Azure, you use YAML to define a build pipeline which can publish an artifact from each of your function apps. The artifacts then get picked up by a release pipeline and can be deployed to Azure.
The basic building blocks of this are, firstly some YAML like this in your build pipeline for each project:
...
steps:
# a script task that let's you use any CLI available on the DevOps build agent, also uses a variable for the build config
- script: dotnet build MyFirstProjectWithinSolution\MyFirstProject.csproj --configuration $(buildConfiguration)
displayName: 'dotnet build MyFirstProject'
# other steps removed, e.g. run and publish tests
- script: dotnet publish MyFirstProjectWithinSolution\MyFirstProject.csproj --configuration $(buildConfiguration) --output MyFirstArtifact
displayName: 'dotnet publish MyFirstProject'
# a DevOps named task called CopyFiles (which is version 2 = #2), DevOps supplies lots of standard tasks you can make use of
- task: CopyFiles#2
inputs:
contents: 'MyFirstProjectWithinSolution\MyFirstArtifact\**'
targetFolder: '$(Build.ArtifactStagingDirectory)'
# now publish the artifact which makes it available to the release pipeline, doing so into a sub folder allows multiple artifacts to be dealt with
- task: PublishBuildArtifacts#1
displayName: 'publish MyFirstArtifact artifact'
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)\MyFirstProjectWithinSolution\MyFirstArtifact'
artifactName: MyFirstArtifact
# now repeat the above for every project you need to deploy, each in their own artifact sub-folder
Next you create a release, which in its simplest form picks up the artifacts and does one or more deployment, here's a simple one which deploys two function app projects:
Within a deployment stage (right hand side above), you can define your release process, again in its simplest form you can just deploy straight to production or to a slot, although until function slots are out of preview you could also spin up another function app and deploy and test there.
This screenshot shows a simple deployment which uses a standard Azure Function App deployment from Azure DevOps:
Within your deployment stage you can define which artifact is deployed and after running your build pipeline for the first time you'll get to see all the available artifacts that it created.
All or parts of the above can be automated from pushing a branch (or other triggers such as on a schedule). Notifications and "gates" can be added as well if you want manual intervention before release or between release stages.
There are also other ways to cut this up, eg with multiple build pipelines, it’s basically completely flexible but the above are the elements you can use to deploy one or more function apps at a time.

Find Azure slot location in azure App Service Deploy

How to find Destination path of deployed web site on Azure App Service Deploy - Azure Slot ?
I need to delete existing old files on a destination specific folder which are generated uniquely on each release(during build process).
This has to be done during pre-deployment.
I used Delete Files Task which is not deleting. Here is YAML.
In Kudu , I am able to see my web site location is D:\home\site\wwwroot\
I wanted to delete files from the path D:\home\site\wwwroot\scripts\libs>
steps:
task: DeleteFiles#1
displayName: 'Delete files from $(build.artifactstagingdirectory)'
inputs:
SourceFolder: '$(build.artifactstagingdirectory)'
Contents: 'scripts\libs**'
You don't have to manually delete old files on your App Service Instance or its Deployment Slots. When you are using Azure App Service Deploy task you have the option built-in to remove any unused files at the destination (in this case App Service Instance/Deployment slot). Here is how to do it.
On the Azure App Service Deploy task,
Expand Additional Deployment Options.
There make sure Select Deployment Method checkbox is selected
From the Deployment Method drop down, Select Web Deploy
Select the Remove additional files at destination checkbox
If you want to keep any file that is installed with App Service Extensions or WebJobs select Exclude files from the App_Data folder as well
What this will do is it will compare the files that are already at the destination (App Service) with the files that will be copied over from the Artifacts. Then it will delete any file that is not available in the artifacts that you are doing to deploy.
Find Azure slot location in azure App Service Deploy
You may not delete the correct folder.
According to the first image in your question, we could to know you are deleting the file from the folder $(build.artifactstagingdirectory).
As we know, the folder $(build.artifactstagingdirectory) is the local path on the agent where any artifacts are copied to before being pushed to their destination.
Check Build variables for some more details.
However, I do not find any task in your build definition to copy file to the artifacts.
Besides, you said you delete existing old files on a destination specific folder which are generated uniquely on during build process.
So, I am bold to guess that the file you want to delete should be in System.DefaultWorkingDirectory instead of artifacts. But I am not very sure about it, since I could not distinguish folder type from the path D:\home\site\wwwroot\scripts\libs>.
So, to resolve this issue, you can try to delete the file from $(System.DefaultWorkingDirectory).
Or you can use a Inline Powershell task to outcome the value of $(build.artifactstagingdirectory):
Write-Output '$(Build.ArtifactStagingDirectory)'
Then check if the value is match the path of folder you want to delete D:\home\site\wwwroot\scripts\libs>.
Hope this helps.

Resources