Retrieve custom variable from a successful jenkins job run via REST api call - rest

I would like to retrieve a custom variable from a successful last stable build jenkins job. I was able to retrieve the build number using curl on this link using Execute Shell. It's an internal server. https://jenkinsci.internalsvr/view/webapps/job/common-tools/lastStableBuild/buildNumber
Now, I'd like to do the same for a custom variable using curl. But I know that I may need to save the value of the custom variable but not sure how and to where.

So this is how I got it to work
I have this code in our dsl.groovy file
....
parameters {
stringParam('CUSTOM_VAR1', '', 'Custom Variable')
stringParam('CUSTOM_VAR2', '', 'Custom Variable')
}
shellCommands = sprintf('''#/bin/bash
echo "CUSTOM_VAR1=\${%s}" > env.properties
echo "CUSTOM_VAR2=\${%s}" >> env.properties
''', ['CUSTOM_VARIABLE1','CUSTOM_VARIABLE1'])
shell(shellCommands)
// This is extremely important
environmentVariables {
propertiesFile('env.properties')
}
// This allowed me to retrieve env.properties via http call from browser or curl.
publishers {
archiveArtifacts {
pattern('env.properties')
}
}
So if I need to access it, the http url should be formed like this
curl https://our-internal-server/job/theNameOfTheJob/lastStableBuild/artifact/env.properties

You can do it with the API of the EnvInject plugin, either with:
curl <jenkins-host>/job/<job_name>/<buildNumber>/injectedEnvVars/export
curl <jenkins-host>/job/<job_name>/<buildNumber>/injectedEnvVars/api/python
More info here.

Related

How do I get and download the contents of a file in GitHub using the REST API?

I am trying to use the GitHub API to retrieve and download the contents of a file in my GitHub repository to mimic how I am using the Azure DevOps REST API.
My ADO URL is:
https://dev.azure.com/<Org>/<Project>/_apis/git/repositories/<Repository>/items?versionType=branch&version=develop&path=<Path to file>/DEV1.yml&download=true
Above works perfectly fine and if I type that into my browser, it immediately downloads the DEV1.yml file.
I am trying to mimic this implementation using GitHub's REST API. My URL looks like:
https://api.github.com/repos/<Org>/<Repository>/contents/<Path to file>/DEV1.yml&download=true
This GitHub URL does not work in the browser, I assume it's because the repo is private and I need a token. However when I use curl:
curl -H "Authorization: Bearer <PAT>" https://api.github.com/repos/<Org>/<Repository>/contents/<Path to file>/DEV1.yml&download=true
This returns a large base64 encoded JSON object. I attempted to use the URL in place of the existing ADO one and it failed.
Are the 2 above URLs not the same?
Yes, it is different.
Github REST API download files has two steps.
1, The first step is get the download url.
The url format like this:
https://api.github.com/repos/<Project Name>/<Repository Name>/contents/<File Name>
The response format like this:
2, The second step is using the download url to get the file content.
The url format like this:
https://raw.githubusercontent.com/<Project Name>/<Repository Name>/main/<File Name>?token=<Random Token that related to Revision Version>
Please notice that the first step can't skip, otherwise you will be unable to get the revision token.
I can achieve your requirement using python:
import requests
#Define required information
project_name = "xxx"
repository_name = "xxx"
# repository_name = "xxx"
branch_name = "xxx"
File_name = "xxx"
PAT = "xxx"
url = "https://api.github.com/repos/"+project_name+"/"+repository_name+"/contents/"+File_name
#downoad file from github
payload = {}
headers = {
'Authorization': 'token '+PAT
}
#download file
file_content = requests.request("GET", ((requests.request("GET", url, headers=headers, data=payload)).json())['download_url'], headers=headers, data=payload)
print(file_content.text)
Successfully get the latest contents(I am also based on private repository):

Can I skip generating the {proxy+} when creating Rest API using Amplify

I am currently playing around with creating rest-api:s with amplify. I am trying to create the following structure:
/helloText
/helloText/{name}
But I get the following error when trying to push the backend:
Unable to create resource at path '/helloText/{proxy+}': A sibling ({name}) of this resource already has a variable path part -- only one is allowed
As I understand, it is because Amplify is creating "greedy paths" by default.
When manually removing these "greedy paths" from the template file RestTest-cloudformation-template.json it works.
Is it somehow possible to create these paths by using only the command line?
There is no configuration switch for that in amplify-cli. You need to create an override. Reference: Amplify override api.
Run:
amplify override api
Then edit generated override.ts like:
import { AmplifyApiRestResourceStackTemplate } from '#aws-amplify/cli-extensibility-helper';
export function override(resources: AmplifyApiRestResourceStackTemplate) {
const {paths} = resources.restApi.body;
Object.keys(paths).forEach((path) => {
if (path.includes('{proxy+}')) {
delete paths[path];
}
});
}

Jenkins http plugin to upload file using rest

I am trying to upload a file to a rest server from jenkins using http plugin. I have a jenkins pipelie where a step involves loading a file(type formData)
to a server using rest.
the server side method uses two parameters:
(#FormDataParam("file") InputStream file, #FormDataParam("fileName") String fileName)
I am using the below method
def filename = "${WORKSPACE}/Test.txt"
data="""{ \"fileName\" : \"Test.txt\" }"""
resp3 = httpRequest consoleLogResponseBody: true,url: "http://<url>",contentType:'APPLICATION_OCTETSTREAM',customHeaders:[[name:'Authorization', value:"Basic ${auth}"]],httpMode: 'POST',multipartName: 'Test.txt',uploadFile: "${filename}",requestBody:data,validResponseCodes: '200'
but when I run the status code is 400 and in the server logs the message is that no filestream and no filename is received i.e not able to get both the arguments.
Please let me know where it is getting wrong
Regards
You can try using curl instead of built-in Jenkins methods:
curl -XPOST http://<url> -H 'Content-Type: application/octet-stream' -H 'Authorization: Basic ${auth}' --data-binary '{\"fileName\" : \"Test.txt\" }'
You can debug it first from within shell. Once it's working, wrap it in sh directive:
sh "curl ..."
Since I was running on windows so bat + curl worked for me .With this workaround I was able to transfer files using jenkins and rest
However using httpRequest from jenkins inbuild library is still not working.

How to change the metadata of all specific file of exist objects in Google Cloud Storage?

I have uploaded thousands of files to google storage, and i found out all the files miss content-type,so that my website cannot get it right.
i wonder if i can set some kind of policy like changing all the files content-type at the same time, for example, i have bunch of .html files inside the bucket
a/b/index.html
a/c/a.html
a/c/a/b.html
a/a.html
.
.
.
is that possible to set the content-type of all the .html files with one command in the different place?
You could do:
gsutil -m setmeta -h Content-Type:text/html gs://your-bucket/**.html
There's no a unique command to achieve the behavior you are looking for (one command to edit all the object's metadata) however, there's a command from gcloud to edit the metadata which you could use on a bash script to make a loop through all the objects inside the bucket.
1.- Option (1) is to use a the gcloud command "setmeta" on a bash script:
# kinda pseudo code here.
# get the list with all your object's names and iterate over the metadata edition command.
for OUTPUT in $(get_list_of_objects_names)
do
gsutil setmeta -h "[METADATA_KEY]:[METADATA_VALUE]" gs://[BUCKET_NAME]/[OBJECT_NAME]
# the "gs://[BUCKET_NAME]/[OBJECT_NAME]" would be your object name.
done
2.- You could also create a C++ script to achieve the same thing:
namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name, std::string object_name,
std::string key, std::string value) {
# you would need to find list all the objects, while on the loop, you can edit the metadata of the object.
for (auto&& object_metadata : client.ListObjects(bucket_name)) {
string bucket_name=object_metadata->bucket(), object_name=object_metadata->name();
StatusOr<gcs::ObjectMetadata> object_metadata =
client.GetObjectMetadata(bucket_name, object_name);
gcs::ObjectMetadata desired = *object_metadata;
desired.mutable_metadata().emplace(key, value);
StatusOr<gcs::ObjectMetadata> updated =
client.UpdateObject(bucket_name, object_name, desired,
gcs::Generation(object_metadata->generation()))
}
}

SoapUI print full URL path of rest request using groovy

I'm using soapui with groovy script step
I want to print the full url of my REST request.
I tried using:
myFile.append( testRunner.testCase.testSteps["My Test Name"].getProperty( "requestUri" ));
and I got null.
You will not be able to see the request info from a test step groovy script. However, the groovy script assertion has access to that information.
You can use this to easily retrieve the full endpoint:
def endpoint = messageExchange.getEndpoint()
The below is working fine for me. you can use the same code just need to change your step name.
Note: Make sure your same Test step should have run prior to below code.else you will get the error
[Cannot invoke method getURL() on null object], see error log for details.
Working Code:
def tr=testRunner.testCase.getTestStepByName("TriggerRequestTransactionsReportsService_V)
def String endPointUrlSave= tr.getHttpRequest().getResponse().getURL();
log.info "Your EndpointUrl is : " + endPointUrlSave;