├── cookiecutter.json ├── {{cookiecutter.name}} └── README.md ├── README.md └── .github └── workflows └── setup-repository.yml /cookiecutter.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "choose_project_name" 3 | } -------------------------------------------------------------------------------- /{{cookiecutter.name}}/README.md: -------------------------------------------------------------------------------- 1 | # {{cookiecutter.name}} 2 | 3 | Repository Template generation powered by https://github.com/stefanbuck/cookiecutter-template. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Setup 2 | 3 | ### Create a repository from this template 4 | 5 | 1. Click the big green button `Use this template` or click here. 6 | 1. Enter a Repository name and click `Create repository from template` 7 | 1. Head over to the created repository and complete the setup. 8 | 9 | ### Complete setup 10 | 11 | 1. In the a new repository, complete the project setup by editing the `cookiecutter.json` file. 12 | 1. Hit cmd + S and then Enter to perform a commit (the commit message doesn't really matter). 13 | 1. Wait Setup Repository Action to complete. 14 | 1. That's it, easy isn't it? 15 | 16 | If you have anything to add, please reply in this [Twitter thread](). 17 | 18 | --- 19 | # What is this all about? 20 | 21 | On June 6, 2019, [GitHub introduced Repository Templates](https://github.blog/2019-06-06-generate-new-repositories-with-repository-templates/) giving users an easy way to share boilerplate for their projects. This feature is fantastic, but lacking adoption to my knowledge and opinion for one reason... [Read the full blog post](https://stefanbuck.com/blog/repository-templates-meets-github-actions) to learn how to template new projects using repository templates and GitHub Actions. -------------------------------------------------------------------------------- /.github/workflows/setup-repository.yml: -------------------------------------------------------------------------------- 1 | name: Setup repository 2 | on: 3 | push: 4 | paths: 5 | - cookiecutter.json 6 | jobs: 7 | setup: 8 | name: Reinitialize repository 9 | runs-on: ubuntu-latest 10 | env: 11 | REPO_SETUP_TOKEN: ${{ secrets.REPO_SETUP_TOKEN }} 12 | steps: 13 | - name: Do not run scaffolding on template repository 14 | shell: bash 15 | # This workflow runs when the `cookiecutter.json` file is modified. 16 | # This is the trick to re-init a repository, but we don't want to 17 | # run this action if this file is modified in the origin template repository. 18 | # 19 | # Using the GitHub rest API allows us to identify if the current repository 20 | # is a template repository or not. 21 | run: | 22 | curl --silent -X GET \ 23 | -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ 24 | -H "Accept: application/vnd.github.baptiste-preview+json" \ 25 | https://api.github.com/repos/$GITHUB_REPOSITORY \ 26 | | jq --exit-status '.is_template == false'; 27 | 28 | - uses: actions/checkout@v2 29 | with: 30 | # Comminting workflow files using the regular GITHUB_TOKEN will fail with 31 | # `Git Error: Refusing to allow a GitHub App to create or update workflow without workflows permission`. 32 | # This is by design to prevent third-parties from adding malicious workflow files. 33 | # 34 | # Generate a new personal access token with the workflow `scope` does the trick. 35 | # Checkout my blog post https://stefanbuck.com/blog for alternative options 36 | token: ${{ env.REPO_SETUP_TOKEN || secrets.GITHUB_TOKEN }} 37 | 38 | - uses: actions/setup-python@v2 39 | with: 40 | python-version: '3.x' 41 | 42 | - name: Install dependencies 43 | run: pip install cookiecutter 44 | 45 | - name: Scaffolding repository 46 | # cookiecutter is command-line utility to create projects from templates 47 | # https://github.com/cookiecutter/cookiecutter 48 | # 49 | # --no-input Do not prompt for parameters and only use 50 | # cookiecutter.json file content 51 | # 52 | # --output-dir Where to output the generated project dir into 53 | run: cookiecutter . --no-input --output-dir ./cookiecutter-temp 54 | 55 | - name: Prepare root directory 56 | shell: bash 57 | # Remove all files and folder exepct .git/ and cookiecutter-temp/ 58 | run: | 59 | find ./ -maxdepth 1 \ 60 | ! -name '.git' \ 61 | ! -name 'cookiecutter-temp' \ 62 | ! -name '.' \ 63 | ! -exec rm -rf {} + 64 | 65 | - name: Move files to root 66 | shell: bash 67 | # The cookiecutter-temp/ folder contains a single folder which is the 68 | # generated project by cookiecutter. We want to move all the project 69 | # files into the root directory so we can reinitialize git in the next step 70 | run: | 71 | rsync -r ./cookiecutter-temp/*/ . && \ 72 | rm -rf ./cookiecutter-temp/ 73 | 74 | - name: Reinitialize git repository 75 | shell: bash 76 | # Reinitialize git after scaffolding this repository. 77 | # We use `git checkout --orphan` to create a branch in a git init-like state. 78 | # By force pushing this as `main` we end up with a new clean git history. 79 | run: | 80 | git config --global user.email "github-actions[bot]@users.noreply.github.com" && \ 81 | git config --global user.name "github-actions[bot]" && \ 82 | git checkout --orphan temp-branch && \ 83 | git add . && \ 84 | git commit -m 'Initial commit' && \ 85 | git push origin temp-branch:main -f 86 | 87 | - name: Remove secret REPO_SETUP_TOKEN 88 | # After re-initializing the repository, we can remove the `REPO_SETUP_TOKEN` secret. 89 | shell: bash 90 | if: ${{ env.REPO_SETUP_TOKEN }} 91 | run: | 92 | curl \ 93 | -X DELETE --fail \ 94 | -H "Accept: application/vnd.github.v3+json" \ 95 | -H "Authorization: token ${{ env.REPO_SETUP_TOKEN }}" \ 96 | https://api.github.com/repos/$GITHUB_REPOSITORY/actions/secrets/REPO_SETUP_TOKEN --------------------------------------------------------------------------------