Fastlane, CircleCI를 이용하여 React Native DevOps 도입기 (3)

1. React Native 프로젝트에서 Fastlane을 사용하기(Android 편)

android-circleci 이제 ios를 모두 구성하였으니, android를 구성해보자.

Android의 경우는 beta 배포로서 Fabric Beta도 가능하지만, 이 가이드에서는 간편함을 위해 AWS S3를 사용해본다.

DevOpsForRN/android/fastlane/Fastfile 경로에 다음과 같이 작성한다. 아래 내용은 Setup - fastlane docs 가이드를 따라 하면 동일하게 구성할 수 있다.

# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do
  desc "Runs all the tests"
  lane :test do
    gradle(task: "test", flags: "--no-daemon -x bundleReleaseJsAndAssets")
  end

  desc "Submit a new Beta Build to Crashlytics Beta"
  lane :beta do
    gradle(task: "assembleRelease", flags: "--no-daemon -x bundleReleaseJsAndAssets")
	  # crashlytics


    # sh "your_script.sh"
    # You can also use other beta testing services here
  end

  desc "Deploy a new version to the Google Play"
  lane :deploy do
    gradle(task: "assembleRelease")
    upload_to_play_store
  end
end

마찬가지로 DevOpsForRN/android/fastlane/Appfile 의 경로에도 다음과 같이 작성한다. 이 부분도 fastlane android 공식 가이드 문서에 따르면 동일하다.

json_key_file "" # Path to the json secret file - Follow https://docs.fastlane.tools/actions/supply/#setup to get one
package_name "com.devopsforrn" # e.g. com.krausefx.app

Android의 경우에는 release apk를 만드는데 Code Signing 필요하다. 다음의 링크를 참조하여 Code Signing을 진행한다. android - How to create a release signed apk file using Gradle? - Stack Overflow

2. Gradle 시간별로 빌드

추후에, 빌드된 apk가 S3에 쌓이는데, 모두 같은 이름이라면 덮어씌워 질 수 있으므로 Gradle이 빌드되었을때 apk의 시간대별로 생성되도록 하면 구별하는데 동무을 줄 수 있다. 이와같은 기능을 사용하려면 다음의 내용을 따라해보자.

DevOpsForRN/app/build.gradle 파일의 내용을 다음의 내용으로 수정한다.

...
...
applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }

            // 시간별로 빌드하는 부분을 추가
            def formattedDate = new Date().format('yyyy-MM-dd-HH-mm-ss')
            def newName = output.outputFile.name
            newName = newName.replace("-release", "-release" + formattedDate)
            output.outputFile = new File(output.outputFile.parent, newName)

        }
    }
...
...

위와같이 작성하면 ./gradlew assembleRelease 와 같은 커맨드를 입력했을때 app-release2018-02-15-12-26-58-signed.apk 의 파일을 얻을 수 있으므로 시간대별로 apk를 언제 빌드했는지 알 수 있다.

3. AWS의 devops-react-native Bucket 만들기

다음의 문서를 따라하여 devops-react-native라는 이름의 버킷을 만든다. https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/gsg/CreatingABucket.html

4. AWS CLI 설치후 aws s3 sync

기본적으로 CircleCI가 실행하는 Docker Image인 circleci/node:8 에는 awscli가 설치되어 있지 않다. 따라서 아래 내용 중 beta-deploy-android에 반드시 awscli를 설치하는 내용이 들어가야 한다.

DevOpsForRN/.circleci/config.yml

# .circleci/config.yml

# To Validate your config.yml then use this cli.
# `$ circleci config validate -c .circleci/config.yml`
version: 2
jobs:
  build:
    working_directory: ~/project
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - run: yarn
      - run: yarn run test
      - persist_to_workspace:
          root: ~/project
          paths:
            - node_modules
      - store_test_results:
          path: ~/project/junit.xml

  beta-deploy-ios:
    working_directory: ~/project/ios
    macos:
      xcode: "9.0"
    steps:
      - checkout:
          path: ~/project
      - attach_workspace:
          at: ~/project
      - run: bundle update fastlane
      - run: bundle exec fastlane beta

  beta-deploy-android:
      working_directory: ~/project/android
      docker:
        - image: circleci/android:api-27-node8-alpha
      steps:
        - checkout:
            path: ~/project
        - attach_workspace:
            at: ~/project
        - run: bundle update fastlane
        - run: bundle exec fastlane beta
        - run: sudo apt-get update && sudo apt-get install -y awscli # AWS CLI Install
        - run: aws s3 sync app/build/outputs s3://devops-react-native/

  android:
    working_directory: ~/project/android
    docker:
      - image: circleci/android:api-27-node8-alpha
    steps:
      - checkout:
          path: ~/project
      - attach_workspace:
          at: ~/project
      - run: bundle install --path vendor/bundle
      - persist_to_workspace:
          root: ~/project
          paths:
            - android
      - run: bundle exec fastlane test
      - store_test_results:
          path: ~/project/android/reports

  ios:
    macos:
      xcode: "9.0"
    working_directory: ~/project/ios
    environment:
      FL_OUTPUT_DIR: ~/project/output
    shell: /bin/bash --login -o pipefail
    steps:
      - checkout:
          path: ~/project
      - run:
          name: Set Ruby Version
          command:  echo "ruby-2.4" > ~/.ruby-version
      # Not using a workspace here as Node and Yarn versions
      # differ between the macOS image and the Docker containers above.
      - run: yarn
      - run: bundle install --path vendor/bundle
      - persist_to_workspace:
          root: ~/project
          paths:
            - ios
      - run: bundle exec fastlane test
      - store_artifacts:
          path: ~/project/output
      - store_test_results:
          path: ~/project/output/scan

workflows:
  version: 2
  node-android-ios:
    jobs:
      - build
      - android:
          requires:
            - build
      - beta-deploy-android:
          filters:
            branches:
              only:
                - beta
          requires:
            - android
      - ios:
          requires:
            - build
      - beta-deploy-ios:
          filters:
            branches:
              only:
                - beta
          requires:
            - ios

5. CircleCI Env Variable

  • AWS Permisson 설정 AWS CLI가 실행되기 위해서는 권한과 인증이 필요하다. AWS IAMAccess-KeySecret-Key를 아래와 같이 설정한다. aws-permission-config
  • AWS Region 설정 AWS CLI가 실행되기 위해서는 기본적으로 region이 설정되어 있어야 한다. AWS_DEFAULT_REGIONap-northeast-2로 설정이 필요하다. aws-region-config

6. Next


Philographer
Written by@Philographer
Fail Fast, Learn Faster

GitHublinkedInFacebook