azure-devopsyamlnuget-packagejfrog-clijfrog-pipelines

push nuget packages from JFrog artifactory to Azure DevOps Artifactory


I use this azure pipeline and it work fine for me:

trigger: none

schedules:
  - cron: "0/10 * * * *"
    displayName: Nuget JFrog to Azure DevOps Artifacts
    branches:
      include:
      - main
    always: true

jobs:
  - job: SyncArtifactory
    pool:
      name: 'Ubuntu-latest'

    steps:

          - task: JfrogCliV2@1
            inputs:
              jfrogPlatformConnection: 'JFrog Artifactory'
              command: 'jf rt dl randomPath/Random.Nuget.Package.1.0.1.nupkg'

          - task: NuGetCommand@2
            inputs:
              command: 'push'
              packagesToPush: '$(System.DefaultWorkingDirectory)/Random.Nuget.Package.1.0.1.nupkg'
              nuGetFeedType: 'internal'
              publishVstsFeed: '<Azure_Devops_Feed>'

Now i want modify this pipeline for deploy nugets dynamically. I mean something like this:

trigger: none

schedules:
  - cron: "0/10 * * * *"
    displayName: Nuget JFrog to Azure DevOps Artifacts
    branches:
      include:
      - main
    always: true

parameters:
  - name: packages
    type: object
    default:
      - name: "Random.Nuget.Package.1"
        version: "latest"
      - name: "Random.Nuget.Package.2"
        version: "latest"
      - name: "Random.Nuget.Package.3"
        version: "latest"


jobs:
  - job: SyncArtifactory
    pool:
      name: 'Ubuntu-latest'


    steps:
      - ${{ each package in parameters.packages }}:
        - task: JfrogCliV2@1
          inputs:
            jfrogPlatformConnection: 'JFrog Artifactory'
            command: 'jf rt s "randomPath/Random.Nuget.Package/*" --limit 1 --sort-by created --sort-order desc --format json'
          displayName: 'Search for Latest Package ${{ package.name }}'
          name: ${{ format('SearchPackage_{0}', replace(package.name, '.', '_')) }}

        - script: |
            latest_version=$(jq -r '.[].name' $(System.DefaultWorkingDirectory)/${{ format('SearchPackage_{0}', replace(package.name, '.', '_')) }}.json)
            echo "##vso[task.setvariable variable=LATEST_VERSION]$latest_version"
          displayName: 'Set Latest Version Variable'

        - task: JfrogCliV2@1
          inputs:
            jfrogPlatformConnection: 'JFrog Artifactory'
            command: |
              jf rt dl randomPath/${{ package.name }}/${{ variables.LATEST_VERSION }}.nupkg
          displayName: 'Download Latest Package ${{ package.name }}'

        - task: NuGetCommand@2
          inputs:
            command: 'push'
            packagesToPush: '$(System.DefaultWorkingDirectory)/${{ package.name }}.${{ variables.LATEST_VERSION }}.nupkg'
            nuGetFeedType: 'internal'
            publishVstsFeed: '<Azure_Devops_Feed>'
          displayName: 'Push Package to Azure Artifacts'

If i use my modified script i have this error:

`Found tool in cache: jf 2.56.1 x64 Running jfrog-cli from /opt/azdevops/agent-02/_work/_tool/jf/2.56.1/x64/jf JFrog CLI version: 2.56.1 Executing JFrog CLI Command: /opt/azdevops/agent-02/_work/_tool/jf/2.56.1/x64/jf c add "test-nuget_20240523.26_jfrog_cli_cmd_1716472614658" --url="https://<my_artifactory>/" --interactive=false --user='**' --basic-auth-only --password-stdin Executing JFrog CLI Command: /opt/azdevops/agent-02/_work/_tool/jf/2.56.1/x64/jf c use "test-nuget_20240523.26_jfrog_cli_cmd_1716472614658" 13:57:06 [Info] Using server ID 'test-nuget_20240523.26_jfrog_cli_cmd_1716472614658' (https://<my_artifactory>/) Executing JFrog CLI Command: /opt/azdevops/agent-02/_work/_tool/jf/2.56.1/x64/jf rt s "randomPath/Random.Nuget.Package/" --limit 1 --sort-by created --sort-order desc --format json 13:57:06 [Error] Wrong number of arguments (3). You can read the documentation at https://jfrog.com/help/r/jfrog-cli

Name: jf rt search - Search files in Artifactory.

--server-id [Optional] Server ID configured using the config command. --sort-by [Optional] List of semicolon-separated(;) fields to sort by. The fields must be part of the 'items' AQL domain. For more information, see https://jfrog.com/help/r/jfrog-artifactory-documentation/artifactory-query-language --sort-order [Default: asc] The order by which fields in the 'sort-by' option should be sorted. Accepts 'asc' or 'desc'. --spec [Optional] Path to a File Spec. --spec-vars [Optional] List of semicolon-separated(;) variables in the form of "key1=value1;key2=value2;..." (wrapped by quotes) to be replaced in the File Spec. In the File Spec, the variables should be used as follows: ${key1}. --ssh-key-path [Optional] SSH key file path. --ssh-passphrase [Optional] SSH key passphrase. --transitive [Default: false] Set to true to look for artifacts also in remote repositories. The search will run on the first five remote repositories within the virtual repository. Available on Artifactory version 7.17.0 or higher. --url [Optional] JFrog Artifactory URL. --user [Optional] JFrog username.

Environment Variables: JFROG_CLI_FAIL_NO_OP [Default: false] Set to true if you'd like the command to return exit code 2 in case of no files are affected. Support by the following commands: copy, delete, delete-props, set-props, download, move, search and upload

##[error]Error: Command failed: /opt/azdevops/agent-02/_work/_tool/jf/2.56.1/x64/jf rt s "randomPath/Random.Nuget.Package/*" --limit 1 --sort-by created --sort-order desc --format json Executing JFrog CLI Command: /opt/azdevops/agent-02/_work/_tool/jf/2.56.1/x64/jf c remove "test-nuget_20240523.26_jfrog_cli_cmd_1716472614658" --quiet `

So i want fisrt step check i have latest version in JFrog or not, if i have i will start download latest version .nupkg and push it to Azure Devops Artifacts.

Can you help me resolve it please?


Solution

  • As far as I tested, running the same JFrog CLI command jf rt s "randomPath/Random.Nuget.Package/*" --limit 1 --sort-by created --sort-order desc --format json failed with the same error as that in the pipeline, because --fortmat json was not a valid argument.

    Besides, the 3rd-party extention task JfrogCliV2@1 doesn't support command other than jf.

    Per your expectation to use JFrog CLI to search for the latest version of a package in JFrog Artifactory, download that NuGet package and push to Azure Artifacts feed, I am afraid you are unable to do so with that extension task.

    However, having took a further look at the JFrog CLI workflow, you may use a script to do so. Here is a sample pipeline for your reference.

    trigger: none
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - script: |
        echo "================ 1. Install JFrog CLI on Ubuntu Microsoft-hosted agent ================"
        curl -fL https://install-cli.jfrog.io | sh
        jf -version
        
        echo "================ 2. Establish access to JFrog with authentication against APIKey ================"
        jf config add --password "$(APIKey)" --url "https://$(JFrogHost).jfrog.io/" --interactive=false
        jf config show
        
        echo "================ 3. Search for the latest version of JFrog package and download ================"
        latestPackage=$(jf rt s "jfrog-nuget/Random.Nuget.Package/*" --limit 1 --sort-by created --sort-order desc)
        echo -e Package info:\n"$latestPackage"
        packagePath=$(echo "$latestPackage" | jq -r '.[0].path')
        echo Path is "$packagePath"
        jf rt dl $packagePath
        
        echo "================ 4. Check the files in System.DefaultWorkingDirectory ================"
        tree $(System.DefaultWorkingDirectory)
    - task: NuGetCommand@2
      inputs:
        command: 'push'
        packagesToPush: '$(System.DefaultWorkingDirectory)/Random.Nuget.Package/*.nupkg'
        nuGetFeedType: 'internal'
        publishVstsFeed: '$(System.TeamProject)/AzureArtifactFeed-FromJFrog'
        allowPackageConflicts: true
    
    

    enter image description here

    enter image description here