2

I have a Personal Access Token from Github that I use in many of my projects. Since the token has read/write ability for all my repos, it's important I use the Travis Command Line Tool to encrypt the GITHUB_TOKENand place it in my .travis.yml as a secure variable:

travis encrypt GITHUB_TOKEN=****secret**** --add

The Problem

  • The GITHUB_TOKEN value is a hard to remember string of random characters, so every time I need it I first have to go find it, and then copy n' paste it into git bash.
  • Whenever I use the travis encrypt method, it associates the GITHUB_TOKEN with ONLY the repository I'm in.

enter image description here

Question

Is it possible to make this travis command an alias I can use over and over?

[alias]
git repo-encrypt = "travis encrypt GITHUB_TOKEN=****secret**** --add"

If so, how and where?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Oneezy
  • 4,881
  • 7
  • 44
  • 73

1 Answers1

3

The simple way to add the alias would be to run this one-liner:

git config --global alias.repo-encrypt '!travis encrypt GITHUB_TOKEN=****secret**** --add'

Alternatively, you can run git config --global --edit to open the global Git configuration in your configured text editor (controlled by the core.editor config value of Git). Then add the following to the file:

[alias] 
    repo-encrypt = "!travis encrypt GITHUB_TOKEN=****secret**** --add"

After you add the alias, running git repo-encrypt will execute the Travis command. For future reference, starting a Git alias with a ! makes it execute the command as though it were a normal shell, instead of simply appending the alias onto the end of the git command as it would normally.

See the Git SCM Book page on aliases for more information.

Emily Mabrey
  • 1,528
  • 1
  • 12
  • 29
  • 1
    Also, just in case you need a different level of configuration: `--local` adds the alias to this repository only; `--global` adds the alias to this user's config file; `--system` adds the alias to the Git install-wide config file; `--file ` adds the alias to the specified git config file. – Emily Mabrey Dec 14 '16 at 07:42
  • Hi @Emily M, I'm curious if it's possible to customize these alias names to allow for more control? I.E., instead of my `repo-encrypt` command, would it be possible to create an `encrypt:github` command, so that the `encrypt:` could be used with other things I might need to encrypt in the future? I.E. ... `encrypt:twitter` , `encrypt:fb` , `encrypt:youtube`, etc... I tried this idea out but it didn't seem to work – Oneezy Dec 14 '16 at 18:23
  • 1
    You can do this using bash: `github(){ echo github;}; twitter(){ echo twitter;}; facebook(){ echo facebook;}; if [[ $(type -t "$1") == "function" ]]; then "$1"; else echo "There is no defined function for $1"; fi;` defines a script which executes the first argument as a function if such a function exists. So calling " – Emily Mabrey Dec 14 '16 at 19:34
  • 1
    If you want to avoid using a bash script file you can add the script as a one-line git alias as follows: `git config --global alias.encrypt-for '!github(){ echo github;}; twitter(){ echo twitter;}; facebook(){ echo facebook;}; if [[ $(type -t "$1") == "function" ]]; then "$1"; else echo "There is no defined function for $1"; fi;'` Then running `git encrypt-for facebook` would run the facebook() function, which would execute and echo "facebook" to the console. Of course you need to add the service-specific code to the functions and replace the echo commands, they are just placeholders. – Emily Mabrey Dec 14 '16 at 19:36
  • The script from earlier is bugged, and the original fix I have deleted because it changed more than actually required to fix the bug. Use `github(){ echo github;};twitter(){ echo twitter;};facebook(){ echo facebook;};if [[ $(type -t "$1") == "function" ]];then "$1"; exit 0; else echo "There is no defined function for $1"; exit 1; fi;` instead of the scripts posted above. The change is the addition of two "exit" commands and it applies to both the script file approach and the git alias – Emily Mabrey Dec 16 '16 at 08:13
  • 1
    This bugged git alias eventually led to me posting another question, which has now been answered [here](http://stackoverflow.com/q/41151579/2446574). It explains why the above git alias (`'!github(){ echo github;}; twitter(){ echo twitter;}; facebook(){ echo facebook;}; if [[ $(type -t "$1") == "function" ]]; then "$1"; else echo "There is no defined function for $1"; fi;'`) executes twice instead of once. – Emily Mabrey Dec 23 '16 at 21:40