Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
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.

163 lines
7.3 KiB

  1. # Git authentication with SSH keys
  2. When using [environment variables](../usage/ci-configuration.md#authentication) to set up the Git authentication, the remote Git repository will automatically be accessed via [https](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols#_the_http_protocols), independently of the [`repositoryUrl`](../usage/configuration.md#repositoryurl) format configured in the **semantic-release** [Configuration](../usage/configuration.md#configuration) (the format will be automatically converted as needed).
  3. Alternatively the Git repository can be accessed via [SSH](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols#_the_ssh_protocol) by creating SSH keys, adding the public one to your Git hosted account and making the private one available on the CI environment.
  4. **Note:** SSH keys allow to push the [Git release tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) associated to the released version. Some plugins might also require an API token. See each plugin documentation for additional information.
  5. ## Generating the SSH keys
  6. In your local repository root:
  7. ```bash
  8. $ ssh-keygen -t rsa -b 4096 -C "<your_email>" -f git_deploy_key -N "<ssh_passphrase>"
  9. ```
  10. `your_email` must be the email associated with your Git hosted account. `ssh_passphrase` must be a long and hard to guess string. It will be used later.
  11. This will generate a public key in `git_deploy_key.pub` and a private key in `git_deploy_key`.
  12. ## Adding the SSH public key to the Git hosted account
  13. Step by step instructions are provided for the following Git hosted services:
  14. - [GitHub](#adding-the-ssh-public-key-to-github)
  15. ### Adding the SSH public key to GitHub
  16. Open the `git_deploy_key.pub` file (public key) and copy the entire content.
  17. In GitHub **Settings**, click on **SSH and GPG keys** in the sidebar, then on the **New SSH Key** button.
  18. Paste the entire content of `git_deploy_key.pub` file (public key) and click the **Add SSH Key** button.
  19. Delete the `git_deploy_key.pub` file:
  20. ```bash
  21. $ rm git_deploy_key.pub
  22. ```
  23. See [Adding a new SSH key to your GitHub account](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/) for more details.
  24. ## Adding the SSH private key to the CI environment
  25. In order to be available on the CI environment, the SSH private key must be encrypted, committed to the Git repository and decrypted by the CI service.
  26. Step by step instructions are provided for the following environments:
  27. - [Travis CI](#adding-the-ssh-private-key-to-travis-ci)
  28. - [Circle CI](#adding-the-ssh-private-key-to-circle-ci)
  29. ### Adding the SSH private key to Travis CI
  30. Install the [Travis CLI](https://github.com/travis-ci/travis.rb#installation):
  31. ```bash
  32. $ gem install travis
  33. ```
  34. [Login](https://github.com/travis-ci/travis.rb#login) to Travis with the CLI:
  35. ```bash
  36. $ travis login
  37. ```
  38. Add the [environment](https://github.com/travis-ci/travis.rb#env) variable `SSH_PASSPHRASE` to Travis with the value set during the [SSH keys generation](#generating-the-ssh-keys) step:
  39. ```bash
  40. $ travis env set SSH_PASSPHRASE <ssh_passphrase>
  41. ```
  42. [Encrypt](https://github.com/travis-ci/travis.rb#encrypt) the `git_deploy_key` (private key) using a symmetric encryption (AES-256), and store the secret in a secure environment variable in the Travis environment:
  43. ```bash
  44. $ travis encrypt-file git_deploy_key
  45. ```
  46. The `travis encrypt-file` will encrypt the private key into the `git_deploy_key.enc` file and output in the console the command to add to your `.travis.yml` file. It should look like `openssl aes-256-cbc -K $encrypted_KKKKKKKKKKKK_key -iv $encrypted_VVVVVVVVVVVV_iv -in git_deploy_key.enc -out git_deploy_key -d`.
  47. Copy this command to your `.travis.yml` file in the `before_install` step. Change the output path to write the unencrypted key in `/tmp`: `-out git_deploy_key` => `/tmp/git_deploy_key`. This will avoid to commit / modify / delete the unencrypted key by mistake on the CI. Then add the commands to decrypt the ssh private key and make it available to `git`:
  48. ```yaml
  49. before_install:
  50. # Decrypt the git_deploy_key.enc key into /tmp/git_deploy_key
  51. - openssl aes-256-cbc -K $encrypted_KKKKKKKKKKKK_key -iv $encrypted_VVVVVVVVVVVV_iv -in git_deploy_key.enc -out /tmp/git_deploy_key -d
  52. # Make sure only the current user can read the private key
  53. - chmod 600 /tmp/git_deploy_key
  54. # Create a script to return the passphrase environment variable to ssh-add
  55. - echo 'echo ${SSH_PASSPHRASE}' > /tmp/askpass && chmod +x /tmp/askpass
  56. # Start the authentication agent
  57. - eval "$(ssh-agent -s)"
  58. # Add the key to the authentication agent
  59. - DISPLAY=":0.0" SSH_ASKPASS="/tmp/askpass" setsid ssh-add /tmp/git_deploy_key </dev/null
  60. ```
  61. See [Encrypting Files](https://docs.travis-ci.com/user/encrypting-files) for more details.
  62. Delete the local private key as it won't be used anymore:
  63. ```bash
  64. $ rm git_deploy_key
  65. ```
  66. Commit the encrypted private key and the `.travis.yml` file to your repository:
  67. ```bash
  68. $ git add git_deploy_key.enc .travis.yml
  69. $ git commit -m "ci(travis): Add the encrypted private ssh key"
  70. $ git push
  71. ```
  72. ### Adding the SSH private key to Circle CI
  73. First we encrypt the `git_deploy_key` (private key) using a symmetric encryption (AES-256). Run the following `openssl` command and *make sure to note the output which we'll need later*:
  74. ```bash
  75. $ openssl aes-256-cbc -e -p -in git_deploy_key -out git_deploy_key.enc -K `openssl rand -hex 32` -iv `openssl rand -hex 16`
  76. salt=SSSSSSSSSSSSSSSS
  77. key=KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
  78. iv =VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
  79. ```
  80. Add the following [environment variables](https://circleci.com/docs/2.0/env-vars/#adding-environment-variables-in-the-app) to Circle CI:
  81. - `SSL_PASSPHRASE` - the value set during the [SSH keys generation](#generating-the-ssh-keys) step.
  82. - `REPO_ENC_KEY` - the `key` (KKK) value from the `openssl` step above.
  83. - `REPO_ENC_IV` - the `iv` (VVV) value from the `openssl` step above.
  84. Then add to your `.circleci/config.yml` the commands to decrypt the ssh private key and make it available to `git`:
  85. ```yaml
  86. version: 2
  87. jobs:
  88. coverage_test_publish:
  89. # docker, working_dir, etc
  90. steps:
  91. - run:
  92. # Decrypt the git_deploy_key.enc key into /tmp/git_deploy_key
  93. - openssl aes-256-cbc -d -K $REPO_ENC_KEY -iv $REPO_ENC_IV -in git_deploy_key.enc -out /tmp/git_deploy_key
  94. # Make sure only the current user can read the private key
  95. - chmod 600 /tmp/git_deploy_key
  96. # Create a script to return the passphrase environment variable to ssh-add
  97. - echo 'echo ${SSH_PASSPHRASE}' > /tmp/askpass && chmod +x /tmp/askpass
  98. # Start the authentication agent
  99. - eval "$(ssh-agent -s)"
  100. # Add the key to the authentication agent
  101. - DISPLAY=":0.0" SSH_ASKPASS="/tmp/askpass" setsid ssh-add /tmp/git_deploy_key </dev/null
  102. # checkout, restore_cache, run: yarn install, save_cache, etc.
  103. # Run semantic-release after all the above is set.
  104. ```
  105. The unencrypted key is written to `/tmp` to avoid to commit / modify / delete the unencrypted key by mistake on the CI environment.
  106. Delete the local private key as it won't be used anymore:
  107. ```bash
  108. $ rm git_deploy_key
  109. ```
  110. Commit the encrypted private key and the `.circleci/config.yml` file to your repository:
  111. ```bash
  112. $ git add git_deploy_key.enc .circleci/config.yml
  113. $ git commit -m "ci(cicle): Add the encrypted private ssh key"
  114. $ git push
  115. ```