Is it possible to edit the YAML of a release pipeline? - azure

I'm working on my first Azure DevOps Services release pipeline. I've got a secret in a variable group in the Azure DevOps | Library area. I've been struggling with getting to that secret so I can use it in the release pipeline. I see by this article Add & use variable groups on Microsoft's Docs that I must add that in the variables section of the YAML file. However, I've not seen any way to edit the release pipeline YAML file. I can view it, but there appears to be all. If there's a way of editing the YAML file, how do I get to it?

You should be able to do that, earlier To enable YAML release pipeline, you need to activate Multi-stage YAML pipelines in Preview features:
From the Docs,
To help make it even easier to edit and update your pipeline, we’ve
created an in-product editor with IntelliSense smart code completion,
and an easy task assistant

From the documentation you're pointing at.
If you're using YAML pipelines, you can reference the variable group in your YAML pipeline:
variables:
- group: my-variable-group
If you're using the classic mode, you can link the variable group to your pipeline through the UI:

Related

Azure devops - How to use same yml for multiple git repos

I use github for hosting my projects and have multiple projects in github. And I use Azure devops for CICD alone. I have a single project in Azure devops, where I create individual pipeline corresponding to each project in my github repo. All these github projects would need to use the same azure-pipeline.yml for build. So instead of keeping the same yml file in each project, is there a way I can keep this yml centrally. So that in future, if at all a change is required, I need not do it for all individual projects, instead, update the main yml template.
A single yml file where I have all the code is even possible for my usecase? Any help is much appreciated
Have you considered using templates? Essentially you have would end up with a single template containing the main build steps that is reusable and individual yaml for each pipeline that can pass parameters to the template for any differences you have between them (such as different triggers or variable values). This way you can update all pipelines by making changes to the template
Template documentation
According to your description, you may setup a repo contains all the YAML files for pipelines. Kindly also be advised that we can also keep the templates in other repositories, if we have defined the repository resources in the core YAML pipeline. Kindly refer to the sample Core and template YAML files below.
#Core YAML in Azure Repos
trigger: none
pool:
  vmImage: ubuntu-latest
resources:
  repositories:
  - repository: GitHub_REPO_1
    type: github
    name: GitHubAccountName/GitHubRepo1
    endpoint: GitHubServiceConnectionName
  - repository: GitHub_REPO_2
    type: github
    name: GitHubAccountName/GitHubRepo2
    endpoint: GitHubServiceConnectionName
steps:
  - checkout: none
  # - checkout: GitHub_REPO_1
  - template: GHREPO1.yml#GitHub_REPO_1
  # - checkout: GitHub_REPO_2
  - template: GHREPO2.yml#GitHub_REPO_2
#Template YAML from GitHub Repo
steps:
- script: echo "This YAML template is from GitHubRepo1"
  displayName: 'Template From GitHubRepo1'
By the way, we could also checkout the code from one or multiple repository resource(s) and trigger the pipeline by the commits from the repository resources. Please refer to the following documents for more information.
Define YAML resources for Azure Pipelines - Azure Pipelines | Microsoft Docs
Check out multiple repositories in your pipeline - Azure Pipelines | Microsoft Docs

Environments are not automatically created in Azure Devops when declared in yaml pipeline config

I'm new to Azure Devops. I would like to have devops pipeline environments to be created automatically during pipeline flow. So the 5th line below should create environment if it does not exist:
- deployment: Deploy
displayName: Deploy job
pool:
vmImage: $(vmImageName)
environment: 'production'
Instead I'm getting:
What am I missing?
To automate environment creation I could also use Terraform but this time I cannot find terraform resource config responsible for that.
I had a similar problem and found that the documentation lists some possible reasons for why this can happen:
Quote from learn.microsoft.com:
Q: Why am I getting error "Job XXXX: Environment XXXX could not be
found. The environment does not exist or has not been authorized for
use"?
A: These are some of the possible reasons of the failure:
When you author a YAML pipeline and refer to an environment that does not exist
in the YAML file, Azure Pipelines automatically creates the
environment in some cases:
You use the YAML pipeline creation wizard in the Azure Pipelines web
experience and refer to an environment that hasn't been created yet.
You update the YAML file using the Azure Pipelines web editor and save
the pipeline after adding a reference to an environment that does not
exist.
In the following flows, Azure Pipelines does not have
information about the user creating the environment: you update the
YAML file using another external code editor, add a reference to an
environment that does not exist, and then cause a manual or continuous
integration pipeline to be triggered. In this case, Azure Pipelines
does not know about the user. Previously, we handled this case by
adding all the project contributors to the administrator role of the
environment. Any member of the project could then change these
permissions and prevent others from accessing the environment.
If you are using runtime parameters for creating the environment, it
will fail as these parameters are expanded at run time. Environment
creation happens at compile time, so we have to use variables to
create the environment.
A user with stakeholder access level cannot create the environment as
stakeholders do not access to repository.
In our case, the problem was using runtime parameters for creating the environment.
You have the environment name 'production' hardcoded, so your problem might be related to one of the other cases.

Azure DevOps - Automated Pipeline Creation

I'm new to Azure DevOps, and I was wondering if there was a way to automatically detected a .yml build file and create a pipeline without having to interact with the site.
I have tried creating a file called azure-pipelines.yml in the root of the repo, with no luck.
Is there anyway to automatically create pipelines? Like how Jenkins detects a Jenkinsfile?
No this is nott possible out of the box, because YAML file is not always pipeline definition. You my try to figure out if it is trully is, however you need to listen for repo changes and in fact you can do this via another pipeline ;) for instance as this:
check if commit has a new yaml file added
verify if the file is pipeline
create a pipeline using azure cli (for instance)
However, this would be quite a lot of work and then you need to create such pipeline in every repo you want to have this detection enabled.

Azure DevOps dynamic Release Pipeline creation

I am currently planning on a type of multi-tenant system, were different resource groups with a set of AppServices are deployed for customers via ARM Templates. Hence, each customer has its own Resource Group and set of AppServices. Currently we use Azure DevOps to deploy to a set of AppServices used for Development and Quality Assurance before it gets to Production. I am now trying to incorporate DevOps into the mix, automating a pipeline creation of some sort... (it would be a copy of an existing pipeline but only changing the Target AppServices). Which is were my question comes from, Is there a way to dynamically create or edit a Release pipeline to add the deployment of those new AppServices, without the need of manually edit or create a pipeline an adding those newly created AppServices, I was thinking something around the lines of being able to copy a yaml file template then replacing the necessary info to point to those AppServices after they have been created, but I am not totally sure where could I store the new yaml file so that it is picked up by Azure DevOps, or how could I would accomplish these, with the main idea being that all of this continues to be part of an automated process (if possible).
Thanks a lot for any help, any suggestion is appreciated.
EDIT:
The question is not about how to Deploy an ARM Template through the DevOps release pipeline (I plan on using a PowerShell Script/REST API to accomplish that), instead, is about when the AppServices Resources are created, I need to deploy code to those newly created AppServices and also update that code when necessary (Hopefully through a Release Pipeline), somehow generate a new release pipeline each time I deploy a new set of Resources. So that, when there is a new update, I could easily have that pipeline triggered and that set if AppServices can be updated (created as part of the automation process "dynamically"). (I Already have a similar pipeline that deploys to a "static" set of AppServices).
This is possible as you eluded to with YAML Pipelines. Based upon the scenario you have subscribed each repository would have it's own pipeline.yml file that will define the trigger, pool etc. It would also reference a repository that will house your yaml template.
The template would accept whichever parameters you may required (resource group, app service name, etc...) The triggering pipeline associated with each repository would pass this information leveraging the teamplate.
By doing this CI/CD can be set up to trigger on the individual pipelines and deploy the appropriate code all while leveraging the same YAML template.
The repository reference would be similar to:
resources:
repositories:
- repository: YAMLTemplates
type: git
name: OrginazationName/YAML Project Name
With the call to the template being similar to:
- template: azure-ARM-template.yml#YAMLTemplate
parameters:
appServiceName: 'AppServiceName'
resourceGroupName: 'ResourceGroupName'
UPDATE
At a high level the YAML pipeline would consist of the following. If all App Services are similar as stated and ARM Templates are similar this how it could be constructed and triggered based on a folder path:
Build necessary artifacts
Publish Pipeline
Deploy Azure Resource Group Task
Deploy App Settings Task (if applicable)
Deploy App Service
Release the deployment pieces for each environment in appropriate stages to help alleviate the amount of copying and pasting each of the above tasks can be part of a template either individually at a task, combination of tasks, or all in one. This would allow for defining the YAML once and referencing it and including app specific components as needed as parameters to the templates.

How do i identify my AzureDevOps id for a pipeline in ADO Pipelines?

So I am attempting to create a downstream project trying to use an artifact stored in azure pipeline artifact to build. I am using the task
DownloadPipelineArtifact#0
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-pipeline-artifact?view=azure-devops
It talks about the need for a pipelineId, not really sure where to find out the id for my other pipeline. Is there any easy way, its supposed to be a ~4 digit number according the documentation.
Thanks
Go to the target pipeline you want -> Edit.
Check the URL. There you have the pipeline id.
.../_apps/hub/ms.vss-build-web.ci-designer-hub?pipelineId=1234&branch=main
I'm sorry I could not find a proper way to refer this without hardcoding.
There is an existing open issues on the pipeline ID.
The doc which you mentioned doesn't provide much information about pipelineID.
As per microsoft
pipelineId appears to be BuildId, and not the build definition id. It
needs the actual instance id of where the artifact is associated. I
was able to make this work by referencing a release variable tied to
the artifact alias. My alias is named "artifacts" and using
$(RELEASE_ARTIFACTS_ARTIFACTS_BUILDID) did the trick. So the format
would be $(RELEASE_ARTIFACTS_<alias>_BUILDID)
If you were trying to consume in a build and not a release pipeline
you would need to somehow get the value of $(Build.BuildId)
I hope as this matures there are plans to make pipeline artifacts
published from a build automatically in release, just like they are
when using the old Build Artifacts. Currently for me that is not
happening so I am forced to manually add this step to my release
pipeline and associate it with the build pipeline.
I was facing the same problem in my azure devops pipelines, I don't know if it applies the same way for you, but here is my solution to do it :
There is the function az pipeline show that gives you the id of a pipeline with its name:
Pipeline_to_find="$1"
pipelineInfo=$(az pipelines show --name "$Pipeline_to_find")
id=$(echo "$pipelineInfo" | python -c "import sys, json; print(json.load(sys.stdin)['id'])")
#export this var to be used in any other task of your pipeline
echo "##vso[task.setvariable variable=id;]$id"
You can get pipeline ID from a pipeline directly from portal.
Go Azure Pipeline
Now select the pipeline you want the ID from and choose “Edit”
Once in EDIT PIPELINE mode, click the dotted menu and select “TRIGGERS”
Now, click on “variables” tab
Here you will see a variable — system.definitionId which is aka PipelineId
There are two IDs you may need to know in Azure Pipelines.
Build Pipeline ID / Definition ID: This is the ID of the Pipeline not a particular run of the pipeline. You can get it via System.DefinitionId
Build Record ID / Build ID: This is the ID for a particular run/record of your pipeline run. You can access it in your pipeline as Build.BuildId
In your case, you will need to use the Build.BuildId since you are trying to get the artifact from a particular run of a pipeline.
Reference: Predefined Azure Pipeline Variables
The following command uses Azure CLI (with DevOps extension) and jq to get the pipeline id in Bash shell:
az pipelines show --name <PIPELINE_NAME> | jq -r .id
If you want to use this inside an Azure Pipeline, you need to use an Azure CLI task and probably install jq on the run agent.
See ultimatom's answer for how to set the id as a variable in the pipeline.

Resources