Write repository name and username to disc? Fetch it with a funtion?

What I’m trying to do is get the name of the repository used in a Netlify deploy.

I have a GitHub repo which I offer through a ‘Deploy to netlify’ button. When someone uses it they are given the option to name the clone that Netlify creates for their GitHub repository.

my-repo/very-nice-name
becomes:
their-repo/something-they-choose

I am wondering if there is a way to use that new GitHub username and repo name that they create in the new repo.

Could a Netlify Function be included in the original repo which can allow me to build a link like this once it is cloned to a new user and location?

This way, no matter what the repo is named it can be referenced. Like this:

http://github.com/{their-repo}/{something-they-choose}

We always set some variables that are available during build:

You could, during build, read those variables and do “anything” with them - including make a function output their values (note that the variables aren’t available directly in functions - you’d have to interpolate the values during build). But, you could also create a static file with that information to keep things simpler, instead, since you’re already interpolating values programatically :slight_smile:

This article talks a bit more about “how to use variables during build”: [Common Issue] Using environment variables on Netlify correctly

1 Like

Thank you for the reply. It got me thinking and I was able to come up with something that will work.

I made a netlify.toml file with the following:

[build]  
  # build command adds environmental variables to a yml file on build
  command = 'printf "repo: %s" "$REPOSITORY_URL" > _data/netlify_build_env_vars.yml; jekyll build'

This creates a file I can read from in addition to doing the build:

I had to put this _data/netlify_build_env_vars.yml in place before it would work. (The command itself created the file for me when testing locally.)

Anyway, It works!

This is a Jekyll project for anyone interested. I thought I should mention that, as the jekyll-netlify Gem is no longer up to date and this solution may be helpful.

1 Like