Skip to content

Commit 5f146bd

Browse files
committed
RHOAIENG-21668: chore(gha): implement the rhel building on pull_request_trigger workflow
1 parent 0c8af0e commit 5f146bd

File tree

10 files changed

+93
-72
lines changed

10 files changed

+93
-72
lines changed

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

+35
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,39 @@ jobs:
4954
echo "CACHE=${CACHE,,}" >>${GITHUB_ENV}
5055
5156
- uses: actions/checkout@v4
57+
if: ${{ fromJson(inputs.github).event_name != 'pull_request_target' }}
58+
59+
# we need to checkout the pr branch, not pr target (the default)
60+
# user access check is done in calling workflow
61+
- uses: actions/checkout@v4
62+
if: ${{ fromJson(inputs.github).event_name == 'pull_request_target' }}
63+
with:
64+
ref: "refs/pull/${{ fromJson(inputs.github).event.number }}/merge"
5265

5366
- run: mkdir -p $TMPDIR
5467

68+
# do this early because it's fast and why not
69+
- name: Unlock encrypted secrets with git-crypt
70+
if: ${{ inputs.subscription }}
71+
run: |
72+
sudo apt-get update
73+
sudo apt-get install git-crypt
74+
echo "${GIT_CRYPT_KEY}" | base64 --decode > ./git-crypt-key
75+
git-crypt unlock ./git-crypt-key
76+
rm ./git-crypt-key
77+
env:
78+
GIT_CRYPT_KEY: ${{ secrets.GIT_CRYPT_KEY }}
79+
80+
- name: Add subscriptions from GitHub secret
81+
if: ${{ inputs.subscription }}
82+
run: |
83+
sudo mkdir -p /etc/pki/
84+
sudo cp -R ${PWD}/ci/secrets/pki/* /etc/pki/
85+
printf "${PWD}/ci/secrets/pki/consumer:/etc/pki/consumer\n${PWD}/ci/secrets/pki/entitlement:/etc/pki/entitlement" | sudo tee /usr/share/containers/mounts.conf
86+
87+
mkdir -p $HOME/.config/containers/
88+
sudo cp ${PWD}/ci/secrets/pull-secret.txt $HOME/.config/containers/auth.json
89+
5590
# for bin/buildinputs in scripts/sandbox.py
5691
- uses: actions/setup-go@v5
5792
with:

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

+11-12
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

+3-1
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

+1
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

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ endef
107107
####################################### Build helpers #######################################
108108

109109
# https://stackoverflow.com/questions/78899903/how-to-create-a-make-target-which-is-an-implicit-dependency-for-all-other-target
110-
skip-init-for := all-images deploy% undeploy% test% scan-image-vulnerabilities
110+
skip-init-for := all-images deploy% undeploy% test% validate% refresh-pipfilelock-files scan-image-vulnerabilities
111111
ifneq (,$(filter-out $(skip-init-for),$(MAKECMDGOALS) $(.DEFAULT_GOAL)))
112112
$(SELF): bin/buildinputs
113113
endif

base/rhel9-python-3.11/Dockerfile

-42
This file was deleted.

ci/cached-builds/gen_gha_matrix_jobs.py

100644100755
+21-7
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.txt

1.17 KB
Binary file not shown.

rstudio/rhel9-python-3.11/Dockerfile.cpu

+7-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ LABEL name="odh-notebook-rstudio-server-rhel9-python-3.11" \
4848

4949
USER 0
5050

51+
# Check if we are already entitled
52+
RUN subscription-manager status || touch /var/tmp/.subscription-not-initially-present
53+
5154
# 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.
5255
#RUN sed -i 's/\(def in_container():\)/\1\n return False/g' /usr/lib64/python*/*-packages/rhsm/config.py
5356

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}) && \
57+
# If necessary, run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
58+
RUN [ -f '/var/tmp/.subscription-not-initially-present' ] && \
59+
SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
5660
BASEURL=$(cat ${SECRET_DIR}/BASEURL 2>/dev/null || echo ${BASEURL_DEFAULT}) && \
5761
USERNAME=$(cat ${SECRET_DIR}/USERNAME) && \
5862
PASSWORD=$(cat ${SECRET_DIR}/PASSWORD) && \
@@ -169,7 +173,7 @@ COPY ${RSTUDIO_SOURCE_CODE}/utils utils/
169173
COPY ${RSTUDIO_SOURCE_CODE}/run-rstudio.sh ${RSTUDIO_SOURCE_CODE}/setup_rstudio.py ${RSTUDIO_SOURCE_CODE}/rsession.sh ${RSTUDIO_SOURCE_CODE}/run-nginx.sh ./
170174

171175
# Unregister the system
172-
RUN subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
176+
RUN [ -f '/var/tmp/.subscription-not-initially-present' ] && subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
173177

174178
USER 1001
175179

rstudio/rhel9-python-3.11/Dockerfile.cuda

+14-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ 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+
# Check if we are already entitled
45+
RUN subscription-manager status || touch /var/tmp/.subscription-not-initially-present
46+
47+
# If necessary, run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
48+
RUN [ -f '/var/tmp/.subscription-not-initially-present' ] && \
49+
SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
4650
BASEURL=$(cat ${SECRET_DIR}/BASEURL 2>/dev/null || echo ${BASEURL_DEFAULT}) && \
4751
USERNAME=$(cat ${SECRET_DIR}/USERNAME) && \
4852
PASSWORD=$(cat ${SECRET_DIR}/PASSWORD) && \
@@ -165,7 +169,7 @@ RUN yum -y install cuda-toolkit-12-4 && \
165169
yum -y clean all --enablerepo="*"
166170

167171
# Unregister the system
168-
RUN subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
172+
RUN [ -f '/var/tmp/.subscription-not-initially-present' ] && subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
169173

170174
# Restore notebook user workspace
171175
USER 1001
@@ -194,11 +198,15 @@ LABEL name="odh-notebook-rstudio-server-rhel9-python-3.11" \
194198

195199
USER 0
196200

201+
# Check if we are already entitled
202+
RUN subscription-manager status || touch /var/tmp/.subscription-not-initially-present
203+
197204
# 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.
198205
#RUN sed -i 's/\(def in_container():\)/\1\n return False/g' /usr/lib64/python*/*-packages/rhsm/config.py
199206

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}) && \
207+
# If necessary, run the subscription manager command using the provided credentials. Only include --serverurl and --baseurl if they are provided
208+
RUN [ -f '/var/tmp/.subscription-not-initially-present' ] && \
209+
SERVERURL=$(cat ${SECRET_DIR}/SERVERURL 2>/dev/null || echo ${SERVERURL_DEFAULT}) && \
202210
BASEURL=$(cat ${SECRET_DIR}/BASEURL 2>/dev/null || echo ${BASEURL_DEFAULT}) && \
203211
USERNAME=$(cat ${SECRET_DIR}/USERNAME) && \
204212
PASSWORD=$(cat ${SECRET_DIR}/PASSWORD) && \
@@ -315,7 +323,7 @@ COPY ${RSTUDIO_SOURCE_CODE}/utils utils/
315323
COPY ${RSTUDIO_SOURCE_CODE}/run-rstudio.sh ${RSTUDIO_SOURCE_CODE}/setup_rstudio.py ${RSTUDIO_SOURCE_CODE}/rsession.sh ${RSTUDIO_SOURCE_CODE}/run-nginx.sh ./
316324

317325
# Unregister the system
318-
RUN subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
326+
RUN [ -f '/var/tmp/.subscription-not-initially-present' ] && subscription-manager remove --all && subscription-manager unregister && subscription-manager clean
319327

320328
USER 1001
321329

0 commit comments

Comments
 (0)