4

I'm new to Gitlab CI and I'm trying to use cache in .gitlab-ci.yml file (on Android platform). The gitlab ci works well but everytime I'm pushing code into gitlab, the CI will download all the data to run it again (It's will take about 30 minutues for each run code pushed).

I'm creating the Gitlab CI with method 1 in this tutorial : http://www.greysonparrelli.com/post/setting-up-android-builds-in-gitlab-ci-using-shared-runners/

I'm trying with many solutions on StackOverflow and another results on Google but still cannot cache.

Here is my .gitlab-ci.yml file:

image: openjdk:8-jdk

variables:
  ANDROID_TARGET_SDK: "24"
  ANDROID_BUILD_TOOLS: "24.0.3"
  ANDROID_SDK_TOOLS: "24.4.1"
  MAVEN_OPTS: -Dmaven.repo.local=/cache/maven.repository

before_script:
  - export GRADLE_USER_HOME=/cache/.gradle
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.tgz https://dl.google.com/android/android-sdk_r${ANDROID_SDK_TOOLS}-linux.tgz
  - tar --extract --gzip --file=android-sdk.tgz
  - echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter android-${ANDROID_TARGET_SDK}
  - echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter platform-tools
  - echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter build-tools-${ANDROID_BUILD_TOOLS}
  - echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-android-m2repository
  - echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-google-google_play_services
  - echo y | android-sdk-linux/tools/android --silent update sdk --no-ui --all --filter extra-google-m2repository
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - chmod +x ./gradlew

cache:
  key: "$CI_BUILD_NAME/$CI_BUILD_REF_NAME"
  paths:
  - .gradle/
  - build/
  - .m2/
  - $HOME/.gradle/caches/
  untracked: true

build:
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

And my result:

Running with gitlab-ci-multi-runner 1.5.2 (76fdacd)
Using Docker executor with image openjdk:8-jdk ...
Pulling docker image openjdk:8-jdk ...
Running on runner-060bf058-project-212-concurrent-0 via gitlab-runner...
Fetching changes...
Removing .gradle/
Removing android-sdk-linux/
Removing android-sdk.tgz
Removing app/build/
Removing build/
HEAD is now at d9b483e text commit here
From my gitlab project here  master     -> origin/master
Checking out b18798df as master...
Checking cache for build/master...
$ export GRADLE_USER_HOME=/cache/.gradle
$ apt-get --quiet update --yes
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
Ign http://deb.debian.org jessie InRelease
Get:2 http://deb.debian.org jessie-updates InRelease [145 kB]
Get:3 http://security.debian.org jessie/updates/main amd64 Packages [402 kB]
Get:4 http://deb.debian.org jessie-backports InRelease [166 kB]
Get:5 http://deb.debian.org jessie Release.gpg [2373 B]
Get:6 http://deb.debian.org jessie-updates/main amd64 Packages [17.6 kB]
Get:7 http://deb.debian.org jessie Release [148 kB]
Get:8 http://deb.debian.org jessie-backports/main amd64 Packages [959 kB]
Get:9 http://deb.debian.org jessie/main amd64 Packages [9064 kB]
Fetched 11.0 MB in 22s (485 kB/s)
Reading package lists...
$ apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
Reading package lists...
Building dependency tree...
Reading state information...
unzip is already the newest version.
wget is already the newest version.
Suggested packages:
  ncompress tar-scripts
The following NEW packages will be installed:
  lib32gcc1 lib32stdc++6 lib32z1 libc6-i386
The following packages will be upgraded:
  tar

.....
.....
.....

BUILD SUCCESSFUL

Creating cache build/master...
.gradle/: found 9 matching files                   
build/: found 4 matching files                     
WARNING: .m2/: no matching files                   
WARNING: /root/.gradle/caches/: no matching files  
untracked: found 28180 files                       
Uploading artifacts...
app/build/outputs/: found 7 matching files         
Uploading artifacts to coordinator... ok            id=643 responseStatus=201 Created token=nGX6FDZD
Build succeeded

How can I do ? Thank you all

John Strood
  • 1,859
  • 3
  • 26
  • 39
bkit4u
  • 505
  • 2
  • 5
  • 18

1 Answers1

0
# This file is a template, and might need editing before it works on your project.
# Read more about this script on this blog post https://about.gitlab.com/2018/10/24/setting-up-gitlab-ci-for-android-projects/, by Jason Lenny
image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS: "4333796"

cache:
  untracked: false
  key: ${CI_PROJECT_ID}
  paths:
    - android-sdk-linux
    - .m2/
    - .gradle/
    - android-sdk.zip
    - /var/cache/apt/
    - android.keystore
    - keystore.properties

stages:
  - init
  - test
  - build

downloadAndroidSDK:
  stage: init
  script:
    - KS_PASS=$(date +%s | sha256sum | base64 | head -c 32 ; echo)
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - apt-get --quiet update --yes
    - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
    - wget --quiet --continue --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
    - unzip -o -d android-sdk-linux android-sdk.zip
    - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
    - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
    - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    # temporarily disable checking for EPIPE error and use yes to accept all licenses
    - set +o pipefail
    - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
    - set -o pipefail
    - rm -f $CI_PROJECT_DIR/android.keystore
    - echo y | keytool -genkeypair -keyalg RSA -ext KeyUsage:critical=digitalSignature -ext ExtendedkeyUsage:critical=codeSigning -dname "ou=Wdes, c=FR" -alias wdes -keypass $KS_PASS -keystore $CI_PROJECT_DIR/android.keystore -storepass $KS_PASS -validity 1 -deststoretype pkcs12
    - keytool -list -storepass $KS_PASS -v -keystore $CI_PROJECT_DIR/android.keystore
    - echo -e "release.key.alias=wdes\nrelease.key.password=$KS_PASS\nrelease.storeFile=$CI_PROJECT_DIR/android.keystore\nrelease.password=$KS_PASS\n\ndebug.key.alias=wdes\ndebug.key.password=$KS_PASS\ndebug.storeFile=$CI_PROJECT_DIR/android.keystore\ndebug.password=$KS_PASS\n" > keystore.properties

assembleDebug:
  stage: build
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

assembleRelease:
  stage: build
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew assembleRelease
  artifacts:
    paths:
    - app/build/outputs/

lintDebug:
  stage: test
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint

lintFiles:
  stage: test
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew ktlint

debugTests:
  stage: test
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew -Pci --console=plain :app:testDebug
William Desportes
  • 1,412
  • 1
  • 22
  • 31