-
Notifications
You must be signed in to change notification settings - Fork 0
172 lines (166 loc) · 6.74 KB
/
Copy pathcodebuild.yml
File metadata and controls
172 lines (166 loc) · 6.74 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# AWS CodeBuild CI for CortexLM/cli (Linux x64 + arm64).
# Marker: CLI_CODEBUILD_CI_READY
#
# Assumes a dedicated IAM role via GitHub OIDC. No long-lived AWS keys.
# Role ARN and region come from repository *variables*, not secrets.
# See deploy/aws/codebuild/README.md for the one-time admin steps.
#
# Does not replace ci.yml / release.yml / publish-r2.yml / homebrew.yml /
# winget.yml / version-bump.yml / test-stability.yml.
name: CodeBuild CI
"on":
push:
branches: [main]
# Unprivileged wiring only. StartBuild is skipped for this event because
# the workflow file would come from the unapproved head.
pull_request:
branches: [main]
# Privileged StartBuild. Workflow YAML and OIDC come from main.
pull_request_target:
branches: [main]
workflow_dispatch:
concurrency:
group: codebuild-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
id-token: write
statuses: write
jobs:
wiring:
name: CodeBuild wiring
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
enabled: ${{ steps.gate.outputs.enabled }}
same_repo: ${{ steps.gate.outputs.same_repo }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
# pull_request must validate the PR tree (github.sha). pin the base
# only for pull_request_target, which must not execute unapproved code.
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.base.sha || github.sha }}
- uses: actions/setup-python@v7
with:
python-version: '3.12'
- run: python -m pip install -r scripts/readiness/requirements.txt
- name: Validate CodeBuild assets
run: python -B -m unittest discover -s scripts/readiness -p test_codebuild.py -v
- name: Decide whether StartBuild is configured
id: gate
env:
ROLE_ARN: ${{ vars.AWS_CODEBUILD_ROLE_ARN }}
PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
run: |
same_repo=false
if [ "$PR_HEAD_REPO" = "${{ github.repository }}" ]; then
same_repo=true
fi
echo "same_repo=$same_repo" >> "$GITHUB_OUTPUT"
if [ -z "$ROLE_ARN" ]; then
echo "enabled=false" >> "$GITHUB_OUTPUT"
echo "AWS_CODEBUILD_ROLE_ARN is unset. Skipping StartBuild. One-time IAM is in deploy/aws/codebuild/README.md"
exit 0
fi
case "$ROLE_ARN" in
arn:aws:iam::*:role/*) ;;
*)
echo "::error::AWS_CODEBUILD_ROLE_ARN must be an IAM role ARN (set a variable, do not commit it)"
exit 1
;;
esac
echo "enabled=true" >> "$GITHUB_OUTPUT"
codebuild:
name: ${{ matrix.context }}
needs: wiring
if: needs.wiring.outputs.enabled == 'true' && needs.wiring.outputs.same_repo == 'true' && github.event_name != 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 120
strategy:
fail-fast: false
matrix:
include:
- context: cortex-cli-gha-x64
project_var: AWS_CODEBUILD_PROJECT_X64
project_default: cortex-cli-gha-x64
pr_project_var: AWS_CODEBUILD_PROJECT_X64_PR
pr_project_default: cortex-cli-gha-x64-pr
- context: cortex-cli-gha-arm64
project_var: AWS_CODEBUILD_PROJECT_ARM64
project_default: cortex-cli-gha-arm64
pr_project_var: AWS_CODEBUILD_PROJECT_ARM64_PR
pr_project_default: cortex-cli-gha-arm64-pr
env:
STATUS_CONTEXT: ${{ matrix.context }}
QUALITY_BASE: ${{ github.event.pull_request.base.sha || github.event.before }}
CORTEX_GITHUB_REPOSITORY: ${{ github.repository }}
steps:
- name: Checkout trusted buildspec revision
uses: actions/checkout@v7
with:
persist-credentials: false
# pull_request_target already runs on the base. Pin the ref so a
# later step cannot silently check out the unapproved head.
ref: ${{ github.event.pull_request.base.sha || github.sha }}
- name: Resolve head SHA
id: rev
run: echo "sha=${{ github.event.pull_request.head.sha || github.sha }}" >> "$GITHUB_OUTPUT"
- name: Post pending status
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ steps.rev.outputs.sha }}
run: |
gh api "repos/${{ github.repository }}/statuses/${SHA}" \
--field state=pending \
--field context="${STATUS_CONTEXT}" \
--field description="AWS CodeBuild starting" \
--field target_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ vars.AWS_CODEBUILD_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION || 'us-east-1' }}
role-session-name: ${{ matrix.context }}
- name: Read trusted buildspec
id: spec
run: |
test -f deploy/aws/codebuild/buildspec-ci.yml
{
echo "yaml<<ENDOFFILE"
cat deploy/aws/codebuild/buildspec-ci.yml
echo "ENDOFFILE"
} >> "$GITHUB_OUTPUT"
- name: Run CodeBuild
uses: aws-actions/aws-codebuild-run-build@v1
with:
# Only push/workflow_dispatch on main use the S3-cached projects.
# pull_request_target always uses *-pr (logs-only, NO_CACHE).
project-name: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && (vars[matrix.project_var] || matrix.project_default) || (vars[matrix.pr_project_var] || matrix.pr_project_default) }}
disable-source-override: true
buildspec-override: ${{ steps.spec.outputs.yaml }}
env-vars-for-codebuild: |
QUALITY_BASE,
CORTEX_SOURCE_SHA,
CORTEX_GITHUB_REPOSITORY
env:
CORTEX_SOURCE_SHA: ${{ steps.rev.outputs.sha }}
- name: Post final status
if: always()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ steps.rev.outputs.sha }}
OUTCOME: ${{ job.status }}
run: |
if [ "$OUTCOME" = "success" ]; then
state=success
desc="AWS CodeBuild passed"
else
state=failure
desc="AWS CodeBuild failed"
fi
gh api "repos/${{ github.repository }}/statuses/${SHA}" \
--field state="$state" \
--field context="${STATUS_CONTEXT}" \
--field description="$desc" \
--field target_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"