Skip to content

Commit db52c26

Browse files
pfebrerLuthaf
authored andcommitted
Improve handling of tokens in CI.
This commit fixes two problems: 1. We were hitting API rate limits when sending unauthenticated requests. 2. The builds for the unmodified examples during a PR were not possible to download if the PR was from a fork.
1 parent fea74ef commit db52c26

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

.github/workflows/docs.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
outputs:
1616
examplesmatrix: ${{ steps.gatherInfo.outputs.examplesjson }}
1717
latest_docs_run: ${{ steps.gatherInfo.outputs.latest_docs_run }}
18+
permissions: read-all # Permissions for the GITHUB_TOKEN
1819
steps:
1920
- uses: actions/checkout@v4
2021
with:
@@ -33,7 +34,7 @@ jobs:
3334
GET_EXAMPLES_ARGS=" --modified-files $(git diff --name-only -r HEAD^1 HEAD)"
3435
fi
3536
echo examplesjson=$(./src/get_examples.py ${GET_EXAMPLES_ARGS}) >> $GITHUB_OUTPUT
36-
echo latest_docs_run=$(./src/latest_docs_run.py id) >> $GITHUB_OUTPUT
37+
echo latest_docs_run=$(./src/latest_docs_run.py id --api-token ${{ secrets.GITHUB_TOKEN }}) >> $GITHUB_OUTPUT
3738
3839
generate-example:
3940
needs: setup
@@ -83,6 +84,8 @@ jobs:
8384
# Run this job even if the generate-example job was skipped due to
8485
# no examples to run. I.e. only avoid if generate-example failed.
8586
if: ${{ always() && needs.generate-example.result != 'failure' }}
87+
# Permissions for the GITHUB_TOKEN (does not affect GH_DEPLOY_TOKEN)
88+
permissions: read-all
8689
needs: [setup, generate-example]
8790
runs-on: ubuntu-latest
8891
steps:
@@ -102,7 +105,7 @@ jobs:
102105
with:
103106
path: docs/src/examples
104107
pattern: example-*
105-
github-token: ${{ secrets.GH_READ_TOKEN }}
108+
github-token: ${{ secrets.GITHUB_TOKEN }}
106109
run-id: ${{ needs.setup.outputs.latest_docs_run }}
107110
merge-multiple: true
108111

@@ -137,7 +140,7 @@ jobs:
137140
if: github.event_name == 'push'
138141
uses: peaceiris/actions-gh-pages@v4
139142
with:
140-
github_token: ${{ secrets.GITHUB_TOKEN }}
143+
github_token: ${{ secrets.GH_DEPLOY_TOKEN }}
141144
publish_dir: ./gh-pages/
142145
force_orphan: true
143146
cname: atomistic-cookbook.org

src/latest_docs_run.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
NIGHTLY_LINK = "https://nightly.link/lab-cosmo/atomistic-cookbook/workflows/docs/main"
2222

2323

24-
def get_latest_successful_docs_run():
24+
def get_latest_successful_docs_run(api_token: Optional[str] = None) -> int:
2525

2626
doc_runs_endpoint = GITHUB_ACTIONS_API + "/workflows/docs.yml/runs"
2727

@@ -32,6 +32,7 @@ def get_latest_successful_docs_run():
3232
"per_page": 1,
3333
"status": "success",
3434
"exclude_pull_requests": True,
35+
"auth": api_token,
3536
},
3637
)
3738

@@ -99,11 +100,19 @@ def download_latest_examples(
99100

100101
subparsers = parser.add_subparsers(dest="command")
101102

102-
subparsers.add_parser(
103+
id_parser = subparsers.add_parser(
103104
"id",
104105
help="Get the ID of the latest successful docs run",
105106
)
106107

108+
id_parser.add_argument(
109+
"--api-token",
110+
default=None,
111+
help="""Github API token to use for the API request.
112+
If not provided, the request will be unauthenticated, and there
113+
are rate limits for unauthenticated requests.""",
114+
)
115+
107116
download_parser = subparsers.add_parser(
108117
"download-examples",
109118
help=download_latest_examples.__doc__,
@@ -139,4 +148,4 @@ def download_latest_examples(
139148
exclude=args.exclude,
140149
)
141150
elif args.command == "id":
142-
print(get_latest_successful_docs_run())
151+
print(get_latest_successful_docs_run(api_token=args.api_token))

0 commit comments

Comments
 (0)