Trong phần này, chúng ta sẽ tạo một luồng CI/CD để triển khai ứng dụng lên ROSA cluster. Chúng ta sẽ tái sử dụng các thành phần ở phần trước (Cài đặt workflow CI/CD cơ bản) Kiến trúc của luồng như sau
Sau khi code được deploy lên CodeCommit, CodeBuild sẽ chạy và đăng nhập vào ROSA cluster thông qua các giá trị cluster credential được lấy về từ ASM. ROSA sẽ sử dụng S2I (source2image) để đọc mã nguồn từ CodeCommit, build image và đẩy lên local registry của OpenShift. DeploymentConfig của app được triển khai từ bước trước sẽ phát hiện thay đổi của image thông qua trigger ImageChange
và triển khai ứng dụng mới dựa trên image vừa được cập nhật.
Với CodeBuild, chúng ta có 2 cách tiếp cận. Thứ nhất, CodeBuild có thể build image và đẩy lên registry của OpenShift, từ đó Deployment tương ứng sẽ được kích hoạt và ứng dụng sẽ được triển khai. Thứ hai, CodeBuild có thể gọi đến webhook của OpenShift cluster, và OpenShift sẽ sử dụng S2I để pull mã nguồn từ CodeCommit, inject code và build trong 1 build container nằm trên OpenShift cluster. Image cuối cùng sẽ được đẩy lên local registry và 1 deployment mới sẽ được triển khai. Trong bài lab này, chúng ta sẽ sử dụng cách thứ 2.
Để OpenShift có thể giao tiếp với CodeCommit, chúng ta sẽ cần cấu hình CodeCommit secret, bao gồm username và password để truy cập CodeCommit.
Truy cập vào Red Hat OpenShift Service on AWS
Trong giao diện Secrects
Trong giao diện Create source secret
codecommitsecret
đối với Secret namekind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: vote
labels:
app: voting-app
spec:
strategy:
type: Docker
source:
type: Git
git:
uri: "https://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/rosa-voting-app"
ref: master
contextDir: voting-app/vote
sourceSecret:
name: codecommitsecret
output:
to:
kind: ImageStreamTag
name: vote:latest
Chỉnh sửa đường dẫn URI phù hợp. Sửa vote-buildconfig.yaml uri: openshift-voting-app -> rosa-voting-app
kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: vote
labels:
app: voting-app
spec:
triggers:
- type: Generic
generic:
secretReference:
name: rosa-voting-app-webhook-secret
key: WebHookSecretKey
strategy:
type: Docker
source:
type: Git
git:
uri: "https://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/rosa-voting-app"
ref: master
contextDir: voting-app/vote
sourceSecret:
name: codecommitsecret
output:
to:
kind: ImageStreamTag
name: vote:latest
oc apply -f openshift-specifications/with-dockerfile/vote-buildconfig.yaml
version: 0.2
phases:
pre_build:
commands:
- echo Hello World
- echo Testing codebuild flow
- apt-get install wget curl
- wget https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-client-linux.tar.gz
- tar xzf openshift-client-linux.tar.gz
- cp oc /usr/local/bin
build:
commands:
- echo Building the 3 apps...
- bash build-all.sh # in project root dir
post_build:
commands:
- echo Test successful!
- echo Another pipeline has been triggered
echo "Trigerring openshift webhook"
oclogin=`aws secretsmanager get-secret-value --secret-id ROSA/login-cmd --output=text | head -1 | cut -f4 -d'"'`
$oclogin
oc config set-context --current --namespace=tungch-voting-app
buildConfig="vote"
secret_name=`oc get bc $buildConfig -o jsonpath='{.spec.triggers[?(@.generic)].generic.secretReference.name}'`
webhookSecretKey=`oc get secret $secret_name -o jsonpath='{.data.WebHookSecretKey}' | base64 -d`
oc describe bc $buildConfig |grep webhooks | awk '{print $2}' | sed -e s/\<secret\>/$webhookSecretKey/
webhookURL=`oc describe bc $buildConfig |grep webhooks | awk '{print $2}' | sed -e s/\<secret\>/$webhookSecretKey/`
curl -kX POST $webhookURL
git add .
git commit -m "change buildspec"
git push