React Native CodeBuild Setup

Add CICD Requirements to Repo

  • Add the given Dockerfile to the root of your repo.

Update Codebuild Project

  • In DevOps Console Open Codebuild Projects -> Open Project associated with your repo and branch.

  • Check/Enable Build based on custom image field, to use a custom Docker image as Codebuild Project.

  • Update the buildspec with the below content

version: 0.2

env:
  shell: bash

phases:
  pre_build:
    commands:
      - export CODE_DIR=`pwd`
      - export CONTAINER_DIR=/code
      - current_requirements_checksum="$(md5sum package.json yarn.lock Dockerfile)"
      - previous_requirements_checksum=$(cat /code/requirements_checksum.txt)
      - echo "$current_requirements_checksum"
      - echo "$previous_requirements_checksum"
      - export ANDROID_HOME="$HOME/Android/Sdk"
      - echo $SERVICE_ACCOUNT_CREDENTIALS_ENCODED | base64 -di >> $CODE_DIR/apps/crm-agent-mobile-app/android/app/service-account-credentials.json
      - cp -r  $CODE_DIR/. $CONTAINER_DIR
      - |
        if [[ "$current_requirements_checksum" = "$previous_requirements_checksum" ]];
          then
            echo "Checksum matched";
          else
            echo "Checksum didn't match";
            exit -1;
        fi
  build:
    commands:
      - CI_BUILD_NUMBER=$CODEBUILD_BUILD_NUMBER
      - cd $CONTAINER_DIR
      - echo "NX_BACKEND_REST_URL=$NX_BACKEND_REST_URL" >> apps/crm-agent-mobile-app/.env
      - echo "NX_BACKEND_GRAPHQL_URL=$NX_BACKEND_GRAPHQL_URL" >> apps/crm-agent-mobile-app/.env
      - nohup npx nx run crm-agent-mobile-app:start-cicd:prod &
      - cd apps/crm-agent-mobile-app/android
      - sleep 15
      # Build and Distribute the App
      - ./gradlew assembleProdRelease appDistributionUploadProdRelease
      - export APP_PATH_IN_S3=releases/$CODEBUILD_SOURCE_VERSION/app-prod-release.apk
      - aws s3 cp $CODE_DIR/apps/crm-agent-mobile-app/android/app/build/outputs/apk/prod/release/app-prod-release.apk s3://$BUCKET_NAME/$APP_PATH_IN_S3
      # Notify in iBCom with Apk URL
      - python3 $CODE_DIR/mobile_app_post_build_script.py
      - echo "Deployed $CI_BUILD_NUMBER"
  • Then save and approve the action to update Project in AWS.

Setup Custom Base Image Build Project

  • In DevOps Console navigate to DEPLOYMENT SETUP -> Codebuild Docker_Image Build and Push

  • Open Project associated with your repo and branch

  • for the Build base image choose UBUNTU_STANDARD_v5.

  • for Compute type choose LINUX_BUILD_GENERAL1_LARGE.

  • Set below environment variables (Update Values)

[{
		"name": "CODEARTIFACT_PYPI_REPO",
		"value": "pypi",
		"type": "PLAINTEXT"
	},
	{
		"name": "CODEARTIFACT_NPM_REPO",
		"value": "npm",
		"type": "PLAINTEXT"
	},
	{
		"name": "CODEARTIFACT_DOMAIN",
		"value": "ibuild-dev",
		"type": "PLAINTEXT"
	},
	{
		"name": "JFROG_NPM_AUTH",
		"value": "",
		"type": "PARAMETER_STORE"
	},
	{
		"name": "JFROG_NPM_PASSWORD",
		"value": "",
		"type": "PARAMETER_STORE"
	},
	{
		"name": "JFROG_NPM_EMAIL",
		"value": "",
		"type": "PLAINTEXT"
	},
	{
		"name": "JFROG_NPM_USERNAME",
		"value": "",
		"type": "PLAINTEXT"
	}
]
  • Set below buildspec in the Buildspec field

version: 0.2

env:
  shell: bash

phases:
  install:
    runtime-versions:
      python: 3.7

  pre_build:
    commands:
      - export AWS_ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -d ":" -f 5)
      - aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
      - docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${DOCKER_IMAGE_NAME_WITH_TAG} || true
      - echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

  build:
    commands:
      - docker build --cache-from ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${DOCKER_IMAGE_NAME_WITH_TAG} -t ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${DOCKER_IMAGE_NAME_WITH_TAG} . --build-arg JFROG_NPM_EMAIL=$JFROG_NPM_EMAIL --build-arg JFROG_NPM_AUTH=$JFROG_NPM_AUTH --build-arg JFROG_NPM_USERNAME=$JFROG_NPM_USERNAME --build-arg JFROG_NPM_PASSWORD=$JFROG_NPM_PASSWORD
      - docker push ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${DOCKER_IMAGE_NAME_WITH_TAG}
      - echo "Deployed $CI_BUILD_NUMBER"

Build the Base Image

In DevOps Console navigate to the Actions and approve the task with BUILD_DOCKER_IMAGE_AND_PUSH action_type

Then trigger the main build, it will use the base image built in the above build and run the commands on top of it.

Updating the Base Image

Whenever there are changes in packages (package.json or yarn.lock), you need to rebuild the image, to do this Navigate to your Project under CodeBuild Projects then select your project, choose Build Docker Image for Selected Projects action, and then approve the action.

Last updated