You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6KB

  1. #!/bin/bash
  2. set -e
  3. if [ -z "$GITHUB_TOKEN" ]; then
  4. echo ""
  5. echo "GITHUB_TOKEN environment variable is missing"
  6. echo ""
  7. echo "TRAVIS DEPLOY KEY GENERATOR"
  8. echo ""
  9. echo "What it does : generate a new private and public key, add the public key as a deploy key
  10. with write access to the origin remote github repository, encrypt the private key as
  11. github_deploy_key.enc and add the configuration necessary to use it in .travis.yml file"
  12. echo ""
  13. echo "Use it like this:"
  14. echo ""
  15. echo " GITHUB_TOKEN=\`cat ~/secret/GITHUB_TOKEN\` ./generate_travis_deploy_key"
  16. echo ""
  17. echo "where ~/secret/GITHUB_TOKEN is a file containing a github token with write access to the current repository : (origin)"
  18. echo ""
  19. echo "You must have the travis executable installed on your system and available in the PATH"
  20. echo ""
  21. exit
  22. fi
  23. url="$(git config --get remote.origin.url)"
  24. reponame="$(echo $url | cut -d/ -f2 | cut -d. -f1)"
  25. # generate a new private and public key
  26. ssh-keygen -t rsa -b 4096 -f github_deploy_key -N '' -C $url -q 1>/dev/null
  27. pubkey="$(cat github_deploy_key.pub)"
  28. # add the PUBLIC key to the github repository as a deploy key with write access
  29. curl https://api.github.com/repos/konnectors/$reponame/keys -H "Authorization: token $GITHUB_TOKEN" --data @- << EOF
  30. {
  31. "title": "travis deploy key",
  32. "key": "$pubkey",
  33. "read_only": false
  34. }
  35. EOF
  36. # use travis to encrypt the private key as github_deploy_key.enc and remove the private key
  37. travis encrypt-file github_deploy_key --add --no-interactive -w /tmp/github_deploy_key -f --pro
  38. git add github_deploy_key.enc
  39. # cleaning
  40. rm github_deploy_key
  41. rm github_deploy_key.pub