0

How do you handle cases when the Extract files task does not find files to extract ?

Is it possible to set the Extract files task to fail if there were no files extracted ? Here is a sample task

steps:
- task: ExtractFiles@1
  displayName: 'Extract files '
  inputs:
    archiveFilePatterns: '$(System.ArtifactsDirectory)\*.zip'
    destinationFolder: '$(System.ArtifactsDirectory)\bin'

it does not fail if no file was found however

2020-10-01T14:25:23.1175947Z Searching for: *.zip under directory: E:\ba\n1_work\r16\a
2020-10-01T14:25:23.1287445Z Found: 0 files to extract:

and then a ftp upload task does nothing

2020-10-01T14:25:36.4142531Z ##[warning]Could not find any files to upload

The release pipeline is simple like

  • extract files
  • stop azure app service
  • upload files by ftp
  • start azure app service

I've added the powershell script to check for files were extracted

if (-not (Test-path $(System.ArtifactsDirectory)\bin\*) )
{
Throw New-Object System.ArgumentException("no files were extracted")
}

but would like to know is it possible to make ExtractFiles@1 or FtpUpload@2 tasks to fail if there was nothing extracted or uploaded ?

Regards

oleksa
  • 3,688
  • 1
  • 29
  • 54
  • Hi @oleksa, Just checking in to see whether this issue is still blocking you now? Any update for this issue? – Vito Liu Oct 07 '20 at 09:18
  • @VitoLiu-MSFT I'm trying to understand why the QueryAzureDevOpsExtensionVersion task fails to connect to the VS marketplace. The token I've generated has Read Acquire and Publish Marketplace scopes enabled but the error is `401 unauthorized` – oleksa Oct 07 '20 at 09:28
  • 1
    Hi @oleksa, You need choose all accessible organizations option to create PAT. Please try it and then kindly share the result here. – Vito Liu Oct 07 '20 at 09:54

3 Answers3

1

There is no easy way to do this as the task doesn't support such case. (So please consider adding feature request on github). Howeer you can use REST API call to get logs analyze them and throw exception when there is no files. It could look like this:


variables:
  devopsAccount : 'thecodemanual'
  projectName : 'DevOps Manual'
  logId: "6"


steps:
- task: ExtractFiles@1
  displayName: 'Extract files '
  inputs:
    archiveFilePatterns: '$(System.ArtifactsDirectory)\*.zip'
    destinationFolder: '$(System.ArtifactsDirectory)\bin'

- task: PowerShell@2
  name: testDetails
  condition: always()
  inputs:
    targetType: 'inline'
    script: |
        # Encode the Personal Access Token (PAT)
        $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(System.AccessToken)")) }

        # Get a list of releases
        $uri = "https://dev.azure.com/$(devopsAccount)/$(projectName)/_apis/build/builds/$(Build.BuildId)/logs/$(logId)?api-version=5.1"

        Write-Host $uri

        # Invoke the REST call
        $result = Invoke-RestMethod -Uri $uri -Method Get -Headers $AzureDevOpsAuthenicationHeader

        Write-Host $result

        $lines = $result.Split([Environment]::NewLine)

        $passed = 0;
        $failed = 0;

        foreach($line in $lines) {
            if ($line -match "Found: 0 files to extract") { 
              throw 'There is no files to extract'

            }
        }

Log id in my case is 6 because Azure DevOps adds 5 steps before running first task from the list.

enter image description here

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • thanks but log analyzer scares me even more :) I've created [the feature request](https://github.com/microsoft/azure-pipelines-tasks/issues/13648) – oleksa Oct 02 '20 at 15:29
  • I fully agree :) I showed that this is possible :) (even if it is not nice :)) Can you consider accepting my reply as answer and upvoting it if it were helpful for yoy? – Krzysztof Madej Oct 02 '20 at 15:31
1

The task ExtractFiles@1 or FtpUpload@2 code is open source on GitHub, we can download it and update the .ts file then Publish it to Visual Studio Marketplace.

For example, make the task FtpUpload@2 to fail if there was nothing uploaded.

2020-10-01T14:25:36.4142531Z ##[warning]Could not find any files to upload

This warning message is defined NoFilesFound in the task.json file and then called in the file ftpuploadtask.ts via this code tl.warning(tl.loc("NoFilesFound"));.

We can change the code to tl.setResult(tl.TaskResult.Failed, tl.loc("NoFilesFound")); Then we can see the error message in the task instead of warning message, and the task result will show failed.

We can do the same things for the task ExtractFiles@1

Vito Liu
  • 7,525
  • 1
  • 8
  • 17
  • PackageAzureDevOpsExtension raises error `No manifests found from the following glob patterns: vss-extension.json` It looks weird since there is no `vss-extension.jso`n file in the original MS sources and I have defined `task.json` already – oleksa Oct 07 '20 at 11:34
  • I've created minimal `vss-extension.json` like [here](https://learn.microsoft.com/en-us/azure/devops/extend/develop/manifest?view=azure-devops#examples-of-required-attributes) (but with my own values) and now PackageAzureDevOpsExtension error is `'/opt/hostedtoolcache/tfx/0.7.11/x64/node_modules/.bin/tfx' failed with exit code 255`. No additional info can be found unfortunately – oleksa Oct 07 '20 at 12:06
  • Hi @oleksa, Do you mind sharing your steps and error message here? Upload the extension, we need you owner publish ID and change the version ID. – Vito Liu Oct 08 '20 at 10:16
  • I've managed to build, [upload the extension](https://marketplace.visualstudio.com/items?itemName=Quipu-CWnet-team.989A60FA-2F3B-49CB-8695-97C2E4521487) and install it. However the problem is that the task is not visible in the `Add task` mode. I've tried to put `ftp`, `customftpupload` and `ftpuploadm` in the filter string. – oleksa Oct 08 '20 at 15:32
  • Hi @oleksa, I will custom this extension and share the full steps here, please wait. Thanks. – Vito Liu Oct 12 '20 at 08:52
  • Hi @oleksa, Sorry to reply you so late. Since the initial issue has been resolved, to better archive this ticket, do you mind creating a new ticket? Please also share the latest error message and steps in the new ticket. And we will help you solve the latest issue as soon as possible. By the way, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks. – Vito Liu Oct 15 '20 at 09:22
  • I've found it a little but tricky way: to copy the existing task, create own extension manifest and try to put it to the Marketplace unfortunately.Will continue to use my three-line powershell script to check are there files to upload. It is much more clear and has been created in 30 minutes. I've spend about one day to create extension from the existing code. And the extension Task is not visible :-[ as the result – oleksa Oct 15 '20 at 14:19
  • Hi @oleksa, Do you mind share the power shell code here and accept it? So it could help other community members who get the same issues and we could archive this thread, thanks – Vito Liu Oct 16 '20 at 02:05
  • please read the original question. There is powershell code sample (you can find it by `Test-path` command) – oleksa Oct 16 '20 at 08:10
  • Sorry... Thanks for your sharing, if these answer could give you some help, would you mind accepting one? Then we could archive this thread, thanks. – Vito Liu Oct 16 '20 at 08:46
1

There are other tasks that are available in the marketplace you can use to unzip the package. You can try using Unzip Task or Zip and unzip directory build task in stead of using Extract files task.

I tested with Unzip Task and Zip and unzip directory build task. They both work greatly and failed as expected when no file is found to unzip. See below

enter image description here

And for FTP upload, you can check out FTP Uploader task.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43