Github的OAuth令牌用于Golang

huangapple go评论81阅读模式
英文:

Github Oauth token for Golang

问题

我们使用AWS CodeDeploy将Github项目部署到Ec2实例中,每次部署时都会要求提供Github用户名和密码以下载存储库。找到以下解决方法:

  1. 提供用户名和密码(不推荐)
  2. 设置SSH密钥(由于实例的IP地址不断变化,不可行)
  3. 使用OAuth令牌

为PHP存储库设置OAuth是通过将其添加到composer auth.json文件中完成的。.composer/auth.json

{
    "http-basic": {},
    "github-oauth": {"github.com": "xyzasasasauhu"}
}

但是找不到在Golang项目中执行此操作的方法。通常,我们希望能够在不显式提供凭据的情况下执行go get https://github.com/username/reponame命令。

英文:

We use AWS code deploy to deploy the Github projects to Ec2 instances every time it deploys it asks for Github username and password to download the repository. Found following ways to solve this

  1. Supply Uname & Pwd (not preferred)
  2. Setup SSH Key (not possible as the instance keeps changing ip)
  3. Oauth token

Setting up Oauth for PHP repository was done by adding it in composer auth.json .composer/auth.json.

{
    "http-basic": {},
    "github-oauth": {"github.com": "xyzasasasauhu"}
}

But couldn't find a way to do this for Golang project. Typically we want to achieve go get https://github.com/username/reponame without supplying the credentials explicitly.

答案1

得分: 2

这个问题有两个解决方案:

  1. 不部署代码。Go是一种静态编译的编程语言,你不需要在打算运行Go程序的服务器上放置Go源代码。

  2. 不使用go get来获取你的私有GitHub仓库的代码。只要代码最终放在正确的子目录($GOPATH/src/github.com/org/project),Go就不关心它是如何到达那里的。只需在构建脚本中添加几个命令:

     DIR=$GOPATH/src/github.com/org/project
     TOKEN=yourtoken
    
     if [ -d $DIR ]; then
       cd $DIR
       git reset --hard  
       git clean -dfx
     else
       mkdir -p $DIR
       cd $DIR
       git init  
     fi
    
     git pull https://$TOKEN@github.com/org/project.git
    
英文:

There are two solutions to this problem:

  1. Don't deploy code. Go is a statically compiled programming language. There's no need to have Go source code on the server you intend to run the Go program on.

  2. Don't use go get to get the code for your private GitHub repo. As long as the code ends up in the correct subdirectory ($GOPATH/src/github.com/org/project), Go doesn't care how it got there. Simply add to your build script a few commands:

     DIR=$GOPATH/src/github.com/org/project
     TOKEN=yourtoken
    
     if [ -d $DIR ]; then
       cd $DIR
       git reset --hard  
       git clean -dfx
     else
       mkdir -p $DIR
       cd $DIR
       git init  
     fi
    
     git pull https://$TOKEN@github.com/org/project.git
    

huangapple
  • 本文由 发表于 2015年11月30日 14:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/33992327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定