Continuous integration - UniApp

Keywords: Android Docker GitLab Gradle

A basic APP build script. Please ignore this article. The author is not talented and can only try to sum up experience repeatedly.
FROM fake front end Engineer


The official development tool HBX (abbreviated as HbuilderX) of UniApp needs to manually export the project file and then operate the Android project.

Recall the scenario where the test chased me and the development always had to install the package (previously, Jenkins was used to continuously integrate the IOS platform project, but it was far from the point where the code submitted could automatically package and generate an APP)

Effect achieved

  1. Submit code and package (automatic pipeline operation)
  2. Support the configuration of packaged certificates in gitlab-ci.yml
  3. Support to define package name
  4. Support custom APP name

Process stage

According to the traditional thinking, namely:

  1. HbuilderX generates APP resource files
  2. Generate the original project replacement file according to the template
  3. Generate file replace native project file
  4. Android Studio pack

Other ideas are to use automatic packaging tools such as fastlane

Deployment example diagram

NPM cache optional

gitlab-ci.yml file display

    - build_uniapp_file
    - rewrite_android_file
    - replace_android_file
    - build_app

    BUILD_TYPE: app-plus # At present, this process can only package APP
    PackageName: com.harrycode.testbuild23232 # APP package name
    APPNAME: test # Name of software display
    CerPwd: 123456 # Certificate password for packaging
    CerAlias: app  # Alias for packaging certificate
    CerAliasPwd: 123456  # Package the password of certificate alias
    CerPath: ../../../cer/app.keystore # The location of the package certificate is not recommended to be modified. Change the location to the cer folder of the root directory of the submitted project

    key: ${CI_COMMIT_SHA}
        - build-dist/           # Uniapp app plus generation file
        - android-files/        # Generated Android files (partial)
        - android-temp/HBuilder-Hello/ # Android project file (manual operation after downloading)

Build source files:
    image: # The image is a simplified file of node+vue cli+ UniApp project template project (see DockerHub for details)
    stage: build_uniapp_file
        - opath=$(pwd)
        - cp -rf . /buildwork/src
        - cd /buildwork
        - rm src/package.json
        - mv src/build.package.json package.json
        # - set SASS_BINARY_SITE=https://NPM. Taobao. Org / mirrors / node sass / & & NPM install node sass
        - npm update && npm install &&  npm run build:$BUILD_TYPE
        - mkdir $opath/build-dist/ -p
        - cp -r /buildwork/dist/build/app-plus/* $opath/build-dist/
            - build-dist/

change files as wish:
    name: # Please refer to DockerHbu for details of the image generation tool for Android files (you need to configure more parameters and modify this file, then copy to the corresponding location in the next step)
  stage: rewrite_android_file
    - opath=$(pwd)
    - cd /app
    - dotnet UniAppBuildHelper.dll
    - \cp -r /app/dist/. $opath/android-files
          - android-files/  # Product of this stage, customized partial configuration file

change file in Android Project:
    image: busybox
    stage: replace_android_file # In this stage, replace the file generated by customization to enter the Android project
        - opath=$(pwd)
        - source_file_path=$opath/android-files
        - uni_source=$(pwd)/build-dist
        - mkdir android-temp && cd android-temp
        - wget
        - unzip
        - project_src=$(pwd)/HBuilder-Hello
        - \cp $source_file_path/AndroidManifest.xml $project_src/app/src/main
        - rm -rf $project_src/app/src/main/java/io
        # Replace package name with file
        - java_dir_path=$(echo "$PackageName"| sed "s/\./\//g")
        - mkdir $project_src/app/src/main/java/$java_dir_path -p
        - \cp $source_file_path/ $project_src/app/src/main/java/$java_dir_path
        - \cp $source_file_path/ $project_src/app/src/main/java/$java_dir_path
        # dcloud_control.xml
        - \cp $source_file_path/dcloud_control.xml $project_src/app/src/main/assets/data
        # build.gradle
        - \cp $source_file_path/build.gradle $project_src/app
        # strings.xml
        - \cp $source_file_path/strings.xml $project_src/app/src/main/res/values
        # uniapp files
        - rm -rf $project_src/app/src/main/assets/apps/*
        - mkdir  $project_src/app/src/main/assets/apps/$APPID/www -p
        - \cp -r $uni_source/. $project_src/app/src/main/assets/apps/$APPID/www
            - android-temp/HBuilder-Hello/ # The product of this stage is the Android project file after replacing the file

    stage: build_app
    before_script: # Please place the nginx static server on the intranet to speed up the download
        - wget
        - mkdir /opt/gradle -p
        - unzip -d /opt/gradle
        - export PATH=/opt/gradle/gradle-5.4.1/bin:$PATH
    script: # Packing
        - mkdir android-temp/HBuilder-Hello/app/build/outputs/apk/ -p
        - opath=$(pwd)
        - cd android-temp/HBuilder-Hello/
        - gradle wrapper
        - bash ./gradlew assembleRelease
        - \cp -r $opath/android-temp/HBuilder-Hello/app/build/outputs/apk/. $opath/outputs
        - rm -rf android-temp/
            - outputs/ # The product of this stage is the Release folder and its APP installation package


Submit directory structure

Note: build.package.json is the package.json generated by NPM version. If you need to use NPM, please add the dependency under dependencies and devdendencies synchronously when building the new required module. The cer folder in this directory is the certificate of packaged UniApp (the certificate path is related to the defined variable CerPath, and the path in this document is the cer in this directory of the submitted file).

Reference command

#Create container
docker run -d --name gitlab-runner-12.10.1 --restart always -v ~/gitlab-runner/config/:/etc/gitlab-runner/  -v /var/run/docker.sock:/var/run/docker.sock  gitlab/gitlab-runner:latest

#Register Runner after entering the container
sudo gitlab-runner register -n  --url=GitLb address --registration-token=GitlabRunnerToken Self access --executor=docker --description="HP-Mini-PC" --docker-image="docker:19.03.8" --docker-volumes /var/run/docker.sock:/var/run/docker.sock  --locked=false

Other matters

  1. The image used in this article will be added to DockerHub as soon as possible
  2. The Gitlab runner of this article is running in the Docker environment, and Gitlab is also running on Docker.
  3. In this paper, the gitlab runner container is implemented by mounting / var/run/docker.sock in the official document.
  4. This article has been verified by construction, with 182 builds during the writing period.
  5. Simple Android file generator source code

Posted by DeadEvil on Sun, 17 May 2020 21:30:44 -0700