Search code examples
templatesgithubcookiecutter

Can you Integrate a Cookiecutter template with the Github Template repository feature?


At the moment I'm planning to write a python template for my company in order to have a consistent structure through all the different projects and channel some best practices.

To do so it seems a good idea to use the Cookiecutter method of generating templates on the one hand and the GitHub Template repository feature on the other hand. Now I'm curious if one can combine both methods, I have something like this in my mind:

  • The user creates a new GitHub repo within the website by using the GitHub Template repository feature
  • Then he clones it on his local machine
  • Then he can run Cookiecutter locally and populate the project with his details
  • Finally he just needs to push the nicely initialized template back to GitHub and overwrite the old defaults

Is it possible to use such a workflow? How would I do it?

Of course it would be even cooler if GitHub could integrate Cookiecutter in its GitHub Template repository feature; like asking you all the question within the website.


Solution

  • I had the same task. Following this post (that is for gitlab, for github is a bit different), this workflow worked for me:

    1. Create a completely empty repository in github. Write down the repository name (e.g. cookiecutter-test) and the repository link (e.g. https://github.com/nepentabits/cookiecutter-test.git)
    2. In your local machine, create the cookie project, with a slug name (the top directory of the project that you will populate) with the same name as the repo, for example (the template will be yours, and in my case the repo is cookiecutter-test)
    $ cookiecutter https://[email protected]/xx/ama-template-data-science
    $ ...
    $ ls
    $ cookiecutter-test
    
    1. Go inside the local package (in my case cookiecutter-test) and perform the git commands exactly in the same order. This is due to the fact that in most git local installations the branch is called master while in github they call it main, and for changing branch names in local, you have to have a commit first. Also, this works if the github repo is completely empty. Substitute
    $ git init 
    $ git add . 
    $ git commit -m "First commit with cookiecutter scaffold" 
    $ git branch -M main
    $ # the repo link should yours one
    $ git remote add origin https://github.com/nepentabits/cookiecutter-test.git
    $ git push -u origin main
    

    In the last step will ask you for your github name and https token.