Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓自动化之旅(七)——短篇,优化Docker缓存(短篇) #78

Open
soapgu opened this issue Sep 23, 2021 · 0 comments
Labels
IDE Good for newcomers 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Sep 23, 2021

安卓自动化之旅(六)——使用Coding&Docker持续集成安卓项目这篇里面留了一个小尾巴,这次一并补上。

  • 正文

参考以下内容

增加两个stage,直接上脚本代码

pipeline {
  agent any
  environment{
    DOCKER_CACHE_EXISTS = fileExists '/root/.cache/docker/jre-11.tar'
  }
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM',
        branches: [[name: GIT_BUILD_REF]],
        userRemoteConfigs: [[
          url: GIT_REPO_URL,
          credentialsId: CREDENTIALS_ID
        ]]])
      }
    }
    stage('加载缓存') {
      when { expression { DOCKER_CACHE_EXISTS == 'true' } }
      steps {
        sh 'docker load -i /root/.cache/docker/jre-11.tar'
      }
    }
    stage('编译构建') {
      agent {
        docker {
          reuseNode 'true'
          registryUrl 'https://shgbit-docker.pkg.coding.net'
          registryCredentialsId "${env.DOCKER_REGISTRY_CREDENTIALS_ID}"
          image 's365/dockers/jre-11:1.0'
          args '-v "/root/programs/android-sdk:/root/programs/android-sdk" -v /root/.gradlecache/:/root/.gradle/'
        }

      }
      steps {
        dir('android/projects/assistant') {
          sh 'chmod +x gradlew'
          sh './gradlew clean && rm -rf ./app/build/'
          sh './gradlew assembleRelease'
        }
      }
    }
    stage('生成缓存') {
      when { expression { DOCKER_CACHE_EXISTS == 'false' } }
      steps {
        sh 'mkdir -p /root/.cache/docker/'
        sh 'docker save -o /root/.cache/docker/jre-11.tar shgbit-docker.pkg.coding.net/s365/dockers/jre-11:1.0'
      }
    }
    stage('上传到 Generic') {
      steps {
        dir('android/projects/assistant/app/build/outputs/apk/release') {
          codingArtifactsGeneric(credentialsId: "${CODING_ARTIFACTS_CREDENTIALS_ID}", withBuildProps: true, files: '*.apk', repoName: "${GENERIC_REPO_NAME}", version: "${CI_BUILD_NUMBER}")
        }
      }
    }
  }
}

说明

  1. 声明环境变量 DOCKER_CACHE_EXISTS
  2. 在加载Docker agent 前的Stage先加入加载缓存的stage
  3. 在结束前,加入生成缓存stage
  4. 增加/root/.cache/缓存目录
  • 原理

主要的疑惑点在于,Jenkins怎么知道我已经加载好了Docker,不需要再pull了??
我们直接从日志里去找答案
没有加载缓存前

[2021-09-22 16:36:20] [Pipeline] {
[2021-09-22 16:36:20] [Pipeline] isUnix
[2021-09-22 16:36:20] [Pipeline] sh
[2021-09-22 16:36:20] + docker inspect -f . s365/dockers/jre-11:1.0
[2021-09-22 16:36:20] 
[2021-09-22 16:36:20] Error: No such object: s365/dockers/jre-11:1.0
[2021-09-22 16:36:20] [Pipeline] isUnix
[2021-09-22 16:36:20] [Pipeline] sh
[2021-09-22 16:36:21] + docker inspect -f . shgbit-docker.pkg.coding.net/s365/dockers/jre-11:1.0
[2021-09-22 16:36:21] 
[2021-09-22 16:36:21] Error: No such object: shgbit-docker.pkg.coding.net/s365/dockers/jre-11:1.0
[2021-09-22 16:36:21] [Pipeline] isUnix
[2021-09-22 16:36:21] [Pipeline] sh
[2021-09-22 16:36:21] + docker pull shgbit-docker.pkg.coding.net/s365/dockers/jre-11:1.0
[2021-09-22 16:36:21] 1.0: Pulling from s365/dockers/jre-11
[2021-09-22 16:36:21] a330b6cecb98: Pulling fs layer
[2021-09-22 16:36:21] 752cb4b38af3: Pulling fs layer
[2021-09-22 16:36:21] 3e2cf95ad979: Pulling fs layer
...

加载缓存后

[2021-09-22 16:39:22] [Pipeline] {
[2021-09-22 16:39:23] [Pipeline] isUnix
[2021-09-22 16:39:23] [Pipeline] sh
[2021-09-22 16:39:23] + docker inspect -f . s365/dockers/jre-11:1.0
[2021-09-22 16:39:23] 
[2021-09-22 16:39:23] Error: No such object: s365/dockers/jre-11:1.0
[2021-09-22 16:39:23] [Pipeline] isUnix
[2021-09-22 16:39:23] [Pipeline] sh
[2021-09-22 16:39:23] + docker inspect -f . shgbit-docker.pkg.coding.net/s365/dockers/jre-11:1.0
[2021-09-22 16:39:23] .
[2021-09-22 16:39:23] [Pipeline] withDockerContainer
[2021-09-22 16:39:23] Jenkins does not seem to be running inside a container
[2021-09-22 16:39:23] $ docker run -t -d -u 0:0 -v /root/programs/android-sdk

还是比较明显的。
Coding会自动生成docker inspect -f的脚本来判断是否已经缓存了Docker

图片
上次没一起弄是因为我的Docker镜像比较小,弄不弄差不多。
但是为了整体性考虑,还是要考虑优化的。对比下优化前后,基本上优化了7s左右,但这个镜像只有140M不到。如果是一个1G的大镜像那空间就很大了。

@soapgu soapgu added IDE Good for newcomers 安卓 安卓 labels Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDE Good for newcomers 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant