Travis CI - Enabling repository build via API - travis-ci

I can't find how to enable a repository to build in Travis CI through the API in the docs. It seems flicking the switch is a manual step, is that correct?
Specifically can we flick the switch shown in the image below programmatically?

The end-point for this is kind of misleading and also it takes more than one step to achieve this:
Force a synchronization just to make sure the repo will be there
Use users/sync end-point
Obtain the repository ID (yes, you must have it)
Use repos/repo.slug end-point
Flick the switch
Use hooks/repo.id end-point
If you are working with Python you can use TravisPy to achieve this.
from travispy import TravisPy
travis = TravisPy.github_auth('github token')
repo = travis.repo('menegazzo/travispy')
repo.enable() # Switch is now on
repo.disable() # Switch is now off

Related

Programmatically Create Runner for BitBucket Pipelines

Is it possible to programmatically create and register a runner in bitbucket pipelines, in other words without having to create it first via the BitBucket UI.
The docker command provided requires a runner UUID, which must be created when creating the runner through the UI. Is there a way to programmatically create it through the BitBucket API? It seems a bit backward to have to create the runner first just to get the UUID so you can then deploy it.
With GitHub Actions Self Hosted runners, a runner can be created and registered to GitHub using a temporary token, but it does not seem like BitBucket have a dopted this approach, at least yet.
At the time of writing the Bitbucket API does not allow for this. There are two open feature requests for Bitbucket Runner APIs, BCLOUD-21708 and BCLOUD-21309, that may benefit from some votes.

Switch api url by changing branch

Tell me please what's the right way to change e.g. an api url by changing the git branch for requests to the right server.
i have two servers (for example):
https://www.production.com
https://www.development.com
and two branch:
master
dev
is there any way to do like this:
let uploadSomethingUrl = url + "/upload/something"
where url is set depending on the checked out branch
"dev" is checked out -> https://www.development.com/upload/something
"master" is checked out -> https://www.production.com/upload/something
No, you cannot check which branch you currently checked out and react on it in code.
But what you can do is set configuration for Debug and Release Builds (or any other custom you want). It may take some fiddling around if you haven't done this before, but this is a clean way to achieve your goal. Here is a guide on how to do it
using xcconfig for you xcode project
You can access these setting in your code, read here:
How can I access a user-defined Xcode build setting?

pre-commit check or Remote-run missing with Github setup & Jenkins

Just to quote as an example one can submit a remote-run with some tool like TeamCity (similar to Jenkins) where it will apply delta/patch on what user is trying to commit & produces result whether changes is good from set-of configured checks for that project.
With Github & Jenkins, can such validation be achieved with any plugins out there?, which will avoid breaking a build?
I know with pull-request & status check one can achieve similar end-result. But without commit/push to remote repo of Git - is there a way Jenkins can handle this validation & produce initial result ??
It isn't possible to have GitHub perform checks on data it doesn't have, so if you don't push the data to the remote server, GitHub won't know anything about it and therefore will do nothing.
Jenkins does have a REST API that you could use to do this, provided you equipped each developer with appropriate credentials. However, this is not a common situation and wouldn't be a recommended configuration.
You'd be better off with a script in the repository that users could install as a hook or invoke from a hook that would perform the testing you want. If your CI jobs run a script in your repository, then sharing code between them should be easy.
Note that you shouldn't mandate pre-commit hooks, since they can interfere with advanced users (who may make intentionally incomplete temporary commits) and they can be disabled by users. Any sort of required checks should be done as part of CI, where policy can be enforced appropriately.

Automatically triggering Travis on push to a different repository?

Is there a way to trigger Travis CI build for repository X each time there's a push to repository Y? Specifically, I want my builds to start each time there's a push to http://github.com/tensorflow/tensorflow
Good question! Here are some solutions that I could think of:
If you have admin privileges on the repo (or know someone who does), you could create a webhook that subscribes to the push event and when triggered, start a build on Travis CI using the Travis API.
This would entail:
Creating a new GitHub webhook over on http://github.com/tensorflow/tensorflow/settings/hooks/new. While of course, customizing the settings as you see fit, but with the information I have, I recommend using the application/json content type and only have GitHub trigger the webhook with the push event.
Write a small web app expecting the HTTP POST payloads from GitHub and start builds using Travis CI's API. This web app can be written in any language but it must be deployed to somewhere that is always awake and listening (to prevent missing builds).
Here's my example of that.
post "/push-webhook" do
uri = URI.parse("https://api.travis-ci.org/repo/your-org/your-repo/requests")
request = Net::HTTP::Get.new(uri.request_uri)
request["Content-Type"] = "application/json"
request["Accept"] = "application/json"
request["Travis-API-Version"] = "3"
request["Authorization"] = "token your-token"
body = { "request" => { "branch" => "master" } }
request.body = body.to_json
response = http.request(request)
end
And voila! Once this web app is deployed, and your GitHub webhook is configured properly, you should see builds to run on Travis CI with every new push on http://github.com/tensorflow/tensorflow.
Helpful documentation
https://docs.travis-ci.com/user/triggering-builds/
https://developer.github.com/webhooks/
https://developer.github.com/webhooks/configuring/
However, if you do not have admin privileges on the repo, you could create a mirror of repository that is in your control and then follow the instructions above (with a few differences). From the little research I did, it's not possible (or at least not easy) to create a mirror of a repository all on GitHub without admin access of the original/official repository.
With that said, I found a workaround that should work.
Import tensorflow/tensorflow to GitLab and use the Mirror Repository feature so it mirrors http://github.com/tensorflow/tensorflow as your GitLab repo's upstream.
From there, follow the instructions above as normal except use GitLab's webhook API instead of GitHub's to send push events to trigger our web app to start builds on Travis CI.
Helpful documentation
https://about.gitlab.com/2016/12/01/how-to-keep-your-fork-up-to-date-with-its-origin/
https://docs.gitlab.com/ce/user/project/integrations/webhooks.html
I hope this information was helpful. Please let me know if you have any other questions and I'd be happy to help in any way I can. :)

Travis basic understanding and set up

I have a little-to-none understanding of what travis-ci is. All I know ( and I could be wrong ) is that, it is where a github code is hosted. This code has a test written in Unit Tests, and Travis is a way to automatically test whenever a new commit is pushed to github. As I said, I could be totally wrong. Wikipedia article didn't help me understand this.
But, in the case that it is a test automation software, then how do I set up a simple application? I have a github account, and I know the first step is to register in travis, connect a github account and place a travis.yml file but what is next?
Any help would be appreciated.
What Travis CI does is connects to your GitHub repository to run you code's tests (Scripts that write to confirm that your code is working) on every commit you make on GitHub. It also runs on pull requests, which makes it great for checking if a pull request breaks anything. You can get started by going to the Travis CI and hitting sign in on the top right corner. You'll sign in with your GitHub account, and on the profile page you can turn on the repositories that you want Travis CI to test. Next, you'll need to create a .travis.yml in the repositories you want to test. You didn't specify what language you to would like to use so I'll refer you to the Travis CI getting started page which has examples in every language that Travis CI supports.
If you want to read up more I reccomend taking a look at the Travis CI docs.

Resources