Cài đặt workflow CI/CD để triển khai ứng dụng

Cài đặt workflow CI/CD để triển khai ứng dụng

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

ROSA

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.

Cấu hình CodeCommit secret

Để 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.

  1. Truy cập vào Red Hat OpenShift Service on AWS

    • Chọn Administrator
    • Chọn Developer

ROSA

  1. Trong giao diện Secrects

    • Chọn Create
    • Chọn Source secrect

ROSA

  1. Trong giao diện Create source secret

    • Nhập codecommitsecret đối với Secret name
    • Nhập UsernamePassword or token (nhập username và password của CodeCommit)
    • Chọn Create

ROSA

  1. Tiếp tục chọn Secrets
  • Chọn Create
  • Chọn Webhook secret

ROSA

  1. Điền thông tin và chọn Create*

ROSA

  1. Thêm thông tin secret vào config build yaml của app vote, thay đổi git url thành url của CodeCommit repository
kind: 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

ROSA

Tạo trigger cho webhook endpoint

  1. Thêm trigger vào BuildConfig của app vote để tạo một webhook endpoint. CodeBuild có thể sử dụng endpoint được tạo để trigger build event
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

ROSA

  1. Cập nhật BuildConfig
oc apply -f openshift-specifications/with-dockerfile/vote-buildconfig.yaml

ROSA

Cấu hình file buildspec

  1. Truy cập vào AWS Secrets Manager
  • Chọn Store a new secret

ROSA

  1. Trong AWS Secret Manager, tạo một giá trị mới chứa câu lệnh đăng nhập vào OpenShift cluster. Thông tin này sẽ được dùng trong build script
  • Chọn Next

ROSA

  1. Nhập Secret name và chọn Next

ROSA

  1. Hoàn thành tạo secret.

ROSA

  1. Thêm policy SecretsManagerReadWrite cho CodeBuild service role
  • Chọn Attach policies

ROSA

  1. Tìm và chọn SecretsManagerReadWrite

ROSA

  1. Hoàn thành gán policy SecretsManagerReadWrite cho CodeBuild service role

ROSA

  1. Cập nhật buildspec để trigger webhook mỗi khi có thay đổi code
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

ROSA

  1. File build-all.sh có nội dung như sau
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

ROSA

  1. Update code lên CodeCommit
git add .
git commit -m "change buildspec"
git push

ROSA

  1. Xem log để xem chi tiết quá trình chạy pipeline.

ROSA

  1. Hoàn thành chạy pipeline. Như vậy là flow CI/CD đã hoàn tất. Bạn có thể thay đổi nội dung của app vote và app sẽ được deploy tự động.

ROSA

  1. Ví dụ về việc thay đổi mầu nền trong file voting-app/vote/src/static/stylesheets:

ROSA

  1. Hoàn thành test pipeline.

ROSA

ROSA