Github Oauth令牌用于Golang

6
我们使用AWS代码部署将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


@SalvadorDali 项目中的同事们遇到了相同的问题。 - Itachi
这是一个私有仓库吗? - holys
@ltachi,你是否使用过 go get xxx 命令?你尝试过这个吗?https://gist.github.com/shurcooL/6927554 - holys
@holys 这涉及使用实例创建SSH密钥。因此,如果实例关闭并以新的IP重新启动,则需要另一个SSH密钥。如果是auth token,我可以编写启动脚本来安装它。另外,“-u”似乎无法正常工作。 - Itachi
@ltachi 这个对你有帮助吗?https://github.com/golang/go/issues/10791 - holys
1个回答

2
这个问题有两种解决方案:
  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
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接