github.com/feiyang21687/docker@v1.5.0/docs/sources/docker-hub/builds.md (about)

     1  page_title: Automated Builds on Docker Hub
     2  page_description: Docker Hub Automated Builds
     3  page_keywords: Docker, docker, registry, accounts, plans, Dockerfile, Docker Hub, docs, documentation, trusted, builds, trusted builds, automated builds
     4  
     5  # Automated Builds on Docker Hub
     6  
     7  ## About Automated Builds
     8  
     9  *Automated Builds* are a special feature of Docker Hub which allow you to
    10  use [Docker Hub's](https://hub.docker.com) build clusters to automatically
    11  create images from a specified `Dockerfile` and a GitHub or Bitbucket repository
    12  (or "context"). The system will clone your repository and build the image
    13  described by the `Dockerfile` using the repository as the context. The
    14  resulting automated image will then be uploaded to the Docker Hub registry
    15  and marked as an *Automated Build*.
    16  
    17  Automated Builds have several advantages:
    18  
    19  * Users of *your* Automated Build can trust that the resulting
    20  image was built exactly as specified.
    21  
    22  * The `Dockerfile` will be available to anyone with access to
    23  your repository on the Docker Hub registry. 
    24  
    25  * Because the process is automated, Automated Builds help to
    26  make sure that your repository is always up to date.
    27  
    28  Automated Builds are supported for both public and private repositories
    29  on both [GitHub](http://github.com) and [Bitbucket](https://bitbucket.org/).
    30  
    31  To use Automated Builds, you must have an [account on Docker Hub](
    32  http://docs.docker.com/userguide/dockerhub/#creating-a-docker-hub-account)
    33  and on GitHub and/or Bitbucket. In either case, the account needs
    34  to be properly validated and activated before you can link to it.
    35  
    36  ## Setting up Automated Builds with GitHub
    37  
    38  In order to set up an Automated Build, you need to first link your
    39  [Docker Hub](https://hub.docker.com) account with a GitHub account.
    40  This will allow the registry to see your repositories.
    41  
    42  > *Note:* 
    43  > Automated Builds currently require *read* and *write* access since
    44  > [Docker Hub](https://hub.docker.com) needs to setup a GitHub service
    45  > hook. We have no choice here, this is how GitHub manages permissions, sorry! 
    46  > We do guarantee nothing else will be touched in your account.
    47  
    48  To get started, log into your Docker Hub account and click the
    49  "+ Add Repository" button at the upper right of the screen. Then select
    50  [Automated Build](https://registry.hub.docker.com/builds/add/).
    51  
    52  Select the [GitHub service](https://registry.hub.docker.com/associate/github/).
    53  
    54  Then follow the onscreen instructions to authorize and link your
    55  GitHub account to Docker Hub. Once it is linked, you'll be able to
    56  choose a repo from which to create the Automatic Build.
    57  
    58  ### Creating an Automated Build
    59  
    60  You can [create an Automated Build](
    61  https://registry.hub.docker.com/builds/github/select/) from any of your
    62  public or private GitHub repositories with a `Dockerfile`.
    63  
    64  ### GitHub Submodules
    65  
    66  If your GitHub repository contains links to private submodules, you'll
    67  need to add a deploy key from your Docker Hub repository. 
    68  
    69  Your Docker Hub deploy key is located under the "Build Details"
    70  menu on the Automated Build's main page in the Hub. Add this key
    71  to your GitHub submodule by visiting the Settings page for the
    72  repository on GitHub and selecting "Deploy keys".
    73  
    74  <table class="table table-bordered">
    75    <thead>
    76      <tr>
    77        <th>Step</th>
    78        <th>Screenshot</th>
    79        <th>Description</th>
    80      </tr>
    81    </thead>
    82    <tbody>
    83      <tr>
    84        <td>1.</td>
    85        <td><img src="/docker-hub/hub-images/deploy_key.png"></td>
    86        <td>Your automated build's deploy key is in the "Build Details" menu 
    87  under "Deploy keys".</td>
    88      </tr>
    89      <tr>
    90        <td>2.</td>
    91        <td><img src="/docker-hub/hub-images/github_deploy_key.png"></td>
    92        <td>In your GitHub submodule's repository Settings page, add the 
    93  deploy key from your Docker Hub Automated Build.</td>
    94      </tr>
    95    </tbody>
    96  </table>
    97       
    98  ### GitHub Organizations
    99  
   100  GitHub organizations will appear once your membership to that organization is
   101  made public on GitHub. To verify, you can look at the members tab for your
   102  organization on GitHub.
   103  
   104  ### GitHub Service Hooks
   105  
   106  Follow the steps below to configure the GitHub service
   107  hooks for your Automated Build:
   108  
   109  <table class="table table-bordered">
   110    <thead>
   111      <tr>
   112        <th>Step</th>
   113        <th>Screenshot</th>
   114        <th>Description</th>
   115      </tr>
   116    </thead>
   117    <tbody>
   118      <tr>
   119        <td>1.</td>
   120        <td><img src="/docker-hub/hub-images/gh_settings.png"></td>
   121        <td>Log in to Github.com, and go to your Repository page. Click on "Settings" on
   122        the right side of the page. You must have admin privileges to the repository in order to do this.</td>
   123      </tr>
   124      <tr>
   125        <td>2.</td>
   126        <td><img src="/docker-hub/hub-images/gh_menu.png" alt="Webhooks & Services"></td>
   127        <td>Click on "Webhooks & Services" on the left side of the page.</td></tr>
   128        <tr><td>3.</td>
   129        <td><img src="/docker-hub/hub-images/gh_service_hook.png" alt="Find the service labeled Docker"></td><td>Find the service labeled "Docker" and click on it.</td></tr>
   130        <tr><td>4.</td><td><img src="/docker-hub/hub-images/gh_docker-service.png" alt="Activate Service Hooks"></td>
   131        <td>Make sure the "Active" checkbox is selected and click the "Update service" button to save your changes.</td>
   132      </tr>
   133    </tbody>
   134  </table>
   135  
   136  ## Setting up Automated Builds with Bitbucket
   137  
   138  In order to setup an Automated Build, you need to first link your
   139  [Docker Hub](https://hub.docker.com) account with a Bitbucket account.
   140  This will allow the registry to see your repositories.
   141  
   142  To get started, log into your Docker Hub account and click the
   143  "+ Add Repository" button at the upper right of the screen. Then
   144  select [Automated Build](https://registry.hub.docker.com/builds/add/).
   145  
   146  Select the [Bitbucket source](
   147  https://registry.hub.docker.com/associate/bitbucket/).
   148  
   149  Then follow the onscreen instructions to authorize and link your
   150  Bitbucket account to Docker Hub. Once it is linked, you'll be able
   151  to choose a repository from which to create the Automatic Build.
   152  
   153  ### Creating an Automated Build
   154  
   155  You can [create an Automated Build](
   156  https://registry.hub.docker.com/builds/bitbucket/select/) from any of your
   157  public or private Bitbucket repositories with a `Dockerfile`.
   158  
   159  ### Adding a Hook
   160  
   161  When you link your Docker Hub account, a `POST` hook should get automatically
   162  added to your Bitbucket repository. Follow the steps below to confirm or modify the
   163  Bitbucket hooks for your Automated Build:
   164  
   165  <table class="table table-bordered">
   166    <thead>
   167      <tr>
   168        <th>Step</th>
   169        <th>Screenshot</th>
   170        <th>Description</th>
   171      </tr>
   172    </thead>
   173    <tbody>
   174      <tr>
   175        <td>1.</td>
   176        <td><img src="/docker-hub/hub-images/bb_menu.png" alt="Settings" width="180"></td>
   177        <td>Log in to Bitbucket.org and go to your Repository page. Click on "Settings" on
   178        the far left side of the page, under "Navigation". You must have admin privileges
   179        to the repository in order to do this.</td>
   180      </tr>
   181      <tr>
   182        <td>2.</td>
   183        <td><img src="/docker-hub/hub-images/bb_hooks.png" alt="Hooks" width="180"></td>
   184        <td>Click on "Hooks" on the near left side of the page, under "Settings".</td></tr>
   185      <tr>
   186        <td>3.</td>
   187        <td><img src="/docker-hub/hub-images/bb_post-hook.png" alt="Docker Post Hook"></td><td>You should now see a list of hooks associated with the repo, including a <code>POST</code> hook that points at
   188        registry.hub.docker.com/hooks/bitbucket.</td>
   189      </tr>
   190    </tbody>
   191  </table>
   192  
   193  
   194  ## The Dockerfile and Automated Builds
   195  
   196  During the build process, Docker will copy the contents of your `Dockerfile`.
   197  It will also add it to the [Docker Hub](https://hub.docker.com) for the Docker
   198  community (for public repositories) or approved team members/orgs (for private
   199  repositories) to see on the repository page.
   200  
   201  ### README.md
   202  
   203  If you have a `README.md` file in your repository, it will be used as the
   204  repository's full description.The build process will look for a
   205  `README.md` in the same directory as your `Dockerfile`.
   206  
   207  > **Warning:**
   208  > If you change the full description after a build, it will be
   209  > rewritten the next time the Automated Build has been built. To make changes,
   210  > modify the `README.md` from the Git repository.
   211  
   212  ## Remote Build triggers
   213  
   214  If you need a way to trigger Automated Builds outside of GitHub or Bitbucket,
   215  you can set up a build trigger. When you turn on the build trigger for an
   216  Automated Build, it will give you a URL to which you can send POST requests.
   217  This will trigger the Automated Build, much as with a GitHub webhook.
   218  
   219  Build triggers are available under the Settings menu of each Automated Build
   220  repository on the Docker Hub.
   221  
   222  ![Build trigger screen](/docker-hub/hub-images/build-trigger.png)
   223  
   224  You can use `curl` to trigger a build:
   225  
   226  ```
   227  $ curl --data "build=true" -X POST https://registry.hub.docker.com/u/svendowideit/testhook/trigger/be579c
   228  82-7c0e-11e4-81c4-0242ac110020/
   229  OK
   230  ```
   231  
   232  > **Note:** 
   233  > You can only trigger one build at a time and no more than one
   234  > every five minutes. If you already have a build pending, or if you
   235  > recently submitted a build request, those requests *will be ignored*.
   236  > To verify everything is working correctly, check the logs of last
   237  > ten triggers on the settings page .
   238  
   239  ## Webhooks
   240  
   241  Automated Builds also include a Webhooks feature. Webhooks can be called
   242  after a successful repository push is made. This includes when a new tag is added
   243  to an existing image.
   244  
   245  The webhook call will generate a HTTP POST with the following JSON
   246  payload:
   247  
   248  ```
   249  {
   250    "callback_url": "https://registry.hub.docker.com/u/svendowideit/testhook/hook/2141b5bi5i5b02bec211i4eeih0242eg11000a/",
   251    "push_data": {
   252      "images": [
   253          "27d47432a69bca5f2700e4dff7de0388ed65f9d3fb1ec645e2bc24c223dc1cc3",
   254          "51a9c7c1f8bb2fa19bcd09789a34e63f35abb80044bc10196e304f6634cc582c",
   255          ...
   256      ],
   257      "pushed_at": 1.417566161e+09,
   258      "pusher": "trustedbuilder"
   259    },
   260    "repository": {
   261      "comment_count": 0,
   262      "date_created": 1.417494799e+09,
   263      "description": "",
   264      "dockerfile": "#\n# BUILD\u0009\u0009docker build -t svendowideit/apt-cacher .\n# RUN\u0009\u0009docker run -d -p 3142:3142 -name apt-cacher-run apt-cacher\n#\n# and then you can run containers with:\n# \u0009\u0009docker run -t -i -rm -e http_proxy http://192.168.1.2:3142/ debian bash\n#\nFROM\u0009\u0009ubuntu\nMAINTAINER\u0009SvenDowideit@home.org.au\n\n\nVOLUME\u0009\u0009[\"/var/cache/apt-cacher-ng\"]\nRUN\u0009\u0009apt-get update ; apt-get install -yq apt-cacher-ng\n\nEXPOSE \u0009\u00093142\nCMD\u0009\u0009chmod 777 /var/cache/apt-cacher-ng ; /etc/init.d/apt-cacher-ng start ; tail -f /var/log/apt-cacher-ng/*\n",
   265      "full_description": "Docker Hub based automated build from a GitHub repo",
   266      "is_official": false,
   267      "is_private": true,
   268      "is_trusted": true,
   269      "name": "testhook",
   270      "namespace": "svendowideit",
   271      "owner": "svendowideit",
   272      "repo_name": "svendowideit/testhook",
   273      "repo_url": "https://registry.hub.docker.com/u/svendowideit/testhook/",
   274      "star_count": 0,
   275      "status": "Active"
   276    }
   277  }
   278  ```
   279  
   280  Webhooks are available under the Settings menu of each Repository.
   281  
   282  > **Note:** If you want to test your webhook out we recommend using
   283  > a tool like [requestb.in](http://requestb.in/).
   284  
   285  > **Note**: The Docker Hub servers are currently in the IP range
   286  > `162.242.195.64 - 162.242.195.127`, so you can restrict your webhooks to
   287  > accept webhook requests from that set of IP addresses.
   288  
   289  ### Webhook chains
   290  
   291  Webhook chains allow you to chain calls to multiple services. For example,
   292  you can use this to trigger a deployment of your container only after
   293  it has been successfully tested, then update a separate Changelog once the
   294  deployment is complete.
   295  After clicking the "Add webhook" button, simply add as many URLs as necessary
   296  in your chain.
   297  
   298  The first webhook in a chain will be called after a successful push. Subsequent
   299  URLs will be contacted after the callback has been validated.
   300  
   301  ### Validating a callback
   302  
   303  In order to validate a callback in a webhook chain, you need to
   304  
   305  1. Retrieve the `callback_url` value in the request's JSON payload.
   306  1. Send a POST request to this URL containing a valid JSON body.
   307  
   308  > **Note**: A chain request will only be considered complete once the last
   309  > callback has been validated.
   310  
   311  To help you debug or simply view the results of your webhook(s),
   312  view the "History" of the webhook available on its settings page.
   313  
   314  ### Callback JSON data
   315  
   316  The following parameters are recognized in callback data:
   317  
   318  * `state` (required): Accepted values are `success`, `failure` and `error`.
   319    If the state isn't `success`, the webhook chain will be interrupted.
   320  * `description`: A string containing miscellaneous information that will be
   321    available on the Docker Hub. Maximum 255 characters.
   322  * `context`: A string containing the context of the operation. Can be retrieved
   323    from the Docker Hub. Maximum 100 characters.
   324  * `target_url`: The URL where the results of the operation can be found. Can be
   325    retrieved on the Docker Hub.
   326  
   327  *Example callback payload:*
   328  
   329      {
   330        "state": "success",
   331        "description": "387 tests PASSED",
   332        "context": "Continuous integration by Acme CI",
   333        "target_url": "http://ci.acme.com/results/afd339c1c3d27"
   334      }
   335  
   336  ## Repository links
   337  
   338  Repository links are a way to associate one Automated Build with
   339  another. If one gets updated,the linking system triggers a rebuild
   340  for the other Automated Build. This makes it easy to keep all your
   341  Automated Builds up to date.
   342  
   343  To add a link, go to the repository for the Automated Build you want to
   344  link to and click on *Repository Links* under the Settings menu at
   345  right. Then, enter the name of the repository that you want have linked.
   346  
   347  > **Warning:**
   348  > You can add more than one repository link, however, you should
   349  > do so very carefully. Creating a two way relationship between Automated Builds will
   350  > cause an endless build loop.