apiops icon indicating copy to clipboard operation
apiops copied to clipboard

[Question]How to change the settings to only publish certain API? Is it supported for this tool or latest version?

Open setyg opened this issue 5 months ago • 2 comments

Release version

apiops verion v5.1.4

Question Details

unable to publish individual APIM even I configure the yaml:

trigger: none parameters:

  • name: API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH type: string displayName: Folder where the artifacts reside default: "artifacts"
  • name: COMMIT_ID type: string displayName: Choose "publish-all-artifacts-in-repo" only when you want to force republishing all artifacts (e.g. after build failure). Otherwise stick with the default behavior of "publish-artifacts-in-last-commit" default: publish-artifacts-in-last-commit values:
    • publish-artifacts-in-last-commit
    • publish-all-artifacts-in-repo
  • name: SH01_API_NAMES type: object displayName: List of API folders to publish default:
    • sh01-kabsa
    • sh01-resource
  • name: CONFIGURATION_YAML_PATH type: string displayName: Optional configuration file default: "$(Build.SourcesDirectory)/configuration.uat.yaml"

variables:

  • group: apim-automation
  • name: System.Debug value: true

stages:

  • stage: push_changes_to_uat_APIM displayName: Push changes to Uat APIM jobs:
    • job: push_changes_to_Uat_APIM displayName: Push changes to Uat APIM pool: vmImage: ubuntu-latest steps:

      • script: echo Provided configuration was $(Build.SourcesDirectory)/configuration.uat.yaml displayName: Print the name of the yaml configuration file if provided

      • script: echo Provided app service name was $(APIM_NAME_UAT) displayName: Print the name of the apim service name if provided

      • checkout: self fetchDepth: 0

      • task: AzureCLI@2 displayName: Set publishing variables inputs: azureSubscription: "$(SERVICE_CONNECTION_NAME)" scriptType: pscore scriptLocation: inlineScript inlineScript: | Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $VerbosePreference = "Continue" $InformationPreference = "Continue"

        Write-Host "##vso[task.setvariable issecret=true;variable=AZURE_BEARER_TOKEN]$(az account get-access-token --query "accessToken" --output tsv)"
        Write-Host "##vso[task.setvariable issecret=true;variable=AZURE_CLIENT_ID]$env:servicePrincipalId"
        Write-Host "##vso[task.setvariable issecret=true;variable=AZURE_CLIENT_SECRET]$env:servicePrincipalKey"
        Write-Host "##vso[task.setvariable issecret=true;variable=AZURE_TENANT_ID]$env:tenantId"
        if (-not $env:AZURE_SUBSCRIPTION_ID) {
          $subscriptionCount = az account list --query "length([])" --output tsv
          if ($subscriptionCount -eq 1) {
              $subscriptionId = az account list --query "[0].id" --output tsv
              Write-Host "Setting AZURE_SUBSCRIPTION_ID environment variable to: $subscriptionId"
              Write-Host "##vso[task.setvariable issecret=true;variable=AZURE_SUBSCRIPTION_ID]$($subscriptionId)"
          } 
          elseif ($subscriptionCount -gt 1) {
              Write-Host "Multiple subscriptions are accessible. Please set the AZURE_SUBSCRIPTION_ID environment variable manually."
              exit 1
          }
        }
        else {
          Write-Host "AZURE_SUBSCRIPTION_ID is already set to: $env:AZURE_SUBSCRIPTION_ID"
        }
        

        addSpnToEnvironment: true failOnStandardError: true

      只保留 SH01_API_NAMES 指定的 API 文件夹

      • task: PowerShell@2 displayName: Filter only selected APIs inputs: targetType: "inline" script: | $ErrorActionPreference = "Stop" $srcRoot = "$(Build.SourcesDirectory)/${{ parameters.API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH }}/apis" $dstRoot = "$(Build.ArtifactStagingDirectory)/filtered-apis" New-Item -ItemType Directory -Force -Path $dstRoot | Out-Null $apis = @() foreach ($api in "${{ join(',', parameters.SH01_API_NAMES) }}".Split(',')) { $src = Join-Path $srcRoot $api if (Test-Path $src) { Copy-Item $src $dstRoot -Recurse $apis += $api } } Write-Host "Filtered APIs: $($apis -join ',')" # 覆盖原 artifacts/apis 目录 Remove-Item $srcRoot -Recurse -Force Move-Item $dstRoot $srcRoot

      replacetokens@3 task will need to be installed to use

      • ${{ if ne(parameters.CONFIGURATION_YAML_PATH, '') }}:

        • task: qetza.replacetokens.replacetokens-task.replacetokens@3 displayName: "Perform namevalue secret substitution in $(Build.SourcesDirectory)/configuration.uat.yaml" inputs: targetFiles: $(Build.SourcesDirectory)/configuration.uat.yaml encoding: "auto" writeBOM: true verbosity: "off" actionOnMissing: "warn" keepToken: false tokenPrefix: "{#" tokenSuffix: "#}"
      • task: PowerShell@2 displayName: Fetch publisher inputs: targetType: "inline" script: | Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $VerbosePreference = "Continue" $InformationPreference = "Continue"

        Write-Information "Downloading publisher..."
        $publisherFileName = "publisher.linux-x64"
        $publisherFinalFileName = "publisher"
        if ("$(Agent.OS)" -like "*win*") {
          $publisherFileName = "publisher.win-x64.exe"
          $publisherFinalFileName = "publisher.exe"
        }
        elseif ("$(Agent.OS)" -like "*mac*" -and "$(Agent.OSArchitecture)" -like "*arm*") {
          $publisherFileName = "publisher.osx-arm64"
        }
        elseif ("$(Agent.OS)" -like "*mac*" -and "$(Agent.OSArchitecture)" -like "*x86_64*") {
          $publisherFileName = "publisher.osx-x64"
        }
        
        $uri = "https://github.com/Azure/apiops/releases/download/$(apiops_release_version)/$publisherFileName"
        $destinationFilePath = Join-Path "$(Agent.TempDirectory)" $publisherFinalFileName
        Invoke-WebRequest -Uri "$uri" -OutFile "$destinationFilePath"
        
        if ("$(Agent.OS)" -like "*linux*")
        {
          Write-Information "Setting file permissions..."
          & chmod +x "$destinationFilePath"
          if ($LASTEXITCODE -ne 0) { throw "Setting file permissions failed."}
        }
        
        Write-Host "##vso[task.setvariable variable=PUBLISHER_FILE_PATH]$destinationFilePath"
        Write-Information "Execution complete."
        

        failOnStderr: true pwsh: true

      • task: PowerShell@2 displayName: Run publisher for Uat environment inputs: targetType: "inline" script: | Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $VerbosePreference = "Continue" $InformationPreference = "Continue"

        & "$(PUBLISHER_FILE_PATH)"                
        if ($LASTEXITCODE -ne 0) { throw "Running publisher failed."}
        
        Write-Information "Execution complete."
        

        failOnStderr: true pwsh: true env: AZURE_BEARER_TOKEN: $(AZURE_BEARER_TOKEN) AZURE_CLIENT_ID: $(AZURE_CLIENT_ID) AZURE_CLIENT_SECRET: $(AZURE_CLIENT_SECRET) AZURE_TENANT_ID: $(AZURE_TENANT_ID) AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID) AZURE_RESOURCE_GROUP_NAME: $(RESOURCE_GROUP_NAME_UAT) API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH: $(Build.SourcesDirectory)/${{ parameters.API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH }} API_MANAGEMENT_SERVICE_NAME: $(APIM_NAME_UAT) ${{ if eq( parameters['COMMIT_ID'], 'publish-artifacts-in-last-commit' ) }}: COMMIT_ID: $(Build.SourceVersion) CONFIGURATION_YAML_PATH: $(Build.SourcesDirectory)/configuration.uat.yaml

How to change the settings to only publish certain API? Is it supported for this tool or latest version?

Expected behavior

The effect I want to achieve is to only publish the code of the corresponding API management project that has been modified in the git repo, while the code submitted through the git repo for other projects should not be published to the API management instance. In brief, I want to achieve the release of specified API projects through a single pipeline script, rather than publishing all APIs.

Actual behavior

My pipelines script has deployed all the code submitted through the git repo to the instance of API management

Reproduction Steps

  1. Establish the binding between the API management instance and the Azure Git repo
  2. Modify the corresponding code in sh01-kabsa, sh02-resource, and api-test 3.run piplines

setyg avatar Aug 04 '25 01:08 setyg

  Thank you for opening this issue! Please be patient while we will look into it and get back to you as this is an open source project. In the meantime make sure you take a look at the [closed issues](https://github.com/Azure/apiops/issues?q=is%3Aissue+is%3Aclosed) in case your question has already been answered. Don't forget to provide any additional information if needed (e.g. scrubbed logs, detailed feature requests,etc.).
  Whenever it's feasible, please don't hesitate to send a Pull Request (PR) our way. We'd greatly appreciate it, and we'll gladly assess and incorporate your changes.

github-actions[bot] avatar Aug 04 '25 01:08 github-actions[bot]

You would configure this in your "configuration.extractor.yaml" file. Specify what api(s) you want to extract into your branch then run the publisher against that branch. We create a main branch for each api to keep things separate. It feels a little weird since it doesnt align with a traditional git strategy but infrastructure as code is just different. Create a branch called something like api/main-API-NAME. In that branch configure your configuration.extractor.yaml. Push that branch to your repo and when you run the the "run-extractor.yaml" pipeline select the branch you just created "api/main-API-NAME". This will extract only the apis you specified in configuration.extractor.yaml which will appear in your artifacts folder after you complete the pull request. Then you can run the publisher off this branch as well and it will only publish what is in your repo.

megamax34 avatar Oct 09 '25 17:10 megamax34