Back to home

SetupGit Ignored filesAdding the GitHub Access TokenCreating input.txtThe Node ScriptRunning the codeResources and Further Reading
Programatically create or update GitHub files main image

Programatically create or update GitHub files

This is a short example to create a new OUTPUT.md file in a GitHub repo programatically using Octokit.

We are going to use Node's fs module to read in a input.txt file and use that to write the contents to the repository.

Setup

We will run a simple setup for this application.

1 2 3 4 5 mkdir octokit-create-file-example cd octokit-create-file-example yarn init -y yarn add @octokit/rest dotenv js-base64 touch index.js input.txt .env .gitignore

We are using dotenv to set our access token variable and js-base64 to encode our file contents that we read in from input.txt.

Git Ignored files

Set your .gitignore to the following:

1 2 node_modules/ .env

This will ensure your Personal GitHub Access token won't be accidentally sent up into your repo.

Adding the GitHub Access Token

You'll need to ensure that you have create a Personal Access Token to follow along.

Once you have that, add it to your .env file:

1 GITHUB_ACCESS_TOKEN=<access-token-goes-here>

Creating input.txt

Add the following to the input.txt file. We will read this in with the Node script.

1 2 3 # Example This needs to be added to the GitHub repo using Octokit.

The Node Script

Add the following to the index.js file.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 const { Octokit } = require("@octokit/rest"); const { Base64 } = require("js-base64"); const fs = require("fs"); require("dotenv").config(); const octokit = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN, }); const main = async () => { try { const content = fs.readFileSync("./input.txt", "utf-8"); const contentEncoded = Base64.encode(content); const { data } = await octokit.repos.createOrUpdateFileContents({ // replace the owner and email with your own details owner: "your-github-account", repo: "octokit-create-file-example", path: "OUTPUT.md", message: "feat: Added OUTPUT.md programatically", content: contentEncoded, committer: { name: `Octokit Bot`, email: "your-email", }, author: { name: "Octokit Bot", email: "your-email", }, }); console.log(data); } catch (err) { console.error(err); } }; main();

We are using Node's fs module to first read in the input.txt file contents.

Secondly, we encode those contents to base64 using the js-base64 package we installed.

Finally, we then use Octokit's createOrUpdateFileContents function to create the file with that encoded content. You can read more about the required options on the documentation.

Running the code

First, we need to push up what we have to the remote repo.

1 2 3 4 git init git add --all git ci -m "feat: Initialise repo setup" git push

If you check the remote repo, it will not have the OUTPUT.md file (naturally).

We can now run node index.js locally to test our script!

Once you have a successful response, you can refresh your repo to ensure to confirm that OUTPUT.md is now there. Hooray!

File on GitHub

File on GitHub

We can now pull the repo using git pull to get the latest file into our local.

Local file after git pull

Local file after git pull

Now you can go forth and set things up to programmatically update your repositories!

Resources and Further Reading

  1. Octokit - GitHub
  2. Octokit documentation
  3. Octokit docuemntation - createOrUpdateFileContents
  4. Creating a Personal Access Token
  5. Completed GitHub project