While trying to set the build status of a commit through ssh, I was experiencing some difficulties. I first set the build status successfully, using a GitHub personal access token. Based on this answer, I created the following curl command:
#!/bin/bash
USER="red"
REPO="code"
COMMIT_SHA="6ec8d6ef221c3e317fa20b1f541770b8f46f065c"
MY_TOKEN="somelongpersonaltoken"
curl -H "Authorization: token $MY_TOKEN" --request POST --data '{"state": "failure", "description": "Failed!", "target_url": "https://www.stackoverflow.com"}' https://api.github.com/repos/$USER/$REPO/statuses/$COMMIT_SHA
Which sets the build status similar to the red cross below:
Next, I retrieved the GitHub commit status, using:
GET https://api.github.com/repos/$USER/$REPO/commits/$COMMIT_SHA/statuses
Which outputs:
[{"url":"https://api.github.com/repos/... ,"state":"failure","description":"Failed!","target_url":"https://www.stackoverflow.com","context":"default","created_at":"2021-12-19T10:10:20Z","updated_at":"2021-12-19T10:10:20Z"...,"site_admin":false}}]
Which is as expected.
Then for the second part, I tried to omit using a GitHub personal access token, and use my ssh credentials to set the commit build status. However, this answer seems to suggest that that is currently not possible. Hence, I would like to ask:
How can I set a GitHub commit build status using ssh credentials in Bash?
I stand by my 2013 answer and confirm, in late 2021, that using SSH for GitHub API URL seems not supported.
Even the latest GitHub CLI gh api command only proposes HTTPS calls, not SSH.
Makes an authenticated HTTP request to the GitHub API and prints the response.
The endpoint argument should either be a path of a GitHub API v3 endpoint, or "graphql" to access the GitHub API v4.
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'm trying to use a post-receive git-hook to automate the deploy of a simple maven project by triggering a Jenkins pipeline I set up. The source is hosted on a GitHub repo while Jenkins on a container running on my PC. So far, the hook is not triggered after I push to master branch.
Thing is if I try and run the script manually it just works! I also tried setting chmod +x with Git Bash (after all I'm on Windows) to the post-receive file, unfortunately without success: the hook still does not get triggered. What might be the issue?
I already tried looking for answers on similar topics here on stackoverflow, but nothing solved my issue. FYI, below the post-receive script (nothing fancy, as you can see):
#!/bin/bash
JENKINS_URL="http://localhost:8080"
JOB="deploy-to-slave-pipeline"
JENKINS_CREDENTIALS="theuser:11d422ee679503eeb328c5b1998327cc7f"
echo "Triggering Jenkins job..."
crumb=$(curl -u "$JENKINS_CREDENTIALS" -s '$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
curl -u $JENKINS_CREDENTIALS -H "$crumb" -X POST "$JENKINS_URL/job/$JOB/build?delay=0sec"
EDIT
As pointed out by #bk2204 post-receive is a server-side hook. What I needed was a webhook, which can be set in the Settings/Webhook page of your GitHub repo. Just configure it as below where Payload URL is your Jenkins URL followed by /github-webhook/:
Then all you have to do is set your Jenkins job to get triggered by GitHub, by checking the related option on the Build Triggers section as below:
And then you're good to go! Also, if you're running your Jenkins instance locally, you could use ngrok to expose it and test your CI/CD pipeline!
[ref. https://dzone.com/articles/adding-a-github-webhook-in-your-jenkins-pipeline]
A post-receive hook is run on the server side, not on the client side. That means that it's run at GitHub, assuming you're pushing to GitHub, ant not on your local machine.
Normally, you'd want a GitHub webhook to notify you of the push event, but you cannot use one here because the machine is running on localhost and such an event has to be able to on a public IP address since GitHub has to send an HTTP request to it.
my requirement is to run postman scripts in bamboo. We have a repository for the collection.json, however, as my environment file has some sensitive data like client_id, secret_id, username, password etc I can't push it to my repo.
Please advise me how can I run my collections in bamboo using Newman.
You can utilize your environmental variables if you have a Postman account:
You'll need to get the X-Api-Key for your Postman project (on your Postman account), then you'll be able to use Postman API calls to get the collection and environment ids. Here's the link with Postman API documentation.
Install Newman to your npm. Here are the details about Newman
Run a command line from your bamboo that will trigger the Postman test run.
The command will look this way:
newman run https://api.getpostman.com/collections/{{collectionId}}?apikey={{ApiKey}}
-e https://api.getpostman.com/environments/{{EnvironmentId}}?apikey={{ApiKey}}
I'm developing a bash script to automatic clone some projects and another task in dev VM's, but we have one project in Heroku and repository is in it. In my .sh file I have:
> heroku login
And this prompt to enter credentials, I read the "help" guide included on binary and documentation but I can't found anything to automatic insert username and password, I want something like this:
> heroku login -u someUser -p mySecurePassword
Exist any way similar to it?
The Heroku CLI only uses your username and password to retrieve your API key, which it stores in your ~/.netrc file ($HOME\_netrc on Windows).
You can manually retrieve your API key and add it to your ~/.netrc file:
Log into the Heroku web interface
Navigate to your Account settings page
Scroll down to the API Key section and click the Reveal button
Copy your API key
Open your ~/.netrc file, or create it, with your favourite text editor
Add the following content:
machine api.heroku.com
login <your-email#address>
password <your-api-key>
machine git.heroku.com
login <your-email#address>
password <your-api-key>
Replace <your-email#address> with the email address registered with Heroku, and <your-api-key> with the API key you copied from Heroku.
This should manually accomplish what heroku login does automatically. However, I don't recommend this. Running heroku login does the same thing more easily and with fewer opportunities to make a mistake.
If you decide to copy ~/.netrc files between machines or accounts you should be aware of two major caveats:
This file is used by many other programs; be careful to only copy the configuration stanzas you want.
Your API key offers full programmatic access to your account. You should protect it as strongly as you protect your password.
Please be very careful if you intend to log into Heroku using any mechanism other than heroku login.
You can generate a non-expiring OAuth token then pass it to the CLI via an environment variable. This is useful if you need to run Heroku CLI commands indefinitely from a scheduler and you don't want the login to expire. Do it like this (these are not actual Tokens and IDs, BTW):
$ heroku authorizations:create
Creating OAuth Authorization... done
Client: <none>
ID: 80fad839-876b-4ea0-a41e-6a9a2fb0cf97
Description: Long-lived user authorization
Scope: global
Token: ddf4a0e5-9294-4c5f-8820-b51c52fce4f9
Updated at: Fri Aug 02 2019 21:26:09 GMT+0100 (British Summer Time) (less than a minute ago)
Get the token (not the ID) from that authorization and pass it to your CLI:
$ HEROKU_API_KEY='ddf4a0e5-9294-4c5f-8820-b51c52fce4f9' heroku run ls --app my-app
Running ls on ⬢ my-app... up, run.2962 (Hobby)
<some file names>
$
By the way this also solves the problem of how to use the Heroku CLI when you have MFA enabled on your Heroku account but your machine doesn't have a web browser e.g., if you are working on an EC2 box via SSH:
$ heroku run ls --app my-app
heroku: Press any key to open up the browser to login or q to exit:
› Error: quit
$ HEROKU_API_KEY='ddf4a0e5-9299-4c5f-8820-b51c52fce4f9' heroku run ls --app my-app
Running ls on ⬢ my-app... up, run.5029 (Hobby)
<some file names>
$
EDIT: For Windows Machines
After you run heroku authorizations:create, copy the "Token", and run the following commands:
set HEROKU_API_KEY=ddf4a0e5-9299-4c5f-8820-b51c52fce4f9
heroku run ls --app my-app
If your goal is just to get the source code, you could use a simple git client. You just need the api key.
Steps to get api key
Log into the Heroku web interface
Navigate to your Account settings page
Scroll down to the API Key section and click the Reveal button
Copy your API key
Download source code using git
Use this url template for git clone
https://my_user:my_password#git.heroku.com/name_of_your_app.git
In my case the user value was my email without domain.
Example :
if mail is **duke#gmail.com**
user for heroku auth will be **duke**
Finally just clone it like any other git repositories:
git clone https://duke:my_password#git.heroku.com/name_of_your_app.git
I agree that Heroku should have by now provided a way to do this with their higher level CLI tool.
You can avoid extreme solutions (and you should, just like Chris mentioned in his answer) by simply using curl and the Heroku API. Heroku allow you to use your API Token (obtainable through your user settings / profile page on the Heroku dashboard).
You can then use the API to achieve whatever it is you wanted to do with their command line tool.
For example, if I wanted to get all config vars for an app I would write a script that did something like the following:
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Authorization: Bearer YOUR_TOKEN```
If *YOUR_APP_NAME* had only one config variable called *my_var* the response of the above call would be
{
"my_var": some_value
}
I've found using this all the time in CI tools that need access to *Heroku* information / resources.
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
}