Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Oauth token for Golang

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.

like image 822
Itachi Avatar asked Nov 30 '15 06:11

Itachi


People also ask

How do I enable OAuth on GitHub?

In the top right corner of GitHub.com, click your profile photo, then click Your organizations. Next to the organization, click Settings. In the "Integrations" section of the sidebar, click Third-party access. Under "Third-party application access policy," click Setup application access restrictions.

Does GitHub use OAuth?

GitHub's OAuth implementation supports the standard authorization code grant type and the OAuth 2.0 Device Authorization Grant for apps that don't have access to a web browser. If you want to skip authorizing your app in the standard way, such as when testing your app, you can use the non-web application flow.


Video Answer


1 Answers

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://[email protected]/org/project.git
    
like image 141
Caleb Avatar answered Sep 20 '22 14:09

Caleb