Can I upload a package to Octopus from linux using curl without using NuGet or PowerShell? - powershell

We're building a package with Jenkins on a linux slave, and want to upload the package to an Octopus instance, but we don't have access to Nuget or Powershell on the linux box.
The Octopus examples say you can do this by
$wc = new-object System.Net.WebClient
$wc.UploadFile("http://octopus.example.com/api/packages/raw?apiKey=<Your API Key>", "YourApp.1.0.0.zip")
Can I do this with curl?

Yes, the following is the curl equivalent.
curl -X POST \
-H "X-Octopus-ApiKey: <Your API Key>" \
-F "file=#\"YourApp.1.0.0.zip\";filename=\"YourApp.1.0.0.zip\";type=application/zip" \
http://octopus.example.com/api/packages/raw?replace=true

Related

Create environment for repository using gh

Is it possible to create a new environment for a repository https://github.com/org/repo/settings/environments using the gh cli?
The only mention of environment I can find in the manual is here https://cli.github.com/manual/gh_secret_set where it says you can assign a secret to an existing environment, but it seems the environment would have to be created manually.
From what I'm seeing here in the gh cli repo that's going to be a no at this time. The issue [linked] is an enhancement request for just that.
I too wanted to know if this could be done, as it's part of my workflow to inject environment secrets.
You can still use the API to create it though.
curl -X PUT \
-H 'Authorization: Bearer ghp_...' \
-H 'Accept: application/vnd.github.v3+json' \
https://api.github.com/repos/<org>/<repo>/environments/<env>
Basically you need to create the environment first, then you can set branch policies:
jq -n '{"deployment_branch_policy": {"protected_branches": false, "custom_branch_policies": true}}'|gh api -H "Accept: application/vnd.github+json" -X PUT /repos/:owner/:repo/environments/dev --input -
gh api --method POST -H "Accept: application/vnd.github+json" "/repos/Oceaneering/it_infra_base_application_bootstrapper/environments/dev/deployment-branch-policies" -f name=dev
I wrote a python script for my use case that uses the gh cli to create environments and can include a branch pattern.
https://gist.github.com/walkerk1980/8a6f6879b32260360854a89bb880a48d

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"

Curl POST gitlab API gives 404

I am trying to push a file to my folder called "collections" in my repository using curl.
I've spent almost 2 days investigating problem and I am not sure what is the exact problem.
curl -D- -k -X GET -H "PRIVATE-TOKEN: faNFKoC4-opiDJ0FJSk" https://gitlab.example.com/api/v4/projects/592/repository/tree?path=collections
Get request works properly and I get list of files in collections folder.
The collections folder is a folder in my gitlab repository
But when I try to POST a file to that exact same folder I get 404:
curl -D- -k -X POST -H "PRIVATE-TOKEN: faNFKoC4-opiDJ0FJSk" -F "file=#C:/Documents/Folder_A/bp30_QA.csv" https://gitlab.example.com/api/v4/projects/592/repository/tree?path=collections
Am I missing some parameter? also gitlab API didn't help me very much.
Edit: Solution from Bertrand Martel helped me solve the issue
Also for everyone on windows having trouble installing jq
jq is a lightweight and flexible command-line JSON processor.
Install choco: https://chocolatey.org/install
Open powershell as administrator and run:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
After installation, run:
choco install jq
To create a new file called bp30_QA.csv in the collections folder, you can use the following :
curl -H 'PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN' \
-H "Content-Type: application/json" \
-d '{
"branch": "master",
"author_email": "johndoe#gmail.com",
"author_name": "John Doe",
"content": '"$(jq -Rs '.' bp30_QA.csv)"',
"commit_message": "create a new file"
}' "https://gitlab.com/api/v4/projects/592/repository/files/collections%2Fbp30_QA.csv"
It uses jq to wrap the content of the file in a single JSON field (check this post)

Glassfish3 server auto deployment

I get a war file daily and deploy it on glass fish server via Remote Desktop connection (Windows server). I want make it do auto deployment e.g I just put war file on some predefined location and run script and that script deploy latest war and restart glass fish server as well.
So what should I do? Any batch script or any other script?
You can put your WAR file in the auto deploy folder of GlassFish:
as-install/domains/domain1/autodeploy
Or you can use a script to deploy with the asadmin tool
Or you can use the REST interface to deploy:
curl -s -S \
-H 'Accept: application/json' -X POST \
-H 'X-Requested-By: dummy' \
-F id=#/path/to/application.war \
-F force=true http://localhost:4848/management/domain/applications/application

How do I deploy a file to Artifactory using the command line?

I've spent far more time on this than I care to admit. I am trying to just deploy one file into my Artifactory server from the command line. I'm doing this using gradle because that is how we manage our java builds. However, this artifact is an NDK/JNI build artifact, and does not use gradle.
So I just need the simplest gradle script to do the deploy. Something equivalent to:
scp <file> <remote>
I am currently trying to use the artifactory plugin, and am having little luck in locating a reference for the plugin.
curl POST did not work for me . PUT worked correctly . The usage is
curl -X PUT $SERVER/$PATH/$FILE --data-binary #localfile
example :
$ curl -v --user username:password --data-binary #local-file -X PUT "http://<artifactory server >/artifactory/abc-snapshot-local/remotepath/remotefile"
Instead of using the curl command, I recommend using the jfrog CLI.
Download from here - https://www.jfrog.com/getcli/ and use the following command (make sure the file is executable) -
./jfrog rt u <file-name> <upload-path>
Here is a simple example:
./jfrog rt u sample-service-1.0.0.jar libs-release-local/com/sample-service/1.0.0/
You will be prompted for credentials and the repo URL the first time.
You can do lots of other stuff with this CLI tool. Check out the detailed instructions here - https://www.jfrog.com/confluence/display/RTF/JFrog+CLI.
The documentation for the artifactory plugin can be found, as expected, in Artifactory User Guide.
Please note that it is adviced to use the newer plugin - artifactory-publish, which supports the new Gradle publishing model.
Regarding uploading from the command line, you really don't need gradle for that. You can execute a simple PUT query using CURL or any other tool.
And of course if you just want to get your file into Artifactory, you can always deploy it via the UI.
Take a look the Artifactory REST API, mostly you can't use scp command, instead use the curl command towards REST API.
$ curl -X POST $SERVER/$PATH/$FILE --data #localfile
Mostly it looks like
$ curl -X POST http://localhost:8081/artifactory/abc-snapshot-local/remotepath/remotefile --data #localfile
The scp command is only used if you really want to access the internal folder which is managed by artifactory
$ curl -v -X PUT \
--user username:password \
--upload-file <path to your file> \
http://localhost:8080/artifactory/libs-release-local/my/jar/1.0/jar-1.0.jar
Ironically, I'm answering my own question. After a couple more hours working on the problem, I found a sample project on github: https://github.com/JFrogDev/project-examples
The project even includes a straightforward bash script for doing the exact deploy/copy from the command line that I was looking for, as well as a couple of less straightforward gradle scripts.
As per official docs, You can upload any file using the following command:
curl -u username:password -T <PATH_TO_FILE> "https://<ARTIFACTORY_SERVER>/<REPOSITORY_PATH>/<TARGET_FILE>"
Note: The user should have write access to this path.