In this section, we will create a CI/CD flow to deploy the application to the ROSA cluster. We will reuse the components in the previous section (Basic CI/CD Workflow Setup) The architecture of the flow is as follows
After the code is deployed to CodeCommit, CodeBuild will run and log into the ROSA cluster through the cluster credential values retrieved from ASM. ROSA will use S2I (source2image) to read the source code from CodeCommit, build the image, and push it to OpenShift’s local registry. The DeploymentConfig of the app deployed from the previous step will detect the change of the image through the ImageChange
trigger and deploy a new application based on the updated image.
With CodeBuild, we have two approaches. First, CodeBuild can build the image and push it to the OpenShift registry, from which the corresponding Deployment will be activated and the application will be deployed. Second, CodeBuild can call the webhook of the OpenShift cluster, and OpenShift will use S2I to pull the source code from CodeCommit, inject the code, and build in a build container located on the OpenShift cluster. The final image will be pushed to the local registry and a new deployment will be deployed. In this lab, we will use the second method.
In order for OpenShift to communicate with CodeCommit, we will need to configure the CodeCommit secret, including the username and password to access CodeCommit.
Access to Red Hat OpenShift Service on AWS
In the Secrets interface
In the Create source secret interface
codecommitsecret
for Secret nameContinue to select Secrets
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:
big:
kind: ImageStreamTag
name: vote:latest
kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: vote
labels:
app: voting-app
spec:
triggers:
- type: Generic
generics:
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:
big:
kind: ImageStreamTag
name: vote:latest
oc apply -f openshift-specifications/with-dockerfile/vote-buildconfig.yaml
Access AWS Secrets Manager
In AWS Secret Manager, create a new value containing the command to log into the OpenShift cluster. This information will be used in the build script
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'"'`
$ologin
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