|
| 1 | +parameters: |
| 2 | + - name: SpecRepoUrl |
| 3 | + type: string |
| 4 | + - name: SdkRepoUrl |
| 5 | + type: string |
| 6 | + - name: SpecRepoCommit |
| 7 | + type: string |
| 8 | + default: 'HEAD' |
| 9 | + - name: SdkRepoCommit |
| 10 | + type: string |
| 11 | + default: 'HEAD' |
| 12 | + - name: ConfigType |
| 13 | + type: string |
| 14 | + values: |
| 15 | + - 'TypeSpec' |
| 16 | + - 'OpenAPI' |
| 17 | + default: 'TypeSpec' |
| 18 | + displayName: 'API specification type' |
| 19 | + - name: ConfigPath |
| 20 | + type: string |
| 21 | + default: 'specification/contosowidgetmanager/Contoso.Management/tspconfig.yaml' |
| 22 | + displayName: 'Path to API specification file (TypeSpec/OpenAPI)' |
| 23 | + - name: SkipPullRequestCreation |
| 24 | + type: boolean |
| 25 | + default: false |
| 26 | + |
| 27 | +jobs: |
| 28 | +- job: |
| 29 | + displayName: 'SDK Generation' |
| 30 | + |
| 31 | + variables: |
| 32 | + - template: /eng/pipelines/templates/variables/image.yml |
| 33 | + - name: NodeVersion |
| 34 | + value: '22.13.x' |
| 35 | + - name: PythonVersion |
| 36 | + value: '3.13' |
| 37 | + - name: SpecGenSdkVersion |
| 38 | + value: 'latest' |
| 39 | + - name: SdkArtifactName |
| 40 | + value: SDK_Artifact |
| 41 | + |
| 42 | + pool: |
| 43 | + name: $(LINUXPOOL) |
| 44 | + vmImage: $(LINUXVMIMAGE) |
| 45 | + |
| 46 | + steps: |
| 47 | + - checkout: none |
| 48 | + |
| 49 | + - pwsh: | |
| 50 | + $urlPattern = '^https://github\.com/(?<organization>[^/]+)/(?<repository>[^/]+)' |
| 51 | + if ('${{ parameters.SpecRepoUrl }}' -match $urlPattern) { |
| 52 | + $specRepoOwner = $Matches['organization'] |
| 53 | + Write-Host "##vso[task.setvariable variable=SpecRepoOwner]$specRepoOwner" |
| 54 | + Write-Host "SpecRepoOwner variable set to: $specRepoOwner" |
| 55 | +
|
| 56 | + $specRepoName = $Matches['repository'] |
| 57 | + Write-Host "##vso[task.setvariable variable=SpecRepoName]$specRepoName" |
| 58 | + Write-Host "SpecRepoName variable set to: $specRepoName" |
| 59 | +
|
| 60 | + $specRepoDirectory = "$(System.DefaultWorkingDirectory)/$specRepoName" |
| 61 | + Write-Host "##vso[task.setvariable variable=SpecRepoDirectory]$specRepoDirectory" |
| 62 | + Write-Host "SpecRepoDirectory variable set to: $specRepoDirectory" |
| 63 | + } |
| 64 | +
|
| 65 | + if ('${{ parameters.SdkRepoUrl }}' -match $urlPattern) { |
| 66 | + if ('${{ parameters.SpecRepoUrl }}'.EndsWith('-pr') -and (-not '${{ parameters.SdkRepoUrl }}'.EndsWith('-pr'))) { |
| 67 | + Write-Host "SdkRepoUrl must be a private repository if SpecRepoUrl is a private repository." |
| 68 | + Exit 1 |
| 69 | + } |
| 70 | +
|
| 71 | + $sdkRepoOwner = if ('$(SdkRepoOwner)' -eq '') { $Matches['organization'] } else { '$(SdkRepoOwner)' } |
| 72 | + Write-Host "##vso[task.setvariable variable=SdkRepoOwner]$sdkRepoOwner" |
| 73 | + Write-Host "SdkRepoOwner variable set to: $sdkRepoOwner" |
| 74 | +
|
| 75 | + $sdkRepoName = $Matches['repository'] |
| 76 | + Write-Host "##vso[task.setvariable variable=SdkRepoName]$sdkRepoName" |
| 77 | + Write-Host "SdkRepoName variable set to: $sdkRepoName" |
| 78 | +
|
| 79 | + $sdkRepoDirectory = "$(System.DefaultWorkingDirectory)/$sdkRepoName" |
| 80 | + Write-Host "##vso[task.setvariable variable=SdkRepoDirectory]$sdkRepoDirectory" |
| 81 | + Write-Host "SdkRepoDirectory variable set to: $sdkRepoDirectory" |
| 82 | + } |
| 83 | +
|
| 84 | + if ([string]::IsNullOrEmpty($SpecRepoOwner) -or [string]::IsNullOrEmpty($SpecRepoName) -or [string]::IsNullOrEmpty($SdkRepoOwner) -or [string]::IsNullOrEmpty($SdkRepoName)) { |
| 85 | + Write-Host "One or more required variables is empty or invalid. Ensure that SpecRepourl and SdkRepoUrl are set to valid GitHub repository URLs." |
| 86 | + Exit 1 |
| 87 | + } |
| 88 | +
|
| 89 | + $sdkRepoNameForSpecGen = $sdkRepoName |
| 90 | + if ($sdkRepoName.EndsWith('-for-net')) { |
| 91 | + $sdkRepoNameForSpecGen = $sdkRepoName + "-track2" |
| 92 | + } |
| 93 | + Write-Host "##vso[task.setvariable variable=SdkRepoNameForSpecGen]$sdkRepoNameForSpecGen" |
| 94 | + Write-Host "SdkRepoNameForSpecGen variable set to: $sdkRepoNameForSpecGen" |
| 95 | + displayName: "Create Run Time Variables" |
| 96 | +
|
| 97 | + - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml |
| 98 | + parameters: |
| 99 | + Paths: |
| 100 | + - '/*' |
| 101 | + - '!sdk/**/test-recordings/*' |
| 102 | + - '!sdk/**/recordings/*' |
| 103 | + - '!sdk/**/SessionRecords/*' |
| 104 | + - '!sdk/**/session-records/*' |
| 105 | + Repositories: |
| 106 | + - Name: $(SpecRepoOwner)/$(SpecRepoName) |
| 107 | + Commitish: ${{ parameters.SpecRepoCommit }} |
| 108 | + WorkingDirectory: $(SpecRepoDirectory) |
| 109 | + - Name: $(SdkRepoOwner)/$(SdkRepoName) |
| 110 | + Commitish: ${{ parameters.SdkRepoCommit }} |
| 111 | + WorkingDirectory: $(SdkRepoDirectory) |
| 112 | + SkipCheckoutNone: true |
| 113 | + |
| 114 | + - script: | |
| 115 | + if [ "${{ parameters.SpecRepoCommit }}" = "HEAD" ]; then |
| 116 | + cd $(SpecRepoDirectory) |
| 117 | + default_commit=$(git rev-parse HEAD) |
| 118 | + echo "##vso[task.setvariable variable=SpecRepoCommit]$default_commit" |
| 119 | + echo "SpecRepoCommit variable set to default commit: $default_commit" |
| 120 | + else |
| 121 | + echo "##vso[task.setvariable variable=SpecRepoCommit]${{ parameters.SpecRepoCommit }}" |
| 122 | + echo "SpecRepoCommit variable set to: ${{ parameters.SpecRepoCommit }}" |
| 123 | + fi |
| 124 | + displayName: 'Set SpecRepoCommit variable' |
| 125 | +
|
| 126 | + - task: NodeTool@0 |
| 127 | + inputs: |
| 128 | + versionSpec: $(NodeVersion) |
| 129 | + displayName: 'Install Node.js' |
| 130 | + |
| 131 | + - task: UsePythonVersion@0 |
| 132 | + inputs: |
| 133 | + versionSpec: $(PythonVersion) |
| 134 | + |
| 135 | + - script: | |
| 136 | + npm install -g @azure-tools/spec-gen-sdk@$(SpecGenSdkVersion) |
| 137 | + displayName: 'Install spec-gen-sdk' |
| 138 | +
|
| 139 | + - script: | |
| 140 | + optional_params="" |
| 141 | + sdk_gen_info="sdk generation from Config : " |
| 142 | +
|
| 143 | + if [ "${{ parameters.ConfigType }}" = "TypeSpec" ]; then |
| 144 | + optional_params="$optional_params --tsp-config-relative-path ${{ parameters.ConfigPath }}" |
| 145 | + sdk_gen_info="$sdk_gen_info '${{ parameters.ConfigPath }}'," |
| 146 | + else |
| 147 | + optional_params="$optional_params --readme-relative-path ${{ parameters.ConfigPath }}" |
| 148 | + sdk_gen_info="$sdk_gen_info '${{ parameters.ConfigPath }}'," |
| 149 | + fi |
| 150 | +
|
| 151 | + sdk_gen_info="$sdk_gen_info and CommitSHA: '$(SpecRepoCommit)', in SpecRepo: '${{ parameters.SpecRepoUrl }}'" |
| 152 | + echo "##vso[task.setvariable variable=GeneratedSDKInformation]$sdk_gen_info" |
| 153 | + echo "Generated SDK Information : $sdk_gen_info" |
| 154 | +
|
| 155 | + spec-gen-sdk \ |
| 156 | + --scp "$(SpecRepoDirectory)" \ |
| 157 | + --sdp "$(SdkRepoDirectory)" \ |
| 158 | + --wf "$(System.DefaultWorkingDirectory)" \ |
| 159 | + -l "$(SdkRepoNameForSpecGen)" \ |
| 160 | + -c "$(SpecRepoCommit)" \ |
| 161 | + -t $true \ |
| 162 | + $optional_params |
| 163 | + displayName: 'Generate SDK' |
| 164 | +
|
| 165 | + - task: PublishPipelineArtifact@1 |
| 166 | + displayName: Publish SDK Artifact to Pipeline Artifacts |
| 167 | + inputs: |
| 168 | + artifactName: $(sdkArtifactName) |
| 169 | + targetPath: "$(System.DefaultWorkingDirectory)/generatedSdkArtifacts" |
| 170 | + |
| 171 | + - ${{ if eq(parameters.SkipPullRequestCreation, false) }}: |
| 172 | + - template: /eng/common/pipelines/templates/steps/git-push-changes.yml |
| 173 | + parameters: |
| 174 | + BaseRepoBranch: $(PrBranch)-$(Build.BuildId) |
| 175 | + BaseRepoOwner: azure-sdk |
| 176 | + CommitMsg: $(GeneratedSDKInformation) |
| 177 | + TargetRepoOwner: $(SdkRepoOwner) |
| 178 | + TargetRepoName: $(SdkRepoName) |
| 179 | + PushArgs: "--force" |
| 180 | + WorkingDirectory: $(SdkRepoDirectory) |
| 181 | + ScriptDirectory: $(SdkRepoDirectory)/eng/common/scripts |
| 182 | + |
| 183 | + - task: PowerShell@2 |
| 184 | + displayName: Create pull request |
| 185 | + condition: and(succeeded(), eq(variables['HasChanges'], 'true')) |
| 186 | + inputs: |
| 187 | + pwsh: true |
| 188 | + workingDirectory: $(SdkRepoDirectory) |
| 189 | + filePath: $(SdkRepoDirectory)/eng/common/scripts/Submit-PullRequest.ps1 |
| 190 | + arguments: > |
| 191 | + -RepoOwner "$(SdkRepoOwner)" |
| 192 | + -RepoName "$(SdkRepoName)" |
| 193 | + -BaseBranch "main" |
| 194 | + -PROwner "azure-sdk" |
| 195 | + -PRBranch "$(PrBranch)-$(Build.BuildId)" |
| 196 | + -AuthToken "$(azuresdk-github-pat)" |
| 197 | + -PRTitle "$(PrTitle)-generated-from-$(Build.DefinitionName)-$(Build.BuildId)" |
| 198 | + -PRBody "$(GeneratedSDKInformation)" |
| 199 | + -OpenAsDraft $true |
0 commit comments