Skip to content

Commit dd69b53

Browse files
committed
RHOAIENG-21668: chore(gha): implement the rhel building on pull_request_trigger workflow
1 parent 3085a3f commit dd69b53

File tree

8 files changed

+98
-33
lines changed

8 files changed

+98
-33
lines changed

.github/workflows/build-notebooks-TEMPLATE.yaml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ name: Build & Publish Notebook Servers (TEMPLATE)
1515
required: true
1616
description: "top workflow's `github`"
1717
type: string
18+
subscription:
19+
required: false
20+
default: false
21+
description: "add RHEL subscription from github secret"
22+
type: boolean
1823

1924
jobs:
2025
build:
@@ -49,9 +54,40 @@ jobs:
4954
echo "CACHE=${CACHE,,}" >>${GITHUB_ENV}
5055
5156
- uses: actions/checkout@v4
57+
if: ${{ fromJson(inputs.github).event_name != 'pull_request_target' }}
58+
# we need to checkout the pr branch, not pr target (the default for pull_request_target)
59+
# user access check is done in calling workflow
60+
- uses: actions/checkout@v4
61+
if: ${{ fromJson(inputs.github).event_name == 'pull_request_target' }}
62+
with:
63+
ref: "refs/pull/${{ fromJson(inputs.github).event.number }}/merge"
5264

5365
- run: mkdir -p $TMPDIR
5466

67+
# do this early because it's fast and why not
68+
- name: Unlock encrypted secrets with git-crypt
69+
if: ${{ inputs.subscription }}
70+
run: |
71+
sudo apt-get update
72+
sudo apt-get install git-crypt
73+
echo "${GIT_CRYPT_KEY}" | base64 --decode > ./git-crypt-key
74+
git-crypt unlock ./git-crypt-key
75+
rm ./git-crypt-key
76+
env:
77+
GIT_CRYPT_KEY: ${{ secrets.GIT_CRYPT_KEY }}
78+
79+
- name: Add subscriptions from GitHub secret
80+
if: ${{ inputs.subscription }}
81+
run: |
82+
sudo mkdir -p /etc/pki/
83+
sudo cp -R ${PWD}/ci/secrets/pki/* /etc/pki/
84+
# https://access.redhat.com/solutions/5870841
85+
# https://github.com/containers/common/issues/1735
86+
printf "${PWD}/ci/secrets/run/secrets/rhsm:/etc/rhsm\n${PWD}/ci/secrets/run/secrets/etc-pki-entitlement:/etc/pki/entitlement\n${PWD}/ci/secrets/pki/consumer:/etc/pki/consumer\n" | sudo tee /usr/share/containers/mounts.conf
87+
88+
mkdir -p $HOME/.config/containers/
89+
sudo cp ${PWD}/ci/secrets/pull-secret.json $HOME/.config/containers/auth.json
90+
5591
# for bin/buildinputs in scripts/sandbox.py
5692
- uses: actions/setup-go@v5
5793
with:
@@ -257,7 +293,8 @@ jobs:
257293
- name: "pull_request: make ${{ inputs.target }}"
258294
run: |
259295
make ${{ inputs.target }}
260-
if: "${{ fromJson(inputs.github).event_name == 'pull_request' }}"
296+
if: "${{ fromJson(inputs.github).event_name == 'pull_request' ||
297+
fromJson(inputs.github).event_name == 'pull_request_target' }}"
261298
env:
262299
IMAGE_TAG: "${{ steps.calculated_vars.outputs.IMAGE_TAG }}"
263300
CONTAINER_BUILD_CACHE_ARGS: "--cache-from ${{ env.CACHE }}"

.github/workflows/build-notebooks-pr-rhel.yaml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "Build Notebooks (pr, RHEL images)"
33
"on":
44
"pull_request_target":
5-
"types": ["opened", "synchronize", "reopened", "edited"]
65

76
# BEWARE: This GitHub Actions workflow runs on pull_request_target, meaning it has access to our secrets
87
# see https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-secrets
@@ -13,7 +12,7 @@ permissions:
1312
packages: read
1413

1514
env:
16-
#language=json
15+
# language=json
1716
contributors: |
1817
["atheo89", "andyatmiami", "caponetto", "daniellutz", "dibryant", "harshad16", "jesuino", "jiridanek", "jstourac", "paulovmr"]
1918
@@ -25,31 +24,31 @@ jobs:
2524
matrix: ${{ steps.gen.outputs.matrix }}
2625
has_jobs: ${{ steps.gen.outputs.has_jobs }}
2726
steps:
28-
- name: Check permissions (this must be done FIRST, for security, before we checkout)
27+
28+
- name: Check permissions and deny untrusted users (this must be done FIRST, for security, before we checkout)
2929
if: ${{ !contains(fromJSON(env.contributors), github.actor) }}
3030
run: |
3131
echo "GitHub user ${{ github.actor }} is not a registered project contributor, not allowed to run actions on RHEL!"
3232
exit 1
3333
34+
# Here we are checking out the pull request, so that we can build from the new code
35+
# We can do this because we already checked that the submitting user is a contributor
3436
- uses: actions/checkout@v4
37+
if: ${{ github.event_name == 'pull_request_target' }}
3538
with:
3639
ref: "refs/pull/${{ github.event.number }}/merge"
37-
38-
- name: Unlock encrypted secrets with git-crypt
39-
run: |
40-
echo "${GIT_CRYPT_KEY}" | base64 --decode > ./git-crypt-key
41-
git-crypt unlock ./git-crypt-key
42-
rm ./git-crypt-key
40+
- uses: actions/checkout@v4
41+
if: ${{ github.event_name != 'pull_request_target' }}
4342

4443
- name: Determine targets to build based on changed files
44+
if: ${{ github.event_name == 'pull_request_target' }}
4545
run: |
4646
set -x
4747
git fetch --no-tags origin 'pull/${{ github.event.pull_request.number }}/head:${{ github.event.pull_request.head.ref }}'
4848
git fetch --no-tags origin '+refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }}'
4949
python3 ci/cached-builds/gen_gha_matrix_jobs.py \
5050
--from-ref 'origin/${{ github.event.pull_request.base.ref }}' \
51-
--to-ref '${{ github.event.pull_request.head.ref }}'
52-
--only-rhel
51+
--to-ref '${{ github.event.pull_request.head.ref }}' \
5352
id: gen
5453
env:
5554
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -65,5 +64,5 @@ jobs:
6564
with:
6665
target: "${{ matrix.target }}"
6766
github: "${{ toJSON(github) }}"
68-
subscription: true
67+
subscription: "${{ matrix.subscription }}"
6968
secrets: inherit

.github/workflows/build-notebooks-pr.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ jobs:
3333
git fetch --no-tags origin '+refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }}'
3434
python3 ci/cached-builds/gen_gha_matrix_jobs.py \
3535
--from-ref 'origin/${{ github.event.pull_request.base.ref }}' \
36-
--to-ref '${{ github.event.pull_request.head.ref }}'
36+
--to-ref '${{ github.event.pull_request.head.ref }}' \
37+
--leave-out-rhel '${{ github.event_name == 'pull_request' }}'
3738
id: gen
3839
env:
3940
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -49,4 +50,5 @@ jobs:
4950
with:
5051
target: "${{ matrix.target }}"
5152
github: "${{ toJSON(github) }}"
53+
subscription: "${{ matrix.subscription }}"
5254
secrets: inherit

.github/workflows/build-notebooks-push.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ jobs:
3939
with:
4040
target: "${{ matrix.target }}"
4141
github: "${{ toJSON(github) }}"
42+
subscription: "${{ matrix.subscription }}"
4243
secrets: inherit

ci/cached-builds/gen_gha_matrix_jobs.py

100644100755
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
import argparse
24
import json
35
import logging
@@ -6,7 +8,6 @@
68
import os
79
import pathlib
810
import re
9-
import string
1011
import sys
1112
import unittest
1213

@@ -21,7 +22,7 @@
2122
project_dir = pathlib.Path(__file__).parent.parent.parent.absolute()
2223

2324

24-
def parse_makefile(target: str, makefile_dir: str) -> str:
25+
def parse_makefile(target: str, makefile_dir: pathlib.Path | str) -> str:
2526
# Check if the operating system is macOS
2627
if platform.system() == 'Darwin':
2728
make_command = 'gmake'
@@ -30,7 +31,9 @@ def parse_makefile(target: str, makefile_dir: str) -> str:
3031

3132
try:
3233
# Run the make (or gmake) command and capture the output
33-
result = subprocess.run([make_command, '-nps', target], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True, cwd=makefile_dir)
34+
result = subprocess.run([make_command, '-nps', target],
35+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,
36+
check=True, cwd=makefile_dir)
3437
except subprocess.CalledProcessError as e:
3538
# Handle errors if the make command fails
3639
print(f'{make_command} failed with return code: {e.returncode}:\n{e.stderr}', file=sys.stderr)
@@ -43,7 +46,7 @@ def parse_makefile(target: str, makefile_dir: str) -> str:
4346
return result.stdout
4447

4548

46-
def extract_image_targets(makefile_dir: str = os.getcwd()) -> list[str]:
49+
def extract_image_targets(makefile_dir: pathlib.Path | str = os.getcwd()) -> list[str]:
4750
makefile_all_target = 'all-images'
4851

4952
output = parse_makefile(target=makefile_all_target, makefile_dir=makefile_dir)
@@ -68,19 +71,30 @@ def main() -> None:
6871
help="Git ref of the base branch (to determine changed files)")
6972
argparser.add_argument("--to-ref", type=str, required=False,
7073
help="Git ref of the PR branch (to determine changed files)")
74+
argparser.add_argument("--leave-out-rhel", type=bool, required=False, default=False, action=argparse.BooleanOptionalAction,
75+
help="Does not output rhel-based images even when they have changed files")
7176
args = argparser.parse_args()
7277

73-
7478
targets = extract_image_targets()
7579

7680
if args.from_ref:
7781
logging.info(f"Skipping targets not modified in the PR")
7882
changed_files = gha_pr_changed_files.list_changed_files(args.from_ref, args.to_ref)
7983
targets = gha_pr_changed_files.filter_out_unchanged(targets, changed_files)
8084

85+
if args.leave_out_rhel:
86+
targets = [target for target in targets if "rhel" not in target]
87+
88+
# https://stackoverflow.com/questions/66025220/paired-values-in-github-actions-matrix
8189
output = [
82-
f"matrix={json.dumps({"target": targets}, separators=(',', ':'))}",
83-
f"has_jobs={json.dumps(len(targets) > 0, separators=(',', ':'))}"
90+
"matrix=" + json.dumps({
91+
"include": [
92+
{"target": target, "subscription": "rhel" in target} for target in targets
93+
],
94+
}, separators=(',', ':')),
95+
"has_jobs=" + json.dumps(
96+
len(targets) > 0, separators=(',', ':')
97+
),
8498
]
8599

86100
print("targets", targets)

ci/secrets/pull-secret.json

1.17 KB
Binary file not shown.

rstudio/rhel9-python-3.11/Dockerfile.cpu

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ USER 0
5151
# uncomment the below line if you fall on this error: subscription-manager is disabled when running inside a container. Please refer to your host system for subscription management.
5252
#RUN sed -i 's/\(def in_container():\)/\1\n return False/g' /usr/lib64/python*/*-packages/rhsm/config.py
5353

54-
# Run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
55-
RUN SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
54+
# If necessary, run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
55+
RUN if [ -d "${SECRET_DIR}" ]; then \
56+
SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
5657
BASEURL=$(cat ${SECRET_DIR}/BASEURL 2>/dev/null || echo ${BASEURL_DEFAULT}) && \
5758
USERNAME=$(cat ${SECRET_DIR}/USERNAME) && \
5859
PASSWORD=$(cat ${SECRET_DIR}/PASSWORD) && \
@@ -62,7 +63,8 @@ RUN SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFA
6263
--username=$USERNAME \
6364
--password=$PASSWORD \
6465
--force \
65-
--auto-attach
66+
--auto-attach; \
67+
fi
6668

6769
ENV R_VERSION=4.4.1
6870

@@ -169,7 +171,9 @@ COPY ${RSTUDIO_SOURCE_CODE}/utils utils/
169171
COPY ${RSTUDIO_SOURCE_CODE}/run-rstudio.sh ${RSTUDIO_SOURCE_CODE}/setup_rstudio.py ${RSTUDIO_SOURCE_CODE}/rsession.sh ${RSTUDIO_SOURCE_CODE}/run-nginx.sh ./
170172

171173
# Unregister the system
172-
RUN subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
174+
RUN if [ -d "${SECRET_DIR}" ]; then \
175+
subscription-manager remove --all && subscription-manager unregister && subscription-manager clean; \
176+
fi
173177

174178
USER 1001
175179

rstudio/rhel9-python-3.11/Dockerfile.cuda

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ ARG BASEURL_DEFAULT=""
4141
USER 0
4242
WORKDIR /opt/app-root/bin
4343

44-
# Run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
45-
RUN SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
44+
# If necessary, run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
45+
RUN if [ -d "${SECRET_DIR}" ]; then \
46+
SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
4647
BASEURL=$(cat ${SECRET_DIR}/BASEURL 2>/dev/null || echo ${BASEURL_DEFAULT}) && \
4748
USERNAME=$(cat ${SECRET_DIR}/USERNAME) && \
4849
PASSWORD=$(cat ${SECRET_DIR}/PASSWORD) && \
@@ -52,7 +53,8 @@ RUN SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFA
5253
--username=$USERNAME \
5354
--password=$PASSWORD \
5455
--force \
55-
--auto-attach
56+
--auto-attach; \
57+
fi
5658

5759
ENV NVARCH=x86_64
5860
ENV NVIDIA_REQUIRE_CUDA="cuda>=12.4 brand=tesla,driver>=470,driver<471 brand=unknown,driver>=470,driver<471 brand=nvidia,driver>=470,driver<471 brand=nvidiartx,driver>=470,driver<471 brand=geforce,driver>=470,driver<471 brand=geforcertx,driver>=470,driver<471 brand=quadro,driver>=470,driver<471 brand=quadrortx,driver>=470,driver<471 brand=titan,driver>=470,driver<471 brand=titanrtx,driver>=470,driver<471 brand=tesla,driver>=525,driver<526 brand=unknown,driver>=525,driver<526 brand=nvidia,driver>=525,driver<526 brand=nvidiartx,driver>=525,driver<526 brand=geforce,driver>=525,driver<526 brand=geforcertx,driver>=525,driver<526 brand=quadro,driver>=525,driver<526 brand=quadrortx,driver>=525,driver<526 brand=titan,driver>=525,driver<526 brand=titanrtx,driver>=525,driver<526 brand=tesla,driver>=535,driver<536 brand=unknown,driver>=535,driver<536 brand=nvidia,driver>=535,driver<536 brand=nvidiartx,driver>=535,driver<536 brand=geforce,driver>=535,driver<536 brand=geforcertx,driver>=535,driver<536 brand=quadro,driver>=535,driver<536 brand=quadrortx,driver>=535,driver<536 brand=titan,driver>=535,driver<536 brand=titanrtx,driver>=535,driver<536"
@@ -165,7 +167,9 @@ RUN yum -y install cuda-toolkit-12-4 && \
165167
yum -y clean all --enablerepo="*"
166168

167169
# Unregister the system
168-
RUN subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
170+
RUN if [ -d "${SECRET_DIR}" ]; then \
171+
subscription-manager remove --all && subscription-manager unregister && subscription-manager clean; \
172+
fi
169173

170174
# Restore notebook user workspace
171175
USER 1001
@@ -197,8 +201,9 @@ USER 0
197201
# uncomment the below line if you fall on this error: subscription-manager is disabled when running inside a container. Please refer to your host system for subscription management.
198202
#RUN sed -i 's/\(def in_container():\)/\1\n return False/g' /usr/lib64/python*/*-packages/rhsm/config.py
199203

200-
# Run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
201-
RUN SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
204+
# If necessary, run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
205+
RUN if [ -d "${SECRET_DIR}" ]; then \
206+
SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
202207
BASEURL=$(cat ${SECRET_DIR}/BASEURL 2>/dev/null || echo ${BASEURL_DEFAULT}) && \
203208
USERNAME=$(cat ${SECRET_DIR}/USERNAME) && \
204209
PASSWORD=$(cat ${SECRET_DIR}/PASSWORD) && \
@@ -208,7 +213,8 @@ RUN SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFA
208213
--username=$USERNAME \
209214
--password=$PASSWORD \
210215
--force \
211-
--auto-attach
216+
--auto-attach; \
217+
fi
212218

213219
ENV R_VERSION=4.4.1
214220

@@ -315,7 +321,9 @@ COPY ${RSTUDIO_SOURCE_CODE}/utils utils/
315321
COPY ${RSTUDIO_SOURCE_CODE}/run-rstudio.sh ${RSTUDIO_SOURCE_CODE}/setup_rstudio.py ${RSTUDIO_SOURCE_CODE}/rsession.sh ${RSTUDIO_SOURCE_CODE}/run-nginx.sh ./
316322

317323
# Unregister the system
318-
RUN subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
324+
RUN if [ -d "${SECRET_DIR}" ]; then \
325+
subscription-manager remove --all && subscription-manager unregister && subscription-manager clean; \
326+
fi
319327

320328
USER 1001
321329

0 commit comments

Comments
 (0)