|
| 1 | +# github-secrets-writer |
| 2 | + |
| 3 | +**github-secrets-writer** is a command-line tool that simplifies the process of creating or updating [Github secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) by carrying out the encryption for you, and writing the secrets to Github via the API. |
| 4 | +> Secrets are encrypted environment variables created in a repository and can only be used by GitHub Actions |
| 5 | +
|
| 6 | +**Table of Contents** |
| 7 | +* [What's all the fuss?](#whats-all-the-fuss) |
| 8 | +* [Installation](#installation) |
| 9 | + + [Binaries](#binaries) |
| 10 | + + [Via Go](#via-go) |
| 11 | +* [Usage](#usage) |
| 12 | + + [Write secrets](#write-secrets) |
| 13 | + |
| 14 | +## What's all the fuss? |
| 15 | + |
| 16 | +GitHub secrets are encrypted using public-key authenticated encryption and the Poly1305 cipher algorithm. The [Github developer documentation](https://developer.github.com/v3/actions/secrets/#create-or-update-a-secret-for-a-repository) suggests carrying out the encryption using [LibSodium](https://libsodium.gitbook.io/doc/bindings_for_other_languages). |
| 17 | + |
| 18 | +😞 Without **github-secrets-writer**, a user would have to piece together bits of code to: |
| 19 | + |
| 20 | +1. Encrypt a secret using LibSodium (requires installing dependencies) |
| 21 | +2. Write the secret to Github using the API |
| 22 | +3. Repeat (1) and (2) for multiple secrets |
| 23 | + |
| 24 | +🚀 **github-secrets-writer** offers the user some convenience by doing all the above for you, without the need to install additional dependencies. It comes as a binary and it uses Go's supplementary [cryptography libraries](https://go.googlesource.com/crypto) (that are interoperable with LibSodium) to carry out the encryption, then writes the secrets to Github using the [Go library for accessing the GitHub API](https://github.com/google/go-github). |
| 25 | + |
| 26 | +## Installation |
| 27 | +### Binaries |
| 28 | +For installation instructions from binaries please visit the [Releases Page](https://github.com/doodlesbykumbi/github-secrets-writer/releases). |
| 29 | + |
| 30 | +As an example, if you wanted to install v0.1.0 on OSX you would run the following commands in your terminal: |
| 31 | +``` |
| 32 | +# Download the release archive with the binary |
| 33 | +wget https://github.com/doodlesbykumbi/github-secrets-writer/releases/download/v0.1.0/github-secrets-writer_darwin_amd64.tar.gz |
| 34 | +
|
| 35 | +# Uncompress the release archive to /usr/local/bin. You might need to run this with `sudo` |
| 36 | +tar -C /usr/local/bin -zxvf github-secrets-writer_darwin_amd64.tar.gz |
| 37 | +``` |
| 38 | + |
| 39 | +### Via Go |
| 40 | +```console |
| 41 | +$ go get -u github.com/doodlesbykumbi/github-secrets-writer |
| 42 | +``` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +**github-secrets-writer** uses flags to specify the Github repository and the secrets to write to it. |
| 47 | + |
| 48 | +NOTE: An OAuth token **must** be provided via the `GITHUB_TOKEN` environment variable, this is used to authenticate to the Github API. Access tokens require [`repo` scope](https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/#available-scopes) for private repos and [`public_repo` scope](https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/#available-scopes) for public repos. GitHub Apps must have the `secrets` permission to use the API. Authenticated users must have collaborator access to a repository to create, update, or read secrets. |
| 49 | + |
| 50 | +```console |
| 51 | +$ github-secrets-writer -h |
| 52 | +Create or update multiple Github secrets sourced from literal values or files. |
| 53 | + |
| 54 | +Key/value pairs representing a secret name and the source of the secret value are provided via the flags --from-file and --from-literal. Depending on the key/value pairs specified a single invocation may carry out zero or more writes to the Github secrets of the repository. |
| 55 | + |
| 56 | +NOTE: An OAuth token **must** be provided via the 'GITHUB_TOKEN' environment variable, this is used to authenticate to the Github API. Access tokens require 'repo' scope for private repos and 'public_repo' scope for public repos. GitHub Apps must have the 'secrets' permission to use the API. Authenticated users must have collaborator access to a repository to create, update, or read secrets. |
| 57 | + |
| 58 | +Usage: |
| 59 | + github-secrets-writer --owner=owner --repo=repo [--from-literal=secretName1=secretValue1] [--from-file=secretName2=/path/to/secretValue2] |
| 60 | + |
| 61 | +Examples: |
| 62 | +# Write a single secret from a literal value |
| 63 | +github-secrets-writer --owner=owner --repo=repo --from-literal=secretName1=secretValue1 |
| 64 | + |
| 65 | +# Write a single secret from a file |
| 66 | +github-secrets-writer --owner=owner --repo=repo --from-file=secretName1=secretFilePath |
| 67 | + |
| 68 | +# Write multiple secrets, one from a literal value and one from a file |
| 69 | +github-secrets-writer --owner=owner --repo=repo --from-literal=secretName1=secretValue1 --from-file=secretName2=/path/to/secretValue2 |
| 70 | + |
| 71 | +Flags: |
| 72 | + --from-file stringArray specify secret name and literal value pairs e.g. secretname=somevalue (zero or more) |
| 73 | + --from-literal stringArray specify secret name and source file pairs e.g. secretname=somefile (zero or more) |
| 74 | + -h, --help help for github-secrets-writer |
| 75 | + --owner string owner of the repository e.g. an organisation or user (required) |
| 76 | + --repo string name of the repository (required) |
| 77 | +``` |
| 78 | + |
| 79 | +### Write secrets |
| 80 | + |
| 81 | +To write secrets to a repository you must invoke **github-secrets-writer** with the relevant flags. Below is an example of writing 3 secrets (2 from literal values and 1 from a source file) to the `example-owner/example-repo` repository. |
| 82 | + |
| 83 | +```console |
| 84 | +$ GITHUB_TOKEN=... github-secrets-writer \ |
| 85 | + --owner example-owner \ |
| 86 | + --repo example-repo \ |
| 87 | + --from-literal secretName1=secretValue1 \ |
| 88 | + --from-literal secretName2=secretValue2 \ |
| 89 | + --from-file secretName3=/path/to/secretValue3 |
| 90 | +Write results: |
| 91 | + |
| 92 | +secretName1: 204 No Content |
| 93 | +secretName2: 204 No Content |
| 94 | +secretName3: 204 No Content |
| 95 | +``` |
| 96 | + |
| 97 | +Following the successful writes, you can [use the encrypted secrets in a workflow](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#using-encrypted-secrets-in-a-workflow) as shown below. |
| 98 | + |
| 99 | +```yaml |
| 100 | +steps: |
| 101 | + - name: Hello world action |
| 102 | + with: # Set the secret as an input |
| 103 | + secretName1: ${{ secrets.secretName1 }} |
| 104 | + env: # Or as an environment variable |
| 105 | + secretName2: ${{ secrets.secretName2 }} |
| 106 | + secretName3: ${{ secrets.secretName3 }} |
| 107 | +``` |
0 commit comments