-
Notifications
You must be signed in to change notification settings - Fork 9
147 lines (128 loc) · 5.48 KB
/
deploy-instance-repo.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: Create PRs to the instance repo
on:
pull_request:
branches: [main]
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
env:
PYTHON_VERSION: "3.11"
INSTANCE_REPO_GITHUB: scverse/cookiecutter-scverse-instance
INSTANCE_REPO: "TEMPLATE_INSTANCE"
# directory in which cookiecuter generates the new repository
INSTANCE_GENERATED: "TEMPLATE_INSTANCE_GENERATED"
PROJECT_NAME: "cookiecutter-scverse-instance"
GH_TOKEN: ${{ secrets.BOT_GH_TOKEN }} # for gh cli
steps:
- name: Checkout template repository
uses: actions/checkout@v4
- name: Checkout instance repository
uses: actions/checkout@v4
with:
repository: ${{ env.INSTANCE_REPO_GITHUB }}
token: ${{ secrets.BOT_GH_TOKEN }}
path: ${{ env.INSTANCE_REPO }}
persist-credentials: true
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: define sister PR branch name for pull request
if: github.event_name == 'pull_request'
run: |
echo "SISTER_PR_BRANCH=pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
- name: define branch name for push event
if: github.event_name == 'push'
run: |
echo "SISTER_PR_BRANCH=main" >> $GITHUB_ENV
- name: Install cookiecutter
run: |
python -m pip install --upgrade pip wheel cookiecutter pre-commit
- name: Build from template
run: |
cookiecutter --output-dir $INSTANCE_GENERATED \
--replay-file .github/assets/cookiecutter-scverse-instance.json \
.
- name: Transfer changes to instance repo
run: |
# checkout branch or create if it does not exist
git fetch origin $SISTER_PR_BRANCH || true
git checkout $SISTER_PR_BRANCH 2>/dev/null || git checkout -b $SISTER_PR_BRANCH
# transfer changes from generated repo
rsync -va --delete --exclude .git $INSTANCE_GENERATED/$PROJECT_NAME/ $INSTANCE_REPO
# check if there is a commit to be made
if [[ `git status --porcelain` ]]; then
echo "GIT_HAS_CHANGES=TRUE" >> $GITHUB_ENV
else
echo "GIT_HAS_CHANGES=FALSE" >> $GITHUB_ENV
fi
env:
GIT_WORK_TREE: ${{ env.INSTANCE_REPO }}
GIT_DIR: ${{ env.INSTANCE_REPO}}/.git
- name: Commit changes
if: ${{ env.GIT_HAS_CHANGES == 'TRUE' }}
uses: EndBug/add-and-commit@v9
with:
default_author: github_actions
commit: "--no-verify" # no need to run pre-commit at this point - saves runtime!
cwd: ${{ env.INSTANCE_REPO }}
message: Update instance repo from cookiecutter template
new_branch: ${{ env.SISTER_PR_BRANCH }}
push: false
- name: Push to instance repo
if: ${{ env.GIT_HAS_CHANGES == 'TRUE' }}
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.BOT_GH_TOKEN }}
branch: ${{ env.SISTER_PR_BRANCH }}
repository: scverse/cookiecutter-scverse-instance
force: false
directory: ${{ env.INSTANCE_REPO }}
# Run the following steps only on the pull request event -- in the other case we just commited
# to the main branch and are done with it.
- name: Check if "sister PR" exists and find its ID
if: github.event_name == 'pull_request'
run: |
PR_ID=$( \
gh pr view $SISTER_PR_BRANCH \
-R $INSTANCE_REPO_GITHUB \
--json number --jq '.number' \
|| echo "NA"
)
echo "SISTER_PR_ID=$PR_ID" >> $GITHUB_ENV
- name: Create sister PR in instance repo
if: ${{ github.event_name == 'pull_request' && env.GIT_HAS_CHANGES == 'TRUE' && env.SISTER_PR_ID == 'NA' }}
run: |
cd $INSTANCE_REPO
gh pr create \
--base main \
--head $SISTER_PR_BRANCH \
--body "Created automatically from scverse/cookiecutter-scverse#${{ github.event.pull_request.number }}" \
--title "Update instance repo (PR ${{ github.event.pull_request.number }} -- ${{ github.event.pull_request.title }})" \
-R $INSTANCE_REPO_GITHUB
# now an ID should be available
PR_ID=$( \
gh pr view $SISTER_PR_BRANCH \
-R $INSTANCE_REPO_GITHUB \
--json number --jq '.number' \
)
echo "SISTER_PR_ID=$PR_ID" >> $GITHUB_ENV
- name: Add comment about sister PR
if: ${{ github.event_name == 'pull_request' && env.GIT_HAS_CHANGES == 'TRUE' }}
uses: mshick/add-pr-comment@v1
with:
message: |
A PR has been generated to the instance repo:
https://github.com/${{ env.INSTANCE_REPO_GITHUB }}/pull/${{ env.SISTER_PR_ID }}
You can check out the PR to preview your changes
in an instance of the cookiecutter template. The PR will be kept in sync with
this PR automatically.
repo-token: ${{ secrets.GITHUB_TOKEN }}
allow-repeats: false
- name: Query status of checks in template repository
if: ${{ github.event_name == 'pull_request' && env.SISTER_PR_ID != 'NA' }}
run: |
sleep 5 # without sleep, 0 checks are discovered.
gh pr checks ${{ env.SISTER_PR_ID }} --watch -R $INSTANCE_REPO_GITHUB