Skip to content

Commit 874c04c

Browse files
authored
chore(ci): Update our CI to use a Github App for opening PRs. (#13249)
Because - We need to switch to using a GitHub app to push from CircleCI for any automated GitHub interactions This commit - Adds the setup to use a GitHub App. [Example PR on my fork](b4handjr#6) Fixes #13206
1 parent 085fdb6 commit 874c04c

File tree

2 files changed

+58
-35
lines changed

2 files changed

+58
-35
lines changed

.circleci/config.yml

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,29 @@ commands:
9494
command: |
9595
FILES=$(ls -1 << parameters.source >>/*.<< parameters.extension >> 2>/dev/null)
9696
gsutil cp -n $FILES << parameters.destination >>
97-
98-
97+
setup-github-bot:
98+
steps:
99+
- run:
100+
name: Install Dependencies
101+
command: |
102+
pip install PyJWT requests cryptography
103+
- gh/install
104+
- run:
105+
name: Generate token and setup git user
106+
command: |
107+
export GH_TOKEN=$(python .circleci/scripts/create_jwt.py)
108+
if [ -z "$GH_TOKEN" ]; then
109+
echo "Failed to generate GH_TOKEN"
110+
exit 1
111+
fi
112+
echo "export GH_TOKEN=\"${GH_TOKEN}\"" >> "$BASH_ENV"
113+
git config --global user.email "experimenter-ci-bot@mozilla.com"
114+
git config --global user.name "Experimenter CircleCI Bot"
115+
git remote set-url origin https://x-access-token:${GH_TOKEN}@github.com/b4handjr/experimenter.git
116+
gh auth setup-git
117+
gh auth status
118+
gh config set git_protocol https
119+
gh config set prompt disabled
99120
jobs:
100121
check_experimenter_x86_64:
101122
machine:
@@ -731,18 +752,8 @@ jobs:
731752
image: ubuntu-2204:2024.11.1
732753
docker_layer_caching: true
733754
steps:
734-
- add_ssh_keys:
735-
fingerprints:
736-
- "32:8e:72:0b:9a:a1:1c:b8:7e:90:e1:53:a3:73:68:47" # for git pushes from circleci, since relies on ssh
737755
- checkout
738-
- gh/setup:
739-
token: GH_EXTERNAL_CONFIG_TOKEN # for gh commands from circleci, since relies on user token, since por que no los dos?
740-
- run:
741-
name: Setup Git
742-
command: |
743-
git config --local user.name "dataops-ci-bot"
744-
git config --local user.email "dataops+ci-bot@mozilla.com"
745-
gh config set git_protocol https
756+
- setup-github-bot
746757
- run:
747758
name: Check for External Config Update
748759
command: |
@@ -775,18 +786,8 @@ jobs:
775786
image: ubuntu-2204:2024.11.1
776787
docker_layer_caching: true
777788
steps:
778-
- add_ssh_keys:
779-
fingerprints:
780-
- "32:8e:72:0b:9a:a1:1c:b8:7e:90:e1:53:a3:73:68:47" # for git pushes from circleci, since relies on ssh
781789
- checkout
782-
- gh/setup:
783-
token: GH_TOKEN # for gh commands from circleci, since relies on user token, since por que no los dos?
784-
- run:
785-
name: Setup Git
786-
command: |
787-
git config --local user.name "dataops-ci-bot"
788-
git config --local user.email "dataops+ci-bot@mozilla.com"
789-
gh config set git_protocol https
790+
- setup-github-bot
790791
- run:
791792
name: Check for Application Services update
792793
command: |
@@ -813,18 +814,8 @@ jobs:
813814
docker_layer_caching: true
814815
working_directory: ~/experimenter
815816
steps:
816-
- add_ssh_keys:
817-
fingerprints:
818-
- "32:8e:72:0b:9a:a1:1c:b8:7e:90:e1:53:a3:73:68:47" # for git pushes from circleci, since relies on ssh
819817
- checkout
820-
- gh/setup:
821-
token: GH_TOKEN # for gh commands from circleci, since relies on user token, since por que no los dos?
822-
- run:
823-
name: Setup Git
824-
command: |
825-
git config --local user.name "dataops-ci-bot"
826-
git config --local user.email "dataops+ci-bot@mozilla.com"
827-
gh config set git_protocol https
818+
- setup-github-bot
828819
- run:
829820
name: Check for external Update
830821
command: |

.circleci/scripts/create_jwt.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import time
5+
6+
import jwt
7+
import requests
8+
9+
10+
app_id = os.environ["GH_APP_ID"]
11+
installation_id = os.environ["GH_INSTALLATION_ID"]
12+
private_key = os.environ["GH_APP_PRIVATE_KEY"].replace("\\n", "\n")
13+
14+
now = int(time.time())
15+
payload = {
16+
"iat": now,
17+
"exp": now + 540,
18+
"iss": app_id
19+
}
20+
jwt_token = jwt.encode(payload, private_key, algorithm="RS256")
21+
22+
# Get installation token
23+
headers = {
24+
"Authorization": f"Bearer {jwt_token}",
25+
"Accept": "application/vnd.github+json"
26+
}
27+
response = requests.post(
28+
f"https://api.github.com/app/installations/{installation_id}/access_tokens",
29+
headers=headers
30+
)
31+
token = response.json()["token"]
32+
print(token)

0 commit comments

Comments
 (0)