This is my Ruby script for GitLab API, I'm doing commits and pushes with it: https://github.com/ivanove/my_works/blob/master/Create-Repo.rb
I'm using CURL inside of it to make requests.
So the question is, how can I set namespace of repository when it's created?
This is the command I use:
cmd = %Q<curl -H "Content-Type:application/json" http://#{#host}/api/v3/projects?private_token=#{#token} -d '{"name":"#{name}","visibility_level":20,"namespace_id":"group-from-1"}'>
I added namespace_id from GitLab Documentation, but it doesn't work.
Related
Need help for creating tag for every bit bucket commit.
Please let me know if anyone have implemented this using Jenkins pipeline, if yes how can we achieve that
I'm going to assume you have some sort of Multi-branch pipeline job set up, or some mechanism to trigger a jenkins job for each commit to BitBucket. Bitbucket will need to trigger a Jenkins job for each commit via a webhook
In your pipeline code, you'll need to do the following:
Get the hash of your commit. You can get this data from the GIT_COMMIT variable in the Git Jenkins Plugin
Create a Bitbucket tag via their api with the commit hash mentioned above
curl https://api.bitbucket.org/2.0/repositories/jdoe/myrepo/refs/tags \
-s -u jdoe -X POST -H "Content-Type: application/json" \
-d '{
"name" : "new-tag-name",
"target" : {
"hash" : "a1b2c3d4e5f6",
}
}'
There are few different ways to do #2.
Put that code in a powershell script and run it from your job.
Call it directly from your jenkins pipeline like this stackoverflow answer.
Or if you plan to do this from many jobs, you could also create your own Bitbucket Api Client shared library that does the api interaction for you.
Note: You'll likely need to authenticate these Bitbucket api requests
I was able to get rid of error using env.GIT_COMMIT
I created a repo in azure, I created a bash file that cloned the repo from my CLI using:
$curl git clone https://username#dev.azure.com/organization/project/_git/reponame
which cloned the repo. My challenge now is automating the yaml file I have saved in my local machine, such that when I push, it goes to the remote repo and builds automatically. I would appreciate your ideas. Thanks
If you have not created an azure pipeline for your repo, then push the yaml file wont trigger a pipeline, since it doesnot exist.
How do I use curl request to create an Azure Pipeline from yaml file using the CLI?
If you intend to create your azure pipeline using curl request. You can call Build Definition Create restful api to create a pipeline via cli.
POST https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=5.1
See below script example:
Check here to get a personal access token.
You can call repository list rest api to get the id of your repository. You can also get the repository id for the UI (go to Project Settings--> Repositories under Repo--> select the your repository-->You will see the repository id in the address bar repositoryId=96a56858-..-...)
curl -X POST \
-u username:personalaccesstoken https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=5.1 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"name" : "MyPipelineName";
"repository" : {
"url" : "<the-https-link-to-my-Git-repo>";
"defaultBranch" : "refs/heads/master";
"id" : "Id of the repository";
"type" : "TfsGit";
};
"process" : {
"yamlFilename": "path to/my-pipeline.yml";
"type" : 2;
};
"path": "\A New Folder";
"type" : "build";
}'
You can also create azure pipeline from your azure devops project UI portal. Please check the detailed steps here. Since you already have your yaml file. You can choose Existing Azure Pipeline YAML file during the setting up wizard. See below:
To enable triggerring your pipeline automatically after your created your pipeline via above methods. You need to define triggers in your yaml file. See here for more information.
For below example: every push to master branch will trigger build on master branch.
trigger:
- master
Note: yaml file must exist in the branch to trigger a build against this branch.
You could use Azure DevOps CLI:
Store the personal access token in environment (or other way)
Create a new repo in your DevOps organization by calling az repos create
Push local git repository to that repo (#2) by calling git push command
Create a new yaml pipeline by calling az pipelines create
Then the related build will be ran.
Referring the guide below, I'm trying to upload library files to a maven repository on Artifactory; however, I'm having an error.
https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-DeployArtifactsfromArchive
We've installed Artifactory on premise Linux server, and created a maven repository to store libraries for Java app build. It's prepared because our environment does not allow access to public internet and jcenter.
We're planning to configure build job with maven on Jenkins which takes all the necessary libraries from the Artifactory repository.
The library files are given by development team. It follows maven structure because it's exported from Maven on local development PC.
I zipped the files, placed on the server and hit the below code.
curl -u user:password -X PUT http://[artifactory URL]/../[repository name] /tmp/src/archive.zip
Expected successful files upload to the repository.
However, I've got the below error.
{"errors" : [ {
"status" : 403,
"message" : ""
}]
}curl: (3) <url> malformed
I zipped the files, placed on the server and hit the below code.
curl -u user:password -X PUT http://[artifactory URL]/../[repository name] /tmp/src/archive.zip
A few issues here:
As the error suggests, your URL is malformed. There should not be a /../. Just http://[artifactory URL]/[repository name] should be fine.
You're giving curl two bare paths, which it interprets as two URLs that it should PUT to. It's clear that this isn't your intent; the second path is the file you want to upload. You need to specify that by preceding it with, say, a -T.
This call will just upload the zip file into the root of your repository, but you want it to instead extract the contents. As stated by the documentation you linked, this can be done if you pass a header X-Explode-Archive: true or X-Explode-Archive-Atomic: true.
So your call should be something like:
curl -u user:password -X PUT http://[artifactory URL]/[repository name] -T /tmp/src/archive.zip -H 'X-Explode-Archive: true'
I figured out the cause. The reason was because our version is not Artifactory Pro.
I was told it's pro but confirmed the actually installed SW is OSS version.
I'm using Jenkins to run tests before deploying to Heroku using a git publisher plugin. I want to sandwich my deploy with maintenance mode ON/OFF but don't want to install the heroku toolbelt (reference) on my jenkins machine (nor do I want to go to the Web UI and toggle the maintenance mode manually). I don't want to install the heroku toolbelt on jenkins because it adds to the customization and configuration I'd need to do to the jenkins installation and I want to keep my jenkins instance as disposable as possible...driven rather by source code pipelines (Jenkinsfile).
Ideas:
utilize a docker image that has the heroku toolbelt installed on it
utilize a multi-buildpack that does it for me?
Any suggestions or experience on how I might accomplish this?
You can use heroku's platform api to do this:
https://devcenter.heroku.com/articles/platform-api-reference#app-update
The following curl request will put your app into maintenance mode:
curl -n -X PATCH https://api.heroku.com/apps/$APP_ID_OR_NAME \
-d '{"maintenance": true}' \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3"
-H "Authorization: Bearer <heroku token>
You will just need to replace the app name, as well as the token.
You can generate OAuth tokens using the cli-oauth cli plugin locally.
I am having trouble with my bash script to fork a GitHub repo using cUrl.
The gitHub API doc for creating a fork.
I've tried many variations:
curl -u $my_user_name https://api.github.com/repos/forks -d "{\"owner\":\"$upstream_repo_username\",\"repo\":\"$upstream_repo_name\"}"
and
curl -u $my_user_name https://api.github.com/repos/'$upstream_repo_username'/'$upstream_repo_name'/forks
yield the following error:
{
"message": "Not Found",
"documentation_url": "https://developer.github.com/v3"
}
In Contrast, the following Creates a new empty github repo, as expected:
curl -u $my_user_name https://api.github.com/user/repos -d "{\"name\":\"$upstream_repo_name\"}"
Any ideas on how to create a fork of a repo from the command line?
I have a bash script that:
- creates an empty repo on github with the name of the repo I'm going to clone,
- clones a repo from another user locally, and
- pushes my cloned repo into the empty repo I created in my github account
- sets origin and upstream remotes appropriately
However, this method does not keep a connection within GitHub to the source (forked) repo. I particularly like the convenience of the forked link appearing below my own repo name ;-)
The goal is to do all my cloning (and forking) from the command line.
I do not want to open a browser, navigate to the repository I wish to fork, just to access that "Fork" button.. only return back to the command line to finish the process.
Alternatively, can I turn a cloned repo into a forked one from the command line? (ie some command line api command that will re-create those internal github links that forks possess?)
Here is my working bash script:
curl -u $my_user_name https://api.github.com/repos/$upstream_repo_username/$upstream_repo_name/forks -d ''
Example using hard-coded strings instead of bash variables:
curl -u 'SherylHohman' https://api.github.com/repos/octocat/Hello-World/forks -d ''
Notice I moved -d '' to the end to avoid login errors.
The request requires authentication.
I provide this via curl's -u parameter (as opposed to using OAuth2).
When I used the -u $my_user_name option,
I had to move the -d '' to after the URI
- it resulted in login errors if placed between -u 'username' and the URI.
It turns out the Main source of errors in my script with bash-syntax.
I had quotation marks surrounding bash variables, that should Not have been there.
(..just Solving a pain point without really knowing bash or curl)
Additionally, as #YuriSchimke pointed out, this particular URI required parameters to be passed in the URI. Passing these options as json is not an option, unlike the URI for Creating a New Blank repo.
Here is why I was baffled over how to send this data in the URI:
Using curl, the default request is a GET.
In curl, POST requests are made by adding the -d (equivalent to --data) flag followed by the data to be sent.
I needed to send a POST request.
The format for GitHub API is that GET (and POST eg. CreateRepo) requests can sometimes send some parameters as json or query strings
NOTE: documentation for GitHub API appears to be slightly incomplete, as I do not see any mention of the API allowing json, only query string.
I suppose in this case, the data is sandwiched between two static URI parts, making it impossible to send as json values.
I was at a loss how to use the -d flag without data:
If I simply left it off, the API call was processed as a GET.
It returned information about the repo I wanted to fork,
instead of forking the repo to my account.
#YuriSchimke's post gave me that "Ahaa!". Thanks! I'm laughing that it didn't cross my mind. I'm grateful Yuri's made this so obvious! (Thanks Again).
The documentation shows the owner and repo being part of the request URI
curl -d '' https://api.github.com/repos/octocat/Hello-World/forks
https://developer.github.com/v3/repos/forks/
This appears to work fine.
It's much easier to create or fork a repo using the hub command line tool.
Installation instructions: https://github.com/github/hub#installation
It can do much more, but here's how to fork a online repo using the command line.
However, there is conflicting information out there, so it can be a
bit confusing.
To fork a online repo owned by somebody else do:
clone the repo using git clone
git clone ssh://git#github.com/keras-users/keras.git
change into the cloned repo
cd keras
create the fork online
hub fork
To fork your own repo that is already hosted on github:
Github doesn't allow you to fork your own repo,
So you need to first create a clone of your own repo on your computer,
Then you can create the cloned repo on the github website
clone your repo to your computer
git clone ssh://git#github.com/alpha_beta_gamma/clone_repo.git
clone the repo on your computer to another repo (also on your computer)
git clone clone_repo clone_repo2
change directory to the new cloned repo
cd clone_repo2
create the cloned repo on Github
hub create
This will create a new repo on GitHub.
set the URL of the remote repo:
git remote set-url https://github.com/username/clone_repo2
push the local repo to the online repo
git push
I use hub.
I have also set git as alias for it so that I don't have to worry everytime what command comes under git and what under hub, so my code looks just like I an running git. Follow installations here.
$ git clone <github_repo>
$ cd <github_repo>
$ git fork
PS: For the above code to work, before executing it in your .bashrc or .bash_profile you need to put the following
alias git=hub
I used this command to fork on github enterprise:
curl -vX POST \
https://git.redacted.com/api/v3/repos/$upstream_repo_username/$upstream_repo_name/forks?access_token=$api-token \
-d #gh-fork.json \
--header "Content-Type: application/json"
gh-fork.json:
{
"organization": "org-to-fork-to",
"description": "",
"homepage": "https://git.redacted.com",
"private": false
}