Ranjan
Ranjan

Reputation: 65

How to Share Azure DevOps Environment Across Multiple Repositories for Deployment?

I am working on an Azure DevOps setup where each repository is located in a different project. I created an environment (e.g., Production) in one project to manage deployments on a virtual machine (VM). However, when I try to access this environment from a repository in another project, I encounter issues like the environment not being visible or accessible.

Scenario:

  1. I created an environment Production in Project A within Repository A and added a VM (MyResource) as a resource.
  2. Now, I want to use this environment (Production) in Project B for a pipeline in Repository B, targeting the same VM (MyResource).
  3. However, Repository B cannot access the Production environment. It seems I would need to recreate the environment in each project, which duplicates the setup unnecessarily.

My Goal:


Example Pipeline (Repository B):

Here’s a simplified version of the YAML pipeline in Repository B:

jobs:  
- deployment: DeployToVM  
  displayName: 'Deploy to VM'  
  environment:   
    name: 'Production' # Environment created in Project A  
    resourceName: 'MyResource'  
    resourceType: VirtualMachine  

  strategy:  
    runOnce:  
      deploy:  
        steps:  
        - script: echo "Deploying to shared VM"  

What I Have Tried:

  1. Verified that the Production environment exists in the Environments section of Project A.
  2. Attempted to reference the environment in Repository B's pipeline YAML, but it results in errors like:
    "Resource MyResource does not exist in environment Production."

My Questions:

  1. How can I configure Azure DevOps so that multiple repositories across different projects can access the same environment?
  2. Is there a better way to share environments/resources across Azure DevOps projects for centralized deployment management?

Upvotes: 0

Views: 100

Answers (1)

Miao Tian-MSFT
Miao Tian-MSFT

Reputation: 5522

Your description of "created an environment Production in Repository A" and "access this environment from another repository" is not quite accurate.

The environment is not created or used in the repository, but in the pipeline.

environment in pipeline

Pipelines in the same project can use the environment in the current project by modifying the pipeline permissions, no matter which repository the pipeline is using. A project can have many repositories.

pipeline permissions

Therefore, your question should probably be changed to "How to Share Azure DevOps Environment Across Multiple Projects for Deployment?"

However, currently Environment cannot be shared between projects. You can request the feature for Azure DevOps here to help the feature better.

Workaround 1:

You can consider using the deployment group of the classic release pipeline. Deployment groups are available only for Classic release pipelines.

classic release pipeline

You can share deployment groups with other projects in the organization.

share

If you can't find the where to create the classic release pipeline, you can enable creation of classic pipelines by toggling the option at the organization level or project level.

For example, to enable it at the organization level, navigate to your organization settings > select Settings > turn off Disable creation of classic release pipelines.

 organization

Workaround 2:

Create the pipelines in the project where the environment is created and check out repositories from other projects in the pipeline.

For example:

resources:
  repositories:
  - repository: B
    type: git
    name: ProjectBName/RepoName
    ref: main
    trigger:
    - main

jobs:
- deployment: DeployToVM  
  displayName: 'Deploy to VM'  
  environment:   
    name: 'Production' # Environment created in Project A  
    resourceName: 'MyResource'  
    resourceType: VirtualMachine  

  strategy:  
    runOnce:  
      deploy:  
        steps:  
      # - checkout: self
        - checkout: B
        - script: echo "Deploying to shared VM" 

This way, we only need to create the environment with VM Resource in one project.

Upvotes: 1

Related Questions