LoginSignup
9
3

More than 5 years have passed since last update.

isomorphic-git でブラウザから GitHub に git push する

Last updated at Posted at 2018-06-06

tl;dr

isomorphic-git の README には、 GitHub の push は cors を認めていないのでブラウザから git push できないとある。サーバー経由でcorsを迂回した。できた。

やり方

remote に cors-buster 経由の github のhttps アドレスを登録する。

こんな感じ

.git/config
[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  ignorecase = true
  precomposeunicode = true
[remote "origin"]
  url = https://github.com/mizchi-sandbox/igit-playground.git
  fetch = +refs/heads/*:refs/remotes/origin/*

[remote "origin-proxied"]
  url = https://cors-buster-tbgktfqyku.now.sh/github.com/mizchi-sandbox/igit-playground
  fetch = +refs/heads/*:refs/remotes/origin/*

origin がいつもの。 origin-proxied が今回使う URL

main.js
const git = require("isomorphic-git");
const path = require("path");
const fs = require("fs");

const repo = {
  dir: ".",
  fs
};

const main = async () => {
  console.log("start");
  let pushResponse = await git.push({
    ...repo,
    remote: "origin-proxied",
    ref: "master",
    authUsername: process.env.GITHUB_TOKEN,
    authPassword: process.env.GITHUB_TOKEN
  });
  console.log(pushResponse);
  console.log("done");
};

main();

GitHubの Personal Access Token を取ってくる。ググれば出るので略。

GITHUB_TOKEN を与えて実行する

> ~/s/igit-playground on master  env GITHUB_TOKEN=<your-toke> node main.js
start
{ ok: [ 'unpack', 'refs/heads/master' ] }
done

できた。

実際に push されたリポジトリがこちら https://github.com/mizchi-sandbox/igit-playground

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3