0
votes

I am trying to do some processing inside a C# console application and that file has to be saved:

File.WriteAllText("C:\JSONOutput\output.md"), htmlResult);

I want save it to Azure DevOps folder like '/home/vsts/work/1/s/' so that it can be accessed again in the next step where I am uploading the file to artifacts using a statement like below:

Write-Host "##vso[artifact.upload containerfolder=testresult;artifactname=worksoftHTML;]/home/vsts/work/1/s/output.html"

So far I have tried by writing the following but it is not working:

File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"/home/vsts/work/1/s/output.md"), htmlResult);

and this also:

File.WriteAllText("../home/vsts/work/1/s/output.md", htmlResult);

It is also not working.Please help. I have tried the following as well in the pipeline where I am passing the filepath as a parameter to the program in command line:

- task: CmdLine@2 
  env:     
      InputJSON: $(testJSON)  
  inputs:  
    script: 
        echo $(testJSON)               
        'jsonProcessor.exe $(Build.ArtifactStagingDirectory)\dropfile\,%INPUTJSON%'     
    workingDirectory: './jsonProcExe/'

Inside the exe I am writing the file like below:

File.WriteAllText(Path.Combine(filePath, @"output.html"), finalHtml);

And then in the next step I am reading the files like this:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.      
      Write-Host "##vso[artifact.upload containerfolder=testresult;artifactname=worksoftHTML;]$(Build.ArtifactStagingDirectory)\dropfile\output.html"

But still I am getting the error

Path does not exist: C:\agent\_work\1\a\dropfile\output.html

Please note that I am using a self hosted agent.

1

1 Answers

1
votes

The following line in my pipeline was not correct:

jsonProcessor.exe $(Build.ArtifactStagingDirectory)\dropfile\,%INPUTJSON%

Because this is not how you should pass parameters to a console application from command line. Also I was trying to pass JSON as an argument in the above line. That will not work. So following is what I have done: I am saving my JSON from powershell script in Build.StagingArtifact folder:

Write-Output $json | Out-File $env:BUILD_ARTIFACTSTAGINGDIRECTORY/results.json

Then in the command line task I am passing the above folder location in the following manner:

task: CmdLine@2     
  inputs:  
    script:                                
        'jsonProcessor.exe $(Build.ArtifactStagingDirectory)\results.json $(Build.ArtifactStagingDirectory)'    
    workingDirectory: './jsonProcExe/'

Also inside the c# code of the .exe file it is better to modify the filepath passed from the command line by replacing backslash with frontslash. Then I can just write the file from C# exe using the following line of code:

//Create the HTML file.
File.WriteAllText(filePath + "/output.html", finalHtml);