Skip to content

Commit d6bc84f

Browse files
authored
Workflows to generate and commit protos (viamrobotics#124)
1 parent fa69771 commit d6bc84f

File tree

3 files changed

+130
-7
lines changed

3 files changed

+130
-7
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Check Requirements for Merge
2+
3+
on:
4+
pull_request_target:
5+
branches:
6+
- main
7+
types:
8+
- labeled
9+
- unlabeled
10+
11+
jobs:
12+
check_requirements:
13+
name: Check Merge Requirements
14+
runs-on: [self-hosted, x64]
15+
if: github.repository_owner == 'viamrobotics'
16+
steps:
17+
- name: Check Labels
18+
if: >
19+
contains(github.event.pull_request.labels.*.name, 'protos-compiled')
20+
run: echo "label-checks-pass=true" >> $GITHUB_ENV
21+
22+
- name: Final Checks
23+
if: >
24+
env.label-checks-pass == false
25+
uses: actions/github-script@v6
26+
with:
27+
script: core.setFailed('Merge requirements not met.\n\tLabel checks - ${{ env.label-checks-pass }}')

.github/workflows/compile_protos.yml

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,111 @@ concurrency:
55
cancel-in-progress: true
66

77
on:
8-
pull_request_review:
9-
branches: [ 'main' ]
10-
types: [submitted]
8+
workflow_run:
9+
workflows: ["On PR Approved"]
10+
types:
11+
- completed
1112

1213
jobs:
1314
compile-protos:
14-
if: github.event.review.state == 'approved' && github.repository_owner == 'viamrobotics'
15+
if: >
16+
github.event.workflow_run.event == 'pull_request_review' &&
17+
github.event.workflow_run.conclusion == 'success'
1518
runs-on: [self-hosted, x64]
1619
container:
1720
image: ghcr.io/viamrobotics/canon:amd64-cache
1821
options: --platform linux/amd64
22+
env:
23+
CI_COMMIT_MESSAGE_PREFIX: Built new protos from
24+
CI_COMMIT_AUTHOR: github-actions
25+
LABEL_NAME: protos-compiled
1926
steps:
27+
# Download PR info from approval workflow and checkout branch
28+
- name: Download artifact
29+
uses: dawidd6/action-download-artifact@v2
30+
with:
31+
run_id: ${{github.event.workflow_run.id }}
32+
- name: Get PR Repo
33+
id: get-pr-repo
34+
uses: actions/github-script@v6
35+
with:
36+
result-encoding: string
37+
script: |
38+
var fs = require('fs');
39+
var repo = String(fs.readFileSync('./pr/repo')).trim();
40+
return repo
41+
- name: Get PR Ref
42+
id: get-pr-ref
43+
uses: actions/github-script@v6
44+
with:
45+
result-encoding: string
46+
script: |
47+
var fs = require('fs');
48+
var ref = String(fs.readFileSync('./pr/ref')).trim();
49+
return ref
50+
- name: Get PR Number
51+
id: get-pr-number
52+
uses: actions/github-script@v6
53+
with:
54+
script: |
55+
var fs = require('fs');
56+
var number = Number(String(fs.readFileSync('./pr/number')).trim());
57+
return number
2058
- uses: actions/checkout@v3
2159
with:
22-
repository: ${{ github.event.pull_request.head.repo.full_name }}
23-
ref: ${{ github.event.pull_request.head.ref }}
60+
repository: ${{ steps.get-pr-repo.outputs.result }}
61+
ref: ${{ steps.get-pr-ref.outputs.result }}
2462
token: ${{ secrets.REPO_READ_TOKEN }}
63+
64+
# Set environment variables based on the last commit
65+
- name: Set environment variable "commit-message"
66+
run: echo "commit-message=$(git log -1 --pretty=format:'%s')" >> $GITHUB_ENV
67+
- name: Set environment variable "commit-author"
68+
run: echo "commit-author=$(git log -1 --pretty=format:'%an')" >> $GITHUB_ENV
69+
70+
# If the last commit is an auto-generated commit from this workflow, we can exit early
71+
- name: Set environment variable "is-auto-commit"
72+
if: startsWith(env.commit-message, env.CI_COMMIT_MESSAGE_PREFIX) && env.commit-author == env.CI_COMMIT_AUTHOR
73+
run: echo "is-auto-commit=true" >> $GITHUB_ENV
74+
75+
# Remove label if exists, since we're about to compile again
76+
- name: Remove label
77+
if: env.is-auto-commit == false
78+
uses: andymckay/[email protected]
79+
with:
80+
repo-token: ${{ secrets.REPO_READ_TOKEN }}
81+
remove-labels: ${{ env.LABEL_NAME }}
82+
issue-number: ${{ steps.get-pr-number.outputs.result }}
83+
84+
# Build and push
2585
- uses: bufbuild/buf-setup-action@v1
86+
if: env.is-auto-commit == false
2687
with:
2788
github_token: ${{ secrets.GITHUB_TOKEN }}
2889
- uses: arduino/setup-protoc@v1
90+
if: env.is-auto-commit == false
2991

3092
- name: Compile protos
93+
if: env.is-auto-commit == false
3194
run: make dist/buf
3295

3396
- name: Add SHORT_SHA env property
97+
if: env.is-auto-commit == false
3498
id: short_sha
3599
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
36100

37101
- name: Commit + Push
102+
if: env.is-auto-commit == false
38103
uses: EndBug/[email protected]
39104
with:
40105
default_author: github_actions
41-
message: Built new protos from ${{ steps.short_sha.outputs.short_sha }} [skip ci]
106+
message: ${{ env.CI_COMMIT_MESSAGE_PREFIX }} ${{ steps.short_sha.outputs.short_sha }} [skip ci]
42107
push: true
108+
109+
- name: Add label
110+
if: env.is-auto-commit == false
111+
uses: andymckay/[email protected]
112+
with:
113+
repo-token: ${{ secrets.REPO_READ_TOKEN }}
114+
add-labels: ${{ env.LABEL_NAME }}
115+
issue-number: ${{ steps.get-pr-number.outputs.result }}

.github/workflows/on_pr_approved.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: On PR Approved
2+
3+
on:
4+
pull_request_review:
5+
branches: [ 'main' ]
6+
types: [submitted]
7+
8+
jobs:
9+
export_pr_info:
10+
name: Export Pull Request Info
11+
runs-on: [self-hosted, x64]
12+
if: github.event.review.state == 'approved' && github.repository_owner == 'viamrobotics'
13+
steps:
14+
- name: Save PR repo and ref
15+
run: |
16+
mkdir -p ./pr
17+
echo ${{ github.event.pull_request.head.repo.full_name }} > ./pr/repo
18+
echo ${{ github.event.pull_request.head.ref }} > ./pr/ref
19+
echo ${{ github.event.pull_request.number }} > ./pr/number
20+
- uses: actions/upload-artifact@v2
21+
with:
22+
name: pr
23+
path: pr/

0 commit comments

Comments
 (0)