How can I get Azure pipeline log file from command line - command-line

I have an Azure pipeline. I can start and see the logs in Chrome. But I would like to do these steps from command line (actually Cygwin, but IMHO this is not relevant).
az pipelines run --name $pipeline --branch $branch
This command gives back a json file format text on the stdout. This json has a entry, called logs:
{
...
"logs": {
"id": 0,
"type": "Container",
"url": "https://dev.azure.com/myazure/a56234f9-0101-5183-b422-db6f8cb55857/_apis/mybuild/mybuilds/1822266/logs"
},
...
}
If I copy the "url" into Chrome I get another json format homepage, like:
{"count":27,"value":[{"lineCount":371,"createdOn":"2022-10-17T13:38:14.013Z","lastChangedOn":"2022-10-17T13:38:14.183Z","id":1,"type":"Container","url":"https://dev.azure.com/myazure/...
But I cannot get back this json data from command line. I tried to get by curl or wget. I got back a HTML page (full with with JavaScript), not the json answer.
I also tried az:
az rest --method get --url "$logs_url"
But the response is:
Can't derive appropriate Azure AD resource from --url to acquire an access token. If access token is required, use --resource to specify the resource
Not a json response, outputting to stdout. For binary data suggest use "--output-file" to write to a file
Then I tried to do:
az account get-access-token --query accessToken --output tsv > access_token.tsv
az rest --method get --resource access_token.tsv --url "$logs_url"
So, I assume I should get an access token to the URL. But how can I acquire it?

You can use curl to call rest api: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/logs/list?view=azure-devops-rest-6.0
E.g.
curl https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}/logs?api-version=6.0-preview.1 ' -H 'Authorization: Basic YourPAT'

Related

Getting "not found" after authenticating when trying to initiate GitHub workflow via REST

I am trying to trigger the workflow_dispatch action for a GitHub workflow via REST but I am getting a "not found" error.
My question is similar to this one but the difference is that I am still getting the "not found" error even though the header indicates I am authenticated (the rate limit has increased to 5,000).
Here's my script:
#!/bin/bash
# https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
OWNER='myGithubOrganization'
REPO='myRepo'
WORKFLOW_ID='main.yml'
POST_URL="https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/dispatches"
echo "Calling $POST_URL"
GITHUB_PERSONAL_ACCESS_TOKEN=$(echo "$PLATFORM_VARIABLES" | base64 --decode | jq '.GITHUB_PERSONAL_ACCESS_TOKEN' --raw-output)
# -i to include headers.
curl \
-i \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_PERSONAL_ACCESS_TOKEN" \
$POST_URL \
-d '{"ref":"ref"}'
In the headers, I see the rate limit has increased to 5,000, so I know I am logged in.
The personal access token has the following permissions:
repo
workflow
admin:org_hook
The personal access token is for a machine user.
In the repo settings, under "Collaborators and teams", the machine user account has the "Read" role.
What more do I need to do to trigger the workflow?
The machine user needs to have write access, not read access.
This is true even if the workflow does something like run CI tests and does not write any code.

How can use cURL with Powershell to download from sharepoint using Microsoft Graph

I am trying to download some files from my SharePoint or OneDrive using the Microsoft graph API. I want to only use cURL (The C:\windows\systems32\curl not invoke-request) and powershell.
I tried using the documentation from Microsoft here:https://learn.microsoft.com/en-us/graph/auth-v2-service#4-get-an-access-token
and here: https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes
However the syntax seems to be off and I have been having some trouble.
Here is my starting code (This assumes you have setup an appid in the azure portal and obtained and client_id,client_secret, and tenant id):
curl -d 'client_id'='ENTERCLIENTIDHERE' \
-d 'scope'='https://graph.microsoft.com/.default' \
-d 'client_secret'='ENTERCLIENTSECRETHERE' \
-d 'grant_type'='client_credentials' \
'https://login.microsoftonline.com/ENTERTANTANTHERE/oauth2/v2.0/token'
Here is code that will help you get a request from the Microsoft graph api using a token from your app id information, then it will generate a url for a specific file or folder of your choosing. You can also modify the code to just get yourself a token from the Microsoft graph API using only cURL and Powershell for windows.
#Generating the Token from the microsoft Graph API
$Result = C:\Windows\System32\curl -n -i -H "Host:login.microsoftonline.com" -H "Content-Type:application/x-www-form-urlencoded" -d "client_id=ENTERCLIENTIDHERE&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=ENTERCLIENTSECRETHERE&grant_type=client_credentials" "https://login.microsoftonline.com/ENTERTENANTIDHERE/oauth2/v2.0/token"
$x = $Result[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
$M = $x[16]
$k = $M -split ‘access_token":"’
$Resultk = $k[1]
$ResulO = $Resultk.Substring(0,$Resultk.Length-2)
#Seeing the token in powershell to verify it came up properly
Write-Output $ResulO
#Requesting a download url from your own sharepoint or onedrive
C:\Windows\System32\curl -H "Host:graph.microsoft.com" -H "Authorization:Bearer $ResulO" -H "X-Cookie:token=$ResulO" "https://graph.microsoft.com/v1.0/sites/ENTERSITENAMEORSITEIDHERE/drive/root:/ENTERFILEPATHORFILEHERE?select=id,#microsoft.graph.downloadUrl"

IBM Speech to Text service call return error

I have created IBM Speech to Text service. Then I have performed the trial call using the following IBM instructions:
Try an API call
Call the POST /v1/recognize method to request a basic transcription of
a FLAC audio file with no additional request parameters.
First, download the sample audio file audio-file.flac.
Then, issue the following command to call the service's /v1/recognize
method for basic transcription with no parameters. The example uses
the Content-Type header to indicate the type of the audio, audio/flac.
The example uses the default language model, en-US_BroadbandModel, for
transcription. Be sure to modify {path_to_file} to specify the
location of the audio-file.flac file.
curl -X POST \
-u "apikey:{apikey}" \
--header "Content-Type: audio/flac" \
--data-binary #{path_to_file}audio-file.flac \
"{url}/v1/recognize"
The service returns the following transcription results:
{
"results": [
{
"alternatives": [
{
"confidence": 0.8691191673278809,
"transcript": "several tornadoes touch down as a line of severe thunderstorms swept through colorado on sunday"
}
],
"final": true
}
],
"result_index": 0
}
I have tried execute this substituting my STT service credentials, but I received error message instead of transcript result:
<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>
An error occurred while processing your request.<p>
Reference #179.24f01502.1545478256.101ee90d
</BODY></HTML>
I have tried different {path_to_file} variants - using slashes /, backslashes \ and double backslashes \ \ but all of them returned the same error. What's wrong with my request or my STT service?
Check the location of your service - this error occures when you mix up the service urls. For example creating the service in Frankfurt
with this API URL https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize and calling the service using the London location gateway-lon
curl -X POST \
-u "apikey:APIKEY" \
--header "Content-Type: audio/flac" \
--data-binary #audio-file.flac \
"https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/recognize"
the error occures. It should probably return something like {"code":401, "error": "Unauthorized"}.

Upload secret file credentials to Jenkins with REST / CLI

How can I create a Jenkins Credential via REST API or Jenkins CLI? The credential should be of type "secret file", instead of a username / password combination.
The question is similar to this question, but not the same or a duplicate.
You can do it as follows:
curl -X POST \
https://jenkins.local/job/TEAM-FOLDER/credentials/store/folder/domain/_/createCredentials \
-F secret=#/Users/maksym/secret \
-F 'json={"": "4", "credentials": {"file": "secret", "id": "test",
"description": "HELLO-curl", "stapler-class":
"org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl",
"$class":
"org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl"}}'
just finished with it today https://www.linkedin.com/pulse/upload-jenkins-secret-file-credential-via-api-maksym-lushpenko/?trackingId=RDcgSk0KyvW5RxrBD2t1RA%3D%3D
To create Jenkins credentials via the CLI you can use the create-credentials-by-xml command:
java -jar jenkins-cli.jar -s <JENKINS_URL> create-credentials-by-xml system::system::jenkins _ < credential-name.xml
The best way to know the syntax of this is to create a credential manually, and then dump it:
java -jar jenkins-cli.jar -s <JENKINS_URL> get-credentials-as-xml system::system::jenkins _ credential-name > credential-name.xml
Then you can use this XML example as a template, it should be self-explanatory.
If you want to update an existing secret file, the simplest way I found was to delete and re-create.
A delete request, to extend #lumaks answer (i.e. with the same hostname, folder name and credentials id), looks like:
curl -v -X POST \
-u "user:password" \
https://jenkins.local/job/TEAM-FOLDER/credentials/store/folder/domain/_/credential/test/doDelete
This will return either HTTP status code 302 Found or 404 Not Found for existing and non-existing creds file respectively.

File upload with post parameter in curl

How to make a request for uploading a file to facebook using graph api in curl
I am making this request but getting Error:
curl https://graph.facebook.com/<id>/photos -F "source=#me.jpg" -d "message=Me" -v
ERR:
Only One Http Request can be Selected
You cannot mix -F and -d options in the same command line. If you want a multipart formpost (which I believe you do) then you need to add all parts with -F, including the "message=Me" part.