Initial commit #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Setup repository | |
on: | |
push: | |
paths: | |
- cookiecutter.json | |
jobs: | |
setup: | |
name: Reinitialize repository | |
runs-on: ubuntu-latest | |
env: | |
REPO_SETUP_TOKEN: ${{ secrets.REPO_SETUP_TOKEN }} | |
steps: | |
- name: Do not run scaffolding on template repository | |
shell: bash | |
# This workflow runs when the `cookiecutter.json` file is modified. | |
# This is the trick to re-init a repository, but we don't want to | |
# run this action if this file is modified in the origin template repository. | |
# | |
# Using the GitHub rest API allows us to identify if the current repository | |
# is a template repository or not. | |
run: | | |
curl --silent -X GET \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.baptiste-preview+json" \ | |
https://api.github.com/repos/$GITHUB_REPOSITORY \ | |
| jq --exit-status '.is_template == false'; | |
- id: string | |
uses: ASzc/change-string-case-action@v2 | |
with: | |
string: ${{ github.repository }} | |
- uses: actions/checkout@v4 | |
with: | |
# Committing workflow files using the regular GITHUB_TOKEN will fail with | |
# `Git Error: Refusing to allow a GitHub App to create or update workflow without workflows permission`. | |
# This is by design to prevent third-parties from adding malicious workflow files. | |
# | |
# Generate a new personal access token with the workflow `scope` does the trick. | |
# Checkout my blog post https://stefanbuck.com/blog for alternative options | |
token: ${{ env.REPO_SETUP_TOKEN || secrets.GITHUB_TOKEN }} | |
- uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' | |
- name: Install dependencies | |
run: pip install cookiecutter | |
- name: Scaffolding repository | |
# cookiecutter is command-line utility to create projects from templates | |
# https://github.com/cookiecutter/cookiecutter | |
# | |
# --no-input Do not prompt for parameters and only use | |
# cookiecutter.json file content | |
# | |
# --output-dir Where to output the generated project dir into | |
run: cookiecutter . --no-input --output-dir ./cookiecutter-temp | |
- name: Prepare root directory | |
shell: bash | |
run: | | |
find ./ -maxdepth 1 \ | |
! -name '.git' \ | |
! -name 'cookiecutter-temp' \ | |
! -name '.' \ | |
! -exec rm -rf {} + | |
- name: Move files to root | |
shell: bash | |
# The cookiecutter-temp/ folder contains a single folder which is the | |
# generated project by cookiecutter. We want to move all the project | |
# files into the root directory so we can reinitialize git in the next step | |
run: | | |
rsync -r ./cookiecutter-temp/*/ . && \ | |
rm -rf ./cookiecutter-temp/ | |
- name: Install Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '1.20.x' | |
- name: go mod init | |
shell: bash | |
run: | | |
go mod init "github.com/${{ steps.string.outputs.lowercase }}" && \ | |
go mod tidy && \ | |
go mod vendor | |
- name: Reinitialize git repository | |
shell: bash | |
# Reinitialize git after scaffolding this repository. | |
# We use `git checkout --orphan` to create a branch in a git init-like state. | |
# By force pushing this as `main` we end up with a new clean git history. | |
run: | | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" && \ | |
git config --global user.name "github-actions[bot]" && \ | |
git checkout --orphan temp-branch && \ | |
git add . && \ | |
git commit -m 'Initial commit' && \ | |
git push origin temp-branch:main -f |