drone在serices中访问文件


原文链接: drone在serices中访问文件

How to access files from services

在drone 执行构建的时候 会创建Service containers.
有时候我们需要这些services容器 可以访问我们源文件repository 中的文件或文件夹

Nginx containers Example

配置 nginx service 在启动的时候加载我们source repository 中的配置
问题: 1. 如果定义为services 那么就是在repository clone之前就启动的
解决: 2. 那么我们可以将Nginx services 放入pipeline中,强制让服务在执行完 clone stage 之后在启动

pipeline:
+ web:
+   image: nginx:latest
+   detach: true

// 服务是在 pipeline 之前启动
services:
- web:
-   image: nginx:latest

The below example overrides the default command used to start the docker image. This allows us to override the location of the default configuration file using command line flags.

pipeline:
web:

image: nginx:latest
detach: true
command:
  - "nginx"
  • - "-c"
  • - "/drone/src/github.com/octocat/hello-world/nginx.conf"
    • "-g"
    • "daemon off;"

The above example instructs nginx to load the configuration file from the workspace where our repository is cloned. The workspace is mounted into every container as a volume and available to services at runtime.

The below configuration instructs nginx to serve content from a folder in our workspace directory. This folder could contain static assets, php files, etc.

server {

index index.html;
server_name php-docker.local;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
  • root /drone/src/github.com/octocat/hello-world/www;
    }

Postgres

This example demonstrate how we can configure a postgres service to execute scripts from our source repository on startup. The official postgres image looks for scripts at /docker-entrypoint-initdb.d

The first problem we’ll face is service containers are started before the source repository is cloned. This can be mitigated by moving the service directly into the pipeline, forcing it to execute after the clone stage.

pipeline:

  • database:
  • image: postgres:latest
  • detach: true

services:

  • database:
  • image: postgres:latest

The below example overrides the default entrypoint and command used to start the docker image. This can be used to run a custom shell script that copies files from our source repository folder to /docker-entrypoint-initdb.d.

pipeline:
database:

detach: true
image: postgres
  • entrypoint: /bin/sh
  • command:
  • - -c
  • - |
  • cp /drone/src/github.com/octocat/hello-world/pg_init.sh /docker-entrypoint-initdb.d/
  • /docker-entrypoint.sh postgres
`