-
Notifications
You must be signed in to change notification settings - Fork 0
AzDoGitRepository
The AzDoGitRepository DSC resource is used to create and manage Git repositories within Azure DevOps projects. It allows you to define the desired state of a Git repository, including its name and default branch settings.
AzDoGitRepository [string] #ResourceName
{
ProjectName = [String] $ProjectName
RepositoryName = [String] $RepositoryName
[ Ensure = [String] {'Present', 'Absent'} ]
[ DefaultBranch = [String] $DefaultBranch ]
[ DependsOn = [String[]] ]
[ PsDscRunAsCredential = [PSCredential] ]
}-
ProjectName [String] - The name of the project that contains or will contain this repository.
-
RepositoryName [String] - The name of the Git repository to create or manage.
-
Ensure [String] - Desired state of the resource:
-
'Present'- (default) Repository should exist -
'Absent'- Repository should be removed
-
-
DefaultBranch [String] - The default branch for the repository (e.g., 'main', 'master'). This is the branch that will be checked out by default.
-
DependsOn [String[]] - Dependencies on other resources. Typically depends on the project existing first.
-
PsDscRunAsCredential [PSCredential] - Credentials to run this resource under.
The resource returns the following properties:
- RepositoryName - The name of the repository
- ProjectName - The project containing the repository
- Ensure - Current state ('Present' or 'Absent')
- RepositoryId - The unique identifier of the repository
- DefaultBranch - The default branch
- RepositoryUrl - The HTTP(S) URL of the repository
- SshUrl - The SSH URL of the repository
Configuration CreateGitRepository {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
# First create the project
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
# Then create the repository
AzDoGitRepository 'MainRepository' {
Ensure = 'Present'
ProjectName = 'MyProject'
RepositoryName = 'MainRepository'
DefaultBranch = 'main'
DependsOn = '[AzDoProject]MyProject'
}
}
}
CreateGitRepository
Start-DscConfiguration -Path ./CreateGitRepository -Wait -VerboseConfiguration CreateMultipleRepositories {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoProject 'CodeProject' {
Ensure = 'Present'
ProjectName = 'CodeProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
# Create main repository
AzDoGitRepository 'Main' {
Ensure = 'Present'
ProjectName = 'CodeProject'
RepositoryName = 'main-app'
DefaultBranch = 'main'
DependsOn = '[AzDoProject]CodeProject'
}
# Create development repository
AzDoGitRepository 'Dev' {
Ensure = 'Present'
ProjectName = 'CodeProject'
RepositoryName = 'dev-environment'
DefaultBranch = 'develop'
DependsOn = '[AzDoProject]CodeProject'
}
# Create infrastructure repository
AzDoGitRepository 'Infrastructure' {
Ensure = 'Present'
ProjectName = 'CodeProject'
RepositoryName = 'infrastructure-as-code'
DefaultBranch = 'main'
DependsOn = '[AzDoProject]CodeProject'
}
}
}
CreateMultipleRepositories
Start-DscConfiguration -Path ./CreateMultipleRepositories -Wait -VerboseConfiguration RepositoryWithPolicies {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoProject 'SecureProject' {
Ensure = 'Present'
ProjectName = 'SecureProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
# Create repository
AzDoGitRepository 'ProductionRepo' {
Ensure = 'Present'
ProjectName = 'SecureProject'
RepositoryName = 'production'
DefaultBranch = 'main'
DependsOn = '[AzDoProject]SecureProject'
}
# Configure branch policy for main branch
AzDoBranchPolicy 'MainBranchPolicy' {
RepositoryName = 'production'
ProjectName = 'SecureProject'
BranchName = 'main'
RequireReviewCount = 2
AllowSelfApproval = $false
DependsOn = '[AzDoGitRepository]ProductionRepo'
}
}
}
RepositoryWithPolicies
Start-DscConfiguration -Path ./RepositoryWithPolicies -Wait -Verbose# Get current state of a repository
$properties = @{
ProjectName = 'MyProject'
RepositoryName = 'MainRepository'
}
$result = Invoke-DscResource -Name 'AzDoGitRepository' `
-Method Get `
-Property $properties `
-ModuleName 'AzureDevOpsDscNative'
Write-Host "Repository URL: $($result.RepositoryUrl)"
Write-Host "SSH URL: $($result.SshUrl)"
Write-Host "Default Branch: $($result.DefaultBranch)"Configuration RemoveRepository {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoGitRepository 'RemoveRepo' {
Ensure = 'Absent'
ProjectName = 'MyProject'
RepositoryName = 'OldRepository'
}
}
}
RemoveRepository
Start-DscConfiguration -Path ./RemoveRepository -Wait -VerboseConfiguration RepositoryWithSettings {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
AzDoProject 'AdvancedProject' {
Ensure = 'Present'
ProjectName = 'AdvancedProject'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
AzDoGitRepository 'ConfiguredRepo' {
Ensure = 'Present'
ProjectName = 'AdvancedProject'
RepositoryName = 'configured-app'
DefaultBranch = 'main'
DependsOn = '[AzDoProject]AdvancedProject'
}
# Configure repository settings
AzDoRepositorySettings 'RepoSettings' {
ProjectName = 'AdvancedProject'
RepositoryName = 'configured-app'
EnablePrAutocompletion = $true
DisableComments = $false
DependsOn = '[AzDoGitRepository]ConfiguredRepo'
}
}
}
RepositoryWithSettings
Start-DscConfiguration -Path ./RepositoryWithSettings -Wait -Verbose- Repository names must be unique within the project
- Names are case-insensitive for Git but case-sensitive for display
- Use descriptive names for clarity
- The default branch is used when cloning the repository
- Common conventions:
mainormaster - The branch should exist before setting it as default
After creation, the repository will have:
- HTTP(S) URL: Use for HTTPS-based Git operations
- SSH URL: Use for SSH-based Git operations (requires SSH key setup)
This resource requires a project to exist first. Always define DependsOn to ensure the project is created before the repository.
Cause: The specified project doesn't exist
Solution:
# Ensure the project is created first
DependsOn = '[AzDoProject]MyProject'Cause: A repository with the same name already exists in the project
Solution:
- Use a unique repository name
- Or ensure the existing repository is removed first
Cause: The specified default branch doesn't exist
Solution:
# Use 'main' or 'master' as these are created automatically
DefaultBranch = 'main'- AzDoProject - Create and manage projects
- AzDoGitPermission - Manage repository permissions
- AzDoRepositorySettings - Configure repository settings
- AzDoBranchPolicy - Configure branch policies