Skip to content

Commit add32d3

Browse files
committed
first commit
0 parents  commit add32d3

16 files changed

Lines changed: 485 additions & 0 deletions

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#github PAT
2+
TOKEN=token-test
3+
DOKKU_DOMAIN=example.com
4+
DOKKU_HOST=127.0.0.1
5+
SSH_PRIVATE_KEY=test

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.env
2+
auto-linux
3+
auto
4+
*.swp
5+
__pycache__
6+
venv
7+
*.db

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
###This repo automatically creates a new repository with the following features in your own user github account:
2+
* release versions
3+
* code analytics
4+
* issue templates
5+
* dokku pr-previews and deployment.
6+
* repositor secrets
7+
* deletiion of the dokku pr-previews after merge.
8+
9+
10+
To start creating first create a token (PAT)</br>
11+
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
12+
13+
Then you will need to fill the .env.example with the required information. </br>
14+
15+
```
16+
python3 -m venv venv
17+
pip install -r requirements.txt
18+
cp .env.example .env
19+
export $(grep -v '^#' .env | xargs) #export all .env variables
20+
chmod +x auto-repo.sh
21+
chmod +x repo-key.sh
22+
```
23+
```
24+
./auto-repo.sh <repo-name> <github-owner>
25+
```
26+
27+
You will need a folder structure of
28+
```
29+
<repo-name>/src/Dockerfile
30+
```
31+
after the first commit is being pushed to enable the pr-preview and deploy workflows</br>
32+
33+
Enjoy your new repo!

auto-repo.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
REPO_NAME=$1
4+
GITHUB_OWNER=$2
5+
6+
set -x
7+
set -e
8+
#create repo inside a username
9+
curl \
10+
-X POST \
11+
-H "Accept: application/vnd.github+json" \
12+
-H "Authorization: token $TOKEN" \
13+
https://api.github.com/user/repos \
14+
-d '{"name":"'"$REPO_NAME"'","homepage":"'"https://github.com/$GITHUB_OWNER/$REPO_NAME"'","private":false,"has_issues":true,"has_projects":true,"has_wiki":true}'
15+
##getting the public key and KEY_ID
16+
KEY_ID=`./repo-key.sh $REPO_NAME $GITHUB_OWNER $TOKEN | grep "key_id" | cut -d '"' -f4`
17+
REPO_PUBLIC_KEY=`./repo-key.sh $REPO_NAME $GITHUB_OWNER $TOKEN | grep '"key"' | cut -d '"' -f4`
18+
19+
#SSH_PRIVATE_KEY=`cat ~/.ssh/id_rsa`
20+
21+
##getting the encrypted part out of the python script
22+
DOKKU_DOMAIN_ENCRYPTED=`python3 encrypt.py $DOKKU_DOMAIN $REPO_PUBLIC_KEY`
23+
DOKKU_HOST_ENCRYPTED=`python3 encrypt.py $DOKKU_HOST $REPO_PUBLIC_KEY`
24+
SSH_PRIVATE_KEY_ENCRYPTED=`python3 encrypt.py $SSH_PRIVATE_KEY $REPO_PUBLIC_KEY`
25+
26+
mkdir -p repositories/$REPO_NAME
27+
cp -r clone-repo-files/.github ./repositories/$REPO_NAME
28+
cp clone-repo-files/.autorc ./repositories/$REPO_NAME
29+
cp clone-repo-files/README.md ./repositories/$REPO_NAME
30+
#Getting inside the new github repo
31+
cd repositories/$REPO_NAME
32+
git init
33+
#Using template + sed to change the values
34+
sed -i "s/REPO_NAME/$REPO_NAME/g" .autorc
35+
sed -i "s/GITHUB_OWNER/$GITHUB_OWNER/g" .autorc
36+
sed -i "s/REPO_NAME/$REPO_NAME/g" .github/workflows/*
37+
sed -i "s/GITHUB_OWNER/$GITHUB_OWNER/g" .github/workflows/*
38+
39+
#commit
40+
git add .
41+
git commit -m "initial commit"
42+
git branch -M main
43+
git remote add origin git@github.com:$GITHUB_OWNER/$REPO_NAME.git || true
44+
45+
#create the secrets inside github repo
46+
curl \
47+
-X PUT \
48+
-H "Accept: application/vnd.github+json" \
49+
-H "Authorization: token $TOKEN" \
50+
https://api.github.com/repos/$GITHUB_OWNER/$REPO_NAME/actions/secrets/DOKKU_DOMAIN \
51+
-d '{"encrypted_value":"'"$DOKKU_DOMAIN_ENCRYPTED"'","key_id":"'"$KEY_ID"'"}'
52+
curl \
53+
-X PUT \
54+
-H "Accept: application/vnd.github+json" \
55+
-H "Authorization: token $TOKEN" \
56+
https://api.github.com/repos/$GITHUB_OWNER/$REPO_NAME/actions/secrets/DOKKU_HOST \
57+
-d '{"encrypted_value":"'"$DOKKU_HOST_ENCRYPTED"'","key_id":"'"$KEY_ID"'"}'
58+
curl \
59+
-X PUT \
60+
-H "Accept: application/vnd.github+json" \
61+
-H "Authorization: token $TOKEN" \
62+
https://api.github.com/repos/$GITHUB_OWNER/$REPO_NAME/actions/secrets/SSH_PRIVATE_KEY \
63+
-d '{"encrypted_value":"'"$SSH_PRIVATE_KEY_ENCRYPTED"'","key_id":"'"$KEY_ID"'"}'
64+
65+
#push to the repo
66+
git push -u origin main

clone-repo-files/.autorc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"plugins": [
3+
"git-tag",
4+
"all-contributors",
5+
"first-time-contributor",
6+
"released"
7+
],
8+
"owner": "GITHUB_OWNER",
9+
"repo": "REPO_NAME",
10+
"name": "GITHUB_OWNER",
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ main ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ main ]
20+
schedule:
21+
- cron: '34 8 * * 1'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'python' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v3
42+
43+
# Initializes the CodeQL tools for scanning.
44+
- name: Initialize CodeQL
45+
uses: github/codeql-action/init@v2
46+
with:
47+
languages: ${{ matrix.language }}
48+
# If you wish to specify custom queries, you can do so here or in a config file.
49+
# By default, queries listed here will override any specified in a config file.
50+
# Prefix the list here with "+" to use these queries and those in the config file.
51+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
52+
53+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54+
# If this step fails, then you should remove it and run the build manually (see below)
55+
- name: Autobuild
56+
uses: github/codeql-action/autobuild@v2
57+
58+
# ℹ️ Command-line programs to run using the OS shell.
59+
# 📚 https://git.io/JvXDl
60+
61+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62+
# and modify them (or add more) to build your code if your project
63+
# uses a compiled language
64+
65+
#- run: |
66+
# make bootstrap
67+
# make release
68+
69+
- name: Perform CodeQL Analysis
70+
uses: github/codeql-action/analyze@v2
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: delete merged pr preview
2+
on:
3+
pull_request:
4+
types: [closed]
5+
6+
jobs:
7+
Delete:
8+
environment: production
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: delete dokku app
12+
env:
13+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
14+
DOKKU_HOST: ${{ secrets.DOKKU_HOST }}
15+
run: |
16+
set -x
17+
mkdir -p ~/.ssh
18+
ssh-keyscan ${{ secrets.DOKKU_HOST }}>> ~/.ssh/known_hosts
19+
eval `ssh-agent -s`
20+
ssh-add - <<< "$SSH_PRIVATE_KEY"
21+
echo deleting dokku app ${{ github.head_ref }}
22+
ssh dokku@$DOKKU_HOST -C "dokku -- --force apps:destroy ${{ github.head_ref }}"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Deploy
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
Deploy:
10+
environment: production
11+
runs-on: ubuntu-latest
12+
steps:
13+
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
14+
- run: echo "🐧 This job is now running on a ${{ runner.os }} server."
15+
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
16+
- name: Check out repository code
17+
uses: actions/checkout@v3
18+
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
19+
- name: List files in the repository
20+
run: |
21+
ls ${{ github.workspace }}
22+
- name: Deploy
23+
run: |
24+
set -x
25+
mkdir -p ~/.ssh
26+
ssh-keyscan ${{ secrets.DOKKU_HOST }}>> ~/.ssh/known_hosts
27+
eval `ssh-agent -s`
28+
ssh-add - <<< "${{ secrets.SSH_PRIVATE_KEY}}"
29+
ssh dokku@${{ secrets.DOKKU_HOST }} -C dokku builder:set REPO_NAME build-dir src
30+
ssh dokku@${{ secrets.DOKKU_HOST }} -C "dokku builder-dockerfile:set REPO_NAME dockerfile-path Dockerfile"
31+
ssh dokku@${{ secrets.DOKKU_HOST }} -C "dokku git:sync --build minimalcd" https://github.com/GITHUB_OWNER/REPO_NAME.git main
32+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
# When a push is made to a branch, deploy an instance of the app using
3+
# that branch.
4+
# The deployed url will be <pr-number>-<app-name>.<domain>
5+
6+
name: PR Preview
7+
on:
8+
pull_request
9+
jobs:
10+
pr_preview:
11+
runs-on: ubuntu-20.04
12+
timeout-minutes: 60
13+
environment:
14+
name: Testing
15+
url: ${{ steps.set_subdomain.outputs.preview_url }}
16+
concurrency:
17+
group: ${{ github.ref }}
18+
cancel-in-progress: true
19+
steps:
20+
21+
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
22+
- run: echo "🐧 This job is now running on a ${{ runner.os }} server."
23+
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
24+
- name: Check out repository code
25+
uses: actions/checkout@v3
26+
with:
27+
fetch-depth: 0
28+
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
29+
- name: List files in the repository
30+
run: |
31+
ls ${{ github.workspace }}
32+
33+
34+
- name: Prepare runner with ssh keys
35+
env:
36+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
37+
DOKKU_HOST: ${{ secrets.DOKKU_HOST }}
38+
run: |
39+
set -x
40+
mkdir -p ~/.ssh
41+
eval `ssh-agent -s`
42+
ssh-add - <<< "$SSH_PRIVATE_KEY"
43+
ssh-keyscan $DOKKU_HOST >> ~/.ssh/known_hosts
44+
45+
- name: Set subdomain (ensure is lowercase for dokku)
46+
id: set_subdomain
47+
run: |
48+
set -x
49+
echo SUBDOMAIN=`echo "${{ github.head_ref }}" | tr '[:upper:]' '[:lower:]' | cut -c -60` >> $GITHUB_ENV
50+
echo "::set-output name=preview_url::http://${{ github.head_ref }}.${{ secrets.DOKKU_DOMAIN }}"
51+
52+
- name: Create dokku app for pr branch if dosent already exist using dokku apps:create
53+
env:
54+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
55+
DOKKU_HOST: ${{ secrets.DOKKU_HOST }}
56+
DOKKU_DOMAIN: ${{ secrets.DOKKU_DOMAIN }}
57+
run: |
58+
set -x
59+
echo The PR was raised by: ${{ github.event.pull_request.user.login }}
60+
eval `ssh-agent -s`
61+
ssh-add - <<< "$SSH_PRIVATE_KEY"
62+
ssh dokku@$DOKKU_HOST -C "dokku apps:unlock --force ${{ env.SUBDOMAIN }}" | true
63+
echo deleting dokku app ${{ github.head_ref }}
64+
ssh dokku@$DOKKU_HOST -C "dokku -- --force apps:destroy ${{ env.SUBDOMAIN }}" | true
65+
echo Creating dokku app ${{ github.head_ref }}
66+
ssh dokku@$DOKKU_HOST -C "dokku apps:create ${{ env.SUBDOMAIN }}" | true
67+
ssh dokku@$DOKKU_HOST -C dokku builder:set ${{ env.SUBDOMAIN }} build-dir src
68+
ssh dokku@$DOKKU_HOST -C "dokku builder-dockerfile:set ${{ env.SUBDOMAIN }} dockerfile-path Dockerfile"
69+
ssh dokku@$DOKKU_HOST -C "dokku git:initialize ${{ env.SUBDOMAIN }}"
70+
ssh dokku@$DOKKU_HOST -C "dokku git:set ${{ env.SUBDOMAIN }} deploy-branch ${{ github.head_ref }}"
71+
72+
- name: Deploy branch ${{ github.head_ref }} to dokku
73+
uses: idoberko2/dokku-deploy-github-action@v1
74+
with:
75+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
76+
dokku-host: ${{ secrets.DOKKU_HOST }}
77+
app-name: ${{ env.SUBDOMAIN }}
78+
git-push-flags: '--force'
79+
remote-branch: ${{ github.head_ref }}
80+
81+
- name: Click to see your PR web address
82+
env:
83+
DOKKU_DOMAIN: ${{ secrets.DOKKU_DOMAIN }}
84+
run: |
85+
echo Visit your pr here: ${{ steps.set_subdomain.outputs.preview_url }}
86+
87+
- name: 'Comment PR with web address of application live preview'
88+
env:
89+
DOKKU_DOMAIN: ${{ secrets.DOKKU_DOMAIN }}
90+
uses: actions/github-script@v3
91+
if: github.event_name == 'pull_request'
92+
with:
93+
script: |
94+
github.issues.createComment({
95+
issue_number: context.issue.number,
96+
owner: context.repo.owner,
97+
repo: context.repo.repo,
98+
body: "🙌 Live preview is here: ${{ steps.set_subdomain.outputs.preview_url }}"
99+
})
100+

0 commit comments

Comments
 (0)