Convenience scripts from command line to create new Repos and push up my development environment and access key secrets to be auto-created in new Github Repos so they can be used by GitHub actions.
Before using this script, ensure that you have the following:
- A GitHub personal access token with appropriate permissions to create repositories. You can create a token by following the instructions here.
- The
GIT_TOKEN
environment variable must be set with your GitHub personal access token.
To use this script, you need to have the following software installed:
curl
: Command-line tool for making HTTP requests. It is usually pre-installed on most Unix-based systems.git
: Version control system for managing repositories. Install it by following the instructions for your operating system.npm
and Node.js: Ensure that you have Node.js installed on your machine. You can download it from https://nodejs.org and follow the installation instructions specific to your operating system.
Before being able to script writing of repository secrets, you need to setup the node environment for the createSecret.js
script. This is later used to encrypt the secret values using a public key from the created Github Repo.
This script uses the libsodium
library for encryption. Follow the steps below to compile the script:
The dependencies are provided in the package.json
file in the root directory.
-
Check out the repository to your local machine using Git:
git clone https://github.com/npiper/npm-sodium.git
-
Navigate to the repository's root directory:
cd npm-sodium
-
Install the required dependencies by running the following command:
npm install
Note: The
createSecret.js
script uses thelibsodium
library for encryption, which ensures secure encryption of secrets. The encryption process is handled within the script, and the encrypted values will be used when setting up repository secrets.
- In the terminal, navigate to the directory where you want to initialize the local repository.
- Run the following command to initialize the repository:
git init
. - Add and commit the first local commit using the necessary
git add
andgit commit
commands.
- Clone this repository or copy the shell script to your local machine.
- Open a terminal and navigate to the directory containing the script.
- Make the script executable if needed:
chmod +x createNewRepo.sh
. - Run the script, providing the desired repository name as an argument:
./create-repo.sh <repository-name>
. - The script will create the repository and print the owner/repo name and the HTTPS format remote URL.
To create a new repository named my-new-repo
, run the following command:
./createNewRepo.sh my-new-repo
- In the terminal, navigate to the local repository directory.
- Run the following command to add the remote origin:
git remote add origin <repository-url>
, replacing<repository-url>
with the capturedRepo URL
from running the script. - Push the first commit to the remote origin using the following command:
git push -u origin master
.
Once your remote Repo is created, in order to create repository secrets for your newly created repository, you can use the provided shell script createRepoSecrets.sh
. This script encrypts and sets the necessary secrets in your repository for use in workflows. Follow the steps below:
Before creating repository secrets, make sure you have completed the following prerequisites:
- Ensure that you have executed the earlier
createNewRepo.sh
script successfully and have obtained the owner/repo name and the HTTPS format remote URL. - That you have a local environment variable with a GitHub Token value set that can add Repository secrets in the varaible
GIT_TOKEN
- An nodeJS envrionment that has been created so that the dependent encryption script
createSecret.js
can be executed by the Script - Set up the necessary environment variables required for creating secrets. These environment variables should contain the values you want to set as secrets. Make sure the values are accessible within your local environment.
- Open a terminal and navigate to the directory containing the
createRepoSecrets.sh
script. - Make the script executable if needed:
chmod +x createRepoSecrets.sh
. - Run the script, providing the owner/repo name as an argument:
./createRepoSecrets.sh <owner>/<repo>
. - The script will attempt to retrieve the repository's public key using the GitHub API.
- The script will encrypt the secret values provided in the script using the public key.
- Encrypted secrets will be created and set in the repository using the GitHub API.
Note: The provided script assumes you have a
createSecret.js
script or function that handles the encryption process. Make sure to have this script or function in the same directory ascreateRepoSecrets.sh
for successful execution.
Once the script completes successfully, the repository secrets will be available for use in your workflows.
To create repository secrets for the repository npiper/my-new-repo
, run the following command:
./createRepoSecrets.sh npiper/my-new-repo
This should be usable for your personal repos if you just replace the value npiper
with your own personal user in these scripts.
Need to create a Github token that has permissions to create / read repos and add secrets.
These are the local environment variables I have behind the defaults;
' GIT User Name
GIT_USER_NAME
'DockerHub User/Pass
DOCKER_USERNAME
DOCKER_PASSWORD
'AWS Signin details
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
https://docs.github.com/en/actions/security-guides/encrypted-secrets
https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#get-a-repository-public-key
OWNER_REPO="owner/examplerepo" # Replace with your repository path
API_URL="https://api.github.com/repos/$OWNER_REPO/actions/secrets"