Skip to content

Commit 9b8fe09

Browse files
committed
add description next part
1 parent 9d270a7 commit 9b8fe09

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

docs/blog/posts/pinning_dependecies.md

+156
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,159 @@ Here is a list of our needs:
4848
## Our solution
4949

5050
Here, I will describe our solution to the problem. Today's date is 2023.11.7. Workflow may change over time. The most recent version of the workflow is available [here](https://github.com/napari/napari/blob/main/.github/workflows/upgrade_test_constraints.yml).
51+
52+
### Tigers
53+
54+
```yaml
55+
on:
56+
workflow_dispatch: # Allow running on-demand
57+
schedule:
58+
# Runs every Monday at 8:00 UTC (4:00 Eastern)
59+
- cron: '0 8 * * 1'
60+
61+
issue_comment:
62+
types: [ created ]
63+
64+
pull_request:
65+
paths:
66+
- '.github/workflows/upgrade_test_constraints.yml'
67+
```
68+
69+
We have 4 triggers:
70+
71+
1. `workflow_dispatch` - for the manual trigger of update or create PR with constraints upgrade.
72+
2. `schedule` - for automatic weekly update of constraints.
73+
3. `issue_comment` - for the manual trigger of constraints upgrade from comment. This generates new constraints files that needs to be manually applied, because of the lack of permissions to push to the different repositories.
74+
4. `pull_request` - for development purposes.
75+
76+
77+
### Setup action
78+
79+
```yaml
80+
jobs:
81+
upgrade:
82+
permissions:
83+
pull-requests: write
84+
issues: write
85+
name: Upgrade & Open Pull Request
86+
if: (github.event.issue.pull_request != '' && contains(github.event.comment.body, '@napari-bot update constraints')) || github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' || github.event_name == 'pull_request'
87+
runs-on: ubuntu-latest
88+
```
89+
90+
To be able to open PR from the workflow, we need to add `pull-request: write` and `issues: write` permissions to the workflow.
91+
To run a workflow only on comments containing `@napari-bot update constraints` we use `contains(github.event.comment.body, '@napari-bot update constraints')` condition.
92+
The rest of the conditions are to allow starting workflow from triggers described above.
93+
94+
### Visual signaling that workflow is running
95+
96+
```yaml
97+
- name: Add eyes reaction
98+
# show that workflow has started
99+
if: github.event_name == 'issue_comment'
100+
run: |
101+
COMMENT_ID=${{ github.event.comment.id }}
102+
curl \
103+
-X POST \
104+
-H "Accept: application/vnd.github+json" \
105+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
106+
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID/reactions" \
107+
-d '{"content": "eyes"}'
108+
```
109+
110+
```yaml
111+
- name: Add rocket reaction
112+
# inform that new constraints are available in artifacts
113+
if: github.event_name == 'issue_comment'
114+
run: |
115+
COMMENT_ID=${{ github.event.comment.id }}
116+
curl \
117+
-X POST \
118+
-H "Accept: application/vnd.github+json" \
119+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
120+
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID/reactions" \
121+
-d '{"content": "rocket"}'
122+
```
123+
124+
To signal that the workflow is running, we add `eyes` reaction to the comment that triggered the workflow.
125+
When the workflow is finished, we add `rocket` reaction to the comment that triggered the workflow. You may remove it or replace with other mechanisms.
126+
127+
### Get repo details
128+
129+
```yaml
130+
131+
- name: Get PR details
132+
# extract PR number and branch name from issue_comment event
133+
if: github.event_name == 'issue_comment'
134+
run: |
135+
PR_number=${{ github.event.issue.number }}
136+
PR_data=$(curl \
137+
-H "Accept: application/vnd.github.v3+json" \
138+
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_number" \
139+
)
140+
141+
FULL_NAME=$(echo $PR_data | jq -r .head.repo.full_name)
142+
echo "FULL_NAME=$FULL_NAME" >> $GITHUB_ENV
143+
144+
BRANCH=$(echo $PR_data | jq -r .head.ref)
145+
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
146+
147+
env:
148+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
149+
150+
- name: Get repo info
151+
# when schedule or workflow_dispatch triggers workflow, then we need to get info about which branch to use
152+
if: github.event_name != 'issue_comment' && github.event_name != 'pull_request'
153+
run: |
154+
echo "FULL_NAME=${{ github.repository }}" >> $GITHUB_ENV
155+
echo "BRANCH=${{ github.ref_name }}" >> $GITHUB_ENV
156+
```
157+
158+
We use these two conditional steps to determine the repository name and branch name.
159+
Runtime determines this allows to use of the same workflow for different repositories and branches.
160+
This is useful when you want to test the workflow before applying it to the main repository,
161+
but may allow you to easier use it in your repository.
162+
163+
164+
### Checkout repository
165+
166+
```yaml
167+
- uses: actions/checkout@v4
168+
with:
169+
fetch-depth: 0
170+
171+
- name: Clone docs repo
172+
uses: actions/checkout@v4
173+
with:
174+
path: docs # place in a named directory
175+
repository: napari/docs
176+
177+
- name: Clone target repo (remote)
178+
uses: actions/checkout@v4
179+
if: github.event_name == 'issue_comment'
180+
with:
181+
path: napari_repo # place in a named directory
182+
repository: ${{ env.FULL_NAME }}
183+
ref: ${{ env.BRANCH }}
184+
token: ${{ secrets.GHA_TOKEN_BOT_REPO }}
185+
186+
187+
- name: Clone target repo (pull request)
188+
# we need separate step as passing empty token to actions/checkout@v4 will not work
189+
uses: actions/checkout@v4
190+
if: github.event_name == 'pull_request'
191+
with:
192+
path: napari_repo # place in a named directory
193+
194+
- name: Clone target repo (main)
195+
uses: actions/checkout@v4
196+
if: github.event_name != 'issue_comment' && github.event_name != 'pull_request'
197+
with:
198+
path: napari_repo # place in a named directory
199+
repository: ${{ env.FULL_NAME }}
200+
ref: ${{ env.BRANCH }}
201+
token: ${{ secrets.GHA_TOKEN_NAPARI_BOT_MAIN_REPO }}
202+
```
203+
204+
We use have docs in a separate repository. We also want to update the constraints for the docs build. If you have a single repository, you may skip the second step.
205+
206+
We use two copies of the target repository. The

0 commit comments

Comments
 (0)