1

I have setup drone.io locally and created a .drone.yml for CI build. But I found drone removes the docker container after finishing the build. Whether it support reusing the docker container? I am working on gradle project and the initial build takes a long time to download java dependencies.

UPDATE1

I used below command to set the admin user on running drone-server container.

docker run -d \
  -e DRONE_GITHUB=true \
  -e DRONE_GITHUB_CLIENT="xxxx" \
  -e DRONE_GITHUB_SECRET="xxxx" \
  -e DRONE_SECRET="xxxx" \
  -e DRONE_OPEN=true  \
  -e DRONE_DATABASE_DRIVER=mysql \
  -e DRONE_DATABASE_DATASOURCE="root:root@tcp(mysql:3306)/drone?parseTime=true" \
  -e DRONE_ADMIN="joeyzhao0113" \
  --restart=always \
  --name=drone-server \
  --link=mysql \
  drone/drone:0.5

After doing this, I use the user joeyzhao0113 to login drone server but failed to enable the Trusted flag on the setting page. The popup message dialog shows setting successfully see below screenshot. But the flag keep showing disabled always.

enter image description here

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

1 Answers1

9

No, it is not possible to re-use a Docker container for your Drone build. Build containers are ephemeral and are destroyed at the end of every build.

That being said, it doesn't mean your problem cannot be solved.

I think a better way to phrase this question would be "how do I prevent my builds from having to re-download dependencies"? There are two solutions to this problem.

Option 1, Cache Plugin

The first, recommended solution, is to use a plugin to cache and restore your dependencies. Cache plugins such as the volume cache and s3 cache are community contributed plugins.

pipeline:
  # restores the cache from a local volume
  restore-cache:
    image: drillster/drone-volume-cache
    restore: true
    mount: [ /drone/.gradle, /drone/.m2 ]
    volumes: 
      - /tmp/cache:/cache

  build:
    image: maven
    environment:
      - M2_HOME=/drone/.m2
      - MAVEN_HOME=/drone/.m2
      - GRADLE_USER_HOME=/drone/.gradle
    commands:
      - mvn install
      - mvn package

  # rebuild the cache in case new dependencies were
  # downloaded during your build
  rebuild-cache:
    image: drillster/drone-volume-cache
    rebuild: true
    mount: [ /drone/.gradle, /drone/.m2 ]
    volumes: 
      - /tmp/cache:/cache

Option 2, Custom Image

The second solution is to create a Docker image with your dependencies, publish to DockerHub, and use this as your build image in your .drone.yml file.

pipeline:
  build:
    image: some-image-with-all-my-dependencies
    commands:
      - mvn package
Robert Taylor
  • 642
  • 5
  • 13
Brad Rydzewski
  • 2,523
  • 14
  • 18
  • I checked the cache plugin and it says: 'The cached files or directories must be located in your build workspace. It is not possible to cache files outside of your build workspace.'. Does the build workspace mean git repository? The cached data will be located at ~/.m2 and ~/.gradle directory which are outside of git repository. How to achieve this cache? – Joey Yi Zhao Jan 01 '17 at 23:14
  • the default workspace base is `/drone` assuming you didn't override the default base value. This means you can set `GRADLE_USER_HOME=/drone/.gradle` to instruct gradle to store the dependencies in your workspace, making accessible to the cache plugin. I've updated the example in my answer to show how this could work. – Brad Rydzewski Jan 02 '17 at 05:33
  • I got this error 'ERROR: Insufficient privileges to use volumes'. How to set privilege in drone in order to use volumes? – Joey Yi Zhao Jan 02 '17 at 06:03
  • this is mentioned in the volume plugin documentation, see http://plugins.drone.io/drillster/drone-volume-cache/ ... You can use the search box in the official documentation for this error message, which will take you to http://readme.drone.io/questions/error-insufficient-privileges/ – Brad Rydzewski Jan 02 '17 at 14:52
  • I have read this line 'The above errors can be resolved by having an administrator grant your repository elevated privileges, by toggling the trusted flag in the repository settings.' Buy I have not found where I can set the trust flag? Is there a document to say how to configure it in drone? – Joey Yi Zhao Jan 02 '17 at 22:43
  • To view the repository settings screen, select your repository in drone, and then click the settings link. The settings link is underneath the repository name, next to the badges and builds links. When you click the settings link, if you are a drone administrator, you will see a "trusted" toggle. If you are not a drone administrator, per the documentation, you need to have an administrator set the trusted flag for you. – Brad Rydzewski Jan 03 '17 at 04:12
  • I have the admin on my github repository. And I am able to see the toggle under setting link. A message 'Successfully update repository settings' was shown at the bottom of the page when I enable the 'Trusted' setting. But it is still shown as disabled on the screen. I have tried to refresh the page and re-login but no help. Other settings work fine. Is there something wrong with the Trusted settings? – Joey Yi Zhao Jan 03 '17 at 05:47
  • you need to be a drone administrator, not a repository administrator. They are not the same thing. See http://readme.drone.io/admin/user-admins/ – Brad Rydzewski Jan 03 '17 at 14:20
  • I have added my configuration in the post and a screenshot to show the issue of unable to enable the trusted flag. Could you please take a look at? – Joey Yi Zhao Jan 03 '17 at 22:24
  • How to run this cache locally? When I run `drone exec`, I got `ERROR: Insufficient privileges to use volumes` error. How can I enable privilege locally? – Joey Yi Zhao Jan 17 '17 at 02:03
  • you need `drone exec --repo.trusted` flag which simulates the local build being marked as trusted in the UI – Brad Rydzewski Jan 17 '17 at 12:50