|
1 | 1 | --- |
2 | | -description: Set up Automated builds |
3 | | -keywords: automated, build, images, Docker Hub |
4 | | -title: Set up Automated Builds |
5 | | -linkTitle: Automated builds |
| 2 | +description: how automated builds work |
| 3 | +keywords: docker hub, automated builds |
| 4 | +title: Automated builds |
6 | 5 | weight: 90 |
7 | 6 | aliases: |
8 | | -- /docker-hub/builds/automated-build/ |
9 | | -- /docker-hub/builds/classic/ |
10 | | -- /docker-hub/builds/ |
| 7 | +- /docker-hub/builds/how-builds-work/ |
11 | 8 | --- |
12 | 9 |
|
13 | 10 | > [!NOTE] |
14 | 11 | > |
15 | 12 | > Automated builds require a |
16 | 13 | > Docker Pro, Team, or Business subscription. |
17 | 14 |
|
18 | | -This page contains information on: |
19 | | -- [Configuring Automated builds](#configure-automated-builds) |
20 | | -- [Advanced Automated build options](#advanced-automated-build-options) |
21 | | -- [Automated builds for teams](#autobuild-for-teams) |
| 15 | +Docker Hub can automatically build images from source code in an external |
| 16 | +repository and automatically push the built image to your Docker repositories. |
22 | 17 |
|
23 | | -## Configure Automated builds |
| 18 | + |
24 | 19 |
|
25 | | -You can configure repositories in Docker Hub so that they automatically |
26 | | -build an image each time you push new code to your source provider. If you have |
27 | | -[automated tests](automated-testing.md) configured, the new image is only pushed |
28 | | -when the tests succeed. |
29 | | - |
30 | | -1. From the **Repositories** section, select a repository to view its details. |
31 | | - |
32 | | -2. Select the **Builds** tab. |
33 | | - |
34 | | -3. Select either GitHub or Bitbucket to connect where the image's source code is stored. |
35 | | - |
36 | | - > Note |
37 | | - > |
38 | | - > You may be redirected to the settings page to [link](link-source.md) the |
39 | | - > code repository service. Otherwise, if you are editing the build settings |
40 | | - > for an existing automated build, click **Configure automated builds**. |
41 | | -
|
42 | | -4. Select the **source repository** to build the Docker images from. |
43 | | - |
44 | | - > Note |
45 | | - > |
46 | | - > You might need to specify an organization or user from |
47 | | - > the source code provider. Once you select a user, source code |
48 | | - > repositories appear in the **Select repository** drop-down list. |
49 | | -
|
50 | | -5. Optional: Enable [autotests](automated-testing.md#enable-automated-tests-on-a-repository). |
51 | | - |
52 | | -6. Review the default **Build Rules**. |
53 | | - |
54 | | - Build rules control what Docker Hub builds into images from the contents |
55 | | - of the source code repository, and how the resulting images are tagged |
56 | | - within the Docker repository. |
57 | | - |
58 | | - A default build rule is set up for you, which you can edit or delete. This |
59 | | - default rule sets builds from the `Branch` in your source code repository called |
60 | | - `master` or `main`, and creates a Docker image tagged with `latest`. For more information, see [set up build rules](#set-up-build-rules) |
61 | | - |
62 | | -7. Optional: Select the **plus** icon to add and [configure more build rules](#set-up-build-rules). |
63 | | - |
64 | | -8. For each branch or tag, enable or disable the **Autobuild** toggle. |
65 | | - |
66 | | - Only branches or tags with autobuild enabled are built, tested, and have |
67 | | - the resulting image pushed to the repository. Branches with autobuild |
68 | | - disabled are built for test purposes (if enabled at the repository |
69 | | - level), but the built Docker image isn't pushed to the repository. |
70 | | - |
71 | | -9. For each branch or tag, enable or disable the **Build Caching** toggle. |
72 | | - |
73 | | - [Build caching](/manuals/build/building/best-practices.md#leverage-build-cache) |
74 | | - can save time if you are building a large image frequently or have |
75 | | - many dependencies. Leave the build caching disabled to |
76 | | - make sure all of your dependencies are resolved at build time, or if |
77 | | - you have a large layer that's quicker to build locally. |
78 | | - |
79 | | -10. Select **Save** to save the settings, or select **Save and build** to save and |
80 | | - run an initial test. |
81 | | - |
82 | | - > Note |
83 | | - > |
84 | | - > A webhook is automatically added to your source code repository to notify |
85 | | - > Docker Hub on every push. Only pushes to branches that are listed as the |
86 | | - > source for one or more tags, trigger a build. |
87 | | - |
88 | | -### Set up build rules |
89 | | - |
90 | | -By default when you set up Automated builds, a basic build rule is created for you. |
91 | | -This default rule watches for changes to the `master` or `main` branch in your source code |
92 | | -repository, and builds the `master` or `main` branch into a Docker image tagged with |
93 | | -`latest`. |
94 | | - |
95 | | -In the **Build Rules** section, enter one or more sources to build. |
96 | | - |
97 | | -For each source: |
98 | | - |
99 | | -* Select the **Source type** to build either a tag or a branch. This |
100 | | - tells the build system what to look for in the source code repository. |
101 | | - |
102 | | -* Enter the name of the **Source** branch or tag you want to build. |
103 | | - |
104 | | - The first time you configure Automated builds, a default build rule is set up |
105 | | - for you. This default set builds from the `Branch` in your source code called |
106 | | - `master`, and creates a Docker image tagged with `latest`. |
107 | | - |
108 | | - You can also use a regex to select which source branches or tags to build. |
109 | | - To learn more, see |
110 | | - [regexes](#regexes-and-automated-builds). |
111 | | - |
112 | | -* Enter the tag to apply to Docker images built from this source. |
113 | | - |
114 | | - If you configured a regex to select the source, you can reference the |
115 | | - capture groups and use its result as part of the tag. To learn more, see |
116 | | - [regexes](#regexes-and-automated-builds). |
117 | | - |
118 | | -* Specify the **Dockerfile location** as a path relative to the root of the source code repository. If the Dockerfile is at the repository root, leave this path set to `/`. |
| 20 | +When you set up automated builds, also called autobuilds, you create a list of |
| 21 | +branches and tags that you want to build into Docker images. When you push code |
| 22 | +to a source-code branch, for example in GitHub, for one of those listed image |
| 23 | +tags, the push uses a webhook to trigger a new build, which produces a Docker |
| 24 | +image. The built image is then pushed to Docker Hub. |
119 | 25 |
|
120 | 26 | > [!NOTE] |
121 | 27 | > |
122 | | -> When Docker Hub pulls a branch from a source code repository, it performs a |
123 | | -> shallow clone - only the tip of the specified branch. Refer to |
124 | | -> [Advanced options for Autobuild and Autotest](advanced.md#source-repository-or-branch-clones) |
125 | | -> for more information. |
126 | | -
|
127 | | -### Environment variables for builds |
128 | | - |
129 | | -You can set the values for environment variables used in your build processes |
130 | | -when you configure an automated build. Add your build environment variables by |
131 | | -selecting the **plus** icon next to the **Build environment variables** section, and |
132 | | -then entering a variable name and the value. |
133 | | - |
134 | | -When you set variable values from the Docker Hub UI, you can use them by the |
135 | | -commands you set in `hooks` files. However, they're stored so that only users who have `admin` access to the Docker Hub repository can see their values. This |
136 | | -means you can use them to store access tokens or other information that |
137 | | -should remain secret. |
138 | | - |
139 | | -> [!NOTE] |
140 | | -> |
141 | | -> The variables set on the build configuration screen are used during |
142 | | -> the build processes only and shouldn't get confused with the environment |
143 | | -> values used by your service, for example to create service links. |
144 | | -
|
145 | | -## Advanced automated build options |
146 | | - |
147 | | -At the minimum you need a build rule composed of a source branch, or tag, and a |
148 | | -destination Docker tag to set up an automated build. You can also: |
149 | | - |
150 | | -- Change where the build looks for the Dockerfile |
151 | | -- Set a path to the files the build should use (the build context) |
152 | | -- Set up multiple static tags or branches to build from |
153 | | -- Use regular expressions (regexes) to dynamically select source code to build and |
154 | | -create dynamic tags |
155 | | - |
156 | | -All of these options are available from the **Build configuration** screen for |
157 | | -each repository. Select **Repositories** from the left navigation, and select the name of the repository you want to edit. Select the **Builds** tab, and then select **Configure Automated builds**. |
158 | | - |
159 | | -### Tag and branch builds |
160 | | - |
161 | | -You can configure your automated builds so that pushes to specific branches or tags triggers a build. |
162 | | - |
163 | | -1. In the **Build Rules** section, select the **plus** icon to add more sources to build. |
164 | | - |
165 | | -2. Select the **Source type** to build either a tag or a branch. |
166 | | - |
167 | | - > Note |
168 | | - > |
169 | | - > This tells the build system what type of source to look for in the code |
170 | | - > repository. |
171 | | -
|
172 | | -3. Enter the name of the **Source** branch or tag you want to build. |
173 | | - |
174 | | - > Note |
175 | | - > |
176 | | - > You can enter a name, or use a regex to match which source branch or tag |
177 | | - > names to build. To learn more, see [regexes](index.md#regexes-and-automated-builds). |
178 | | -
|
179 | | -4. Enter the tag to apply to Docker images built from this source. |
180 | | - |
181 | | - > Note |
182 | | - > |
183 | | - > If you configured a regex to select the source, you can reference the |
184 | | - > capture groups and use its result as part of the tag. To learn more, see |
185 | | - > [regexes](index.md#regexes-and-automated-builds). |
186 | | -
|
187 | | -5. Repeat steps 2 through 4 for each new build rule you set up. |
188 | | - |
189 | | -### Set the build context and Dockerfile location |
190 | | - |
191 | | -Depending on how you arrange the files in your source code repository, the |
192 | | -files required to build your images may not be at the repository root. If that's |
193 | | -the case, you can specify a path where the build looks for the files. |
194 | | - |
195 | | -The build context is the path to the files needed for the build, relative to |
196 | | -the root of the repository. Enter the path to these files in the **Build context** field. Enter `/` to set the build context as the root of the source code repository. |
197 | | - |
198 | | -> [!NOTE] |
199 | | -> |
200 | | -> If you delete the default path `/` from the **Build context** field and leave |
201 | | -> it blank, the build system uses the path to the Dockerfile as the build |
202 | | -> context. However, to avoid confusion it's recommended that you specify the |
203 | | -> complete path. |
204 | | -
|
205 | | -You can specify the **Dockerfile location** as a path relative to the build |
206 | | -context. If the Dockerfile is at the root of the build context path, leave the |
207 | | -Dockerfile path set to `/`. If the build context field is blank, set the path |
208 | | -to the Dockerfile from the root of the source repository. |
209 | | - |
210 | | -### Regexes and Automated builds |
211 | | - |
212 | | -You can specify a regular expression (regex) so that only matching branches or |
213 | | -tags are built. You can also use the results of the regex to create the Docker |
214 | | -tag that's applied to the built image. |
215 | | - |
216 | | -You can use up to nine regular expression capture groups, or expressions enclosed in parentheses, to select a source to build, and reference |
217 | | -these in the **Docker Tag** field using `{\1}` through `{\9}`. |
218 | | - |
219 | | -<!-- Capture groups Not a priority |
220 | | -#### Regex example: build from version number branch and tag with version number |
221 | | -
|
222 | | -You could also use capture groups to build and label images that come from various |
223 | | -sources. For example, you might have |
224 | | -
|
225 | | -`/(alice|bob)-v([0-9.]+)/` --> |
226 | | - |
227 | | -### Build images with BuildKit |
228 | | - |
229 | | -Autobuilds use the BuildKit build system by default. If you want to use the legacy |
230 | | -Docker build system, add the [environment variable](index.md#environment-variables-for-builds) |
231 | | -`DOCKER_BUILDKIT=0`. Refer to the [BuildKit](/manuals/build/buildkit/_index.md) |
232 | | -page for more information on BuildKit. |
233 | | - |
234 | | -## Autobuild for teams |
235 | | - |
236 | | -When you create an automated build repository in your own user account, you |
237 | | -can start, cancel, and retry builds, and edit and delete your own repositories. |
238 | | - |
239 | | -These same actions are also available for team repositories from Docker Hub if |
240 | | -you are an owner. If you are a member of a |
241 | | -team with `write` permissions you can start, cancel, and retry builds in your |
242 | | -team's repositories, but you cannot edit the team repository settings or delete |
243 | | -the team repositories. If your user account has `read` permission, or if you're |
244 | | -a member of a team with `read` permission, you can view the build configuration |
245 | | -including any testing settings. |
246 | | - |
247 | | -| Action/Permission | Read | Write | Admin | Owner | |
248 | | -| --------------------- | ---- | ----- | ----- | ----- | |
249 | | -| view build details | x | x | x | x | |
250 | | -| start, cancel, retry | | x | x | x | |
251 | | -| edit build settings | | | x | x | |
252 | | -| delete build | | | | x | |
253 | | - |
254 | | -### Service users for team autobuilds |
255 | | - |
256 | | -> [!NOTE] |
257 | | -> |
258 | | -> Only owners can set up Automated builds for teams. |
259 | | -
|
260 | | -When you set up Automated builds for teams, you grant Docker Hub access to |
261 | | -your source code repositories using OAuth tied to a specific user account. This |
262 | | -means that Docker Hub has access to everything that the linked source provider |
263 | | -account can access. |
264 | | - |
265 | | -For organizations and teams, it's recommended you create a dedicated service account to grant access to the source provider. This ensures that no |
266 | | -builds break as individual users' access permissions change, and that an |
267 | | -individual user's personal projects aren't exposed to an entire organization. |
268 | | - |
269 | | -This service account should have access to any repositories to be built, |
270 | | -and must have administrative access to the source code repositories so it can |
271 | | -manage deploy keys. If needed, you can limit this account to only a specific |
272 | | -set of repositories required for a specific build. |
273 | | - |
274 | | -If you are building repositories with linked private submodules (private |
275 | | -dependencies), you also need to add an override `SSH_PRIVATE` environment |
276 | | -variable to automated builds associated with the account. For more information, see [Troubleshoot](troubleshoot.md#build-repositories-with-linked-private-submodules) |
277 | | - |
278 | | -1. Create a service user account on your source provider, and generate SSH keys for it. |
279 | | -2. Create a "build" team in your organization. |
280 | | -3. Ensure that the new "build" team has access to each repository and submodule you need to build. |
281 | | - |
282 | | - Go to the repository's **Settings** page. On GitHub, add the new "build" team |
283 | | - to the list of **Collaborators and Teams**. On Bitbucket, add the "build" team |
284 | | - to the list of approved users on the **Access management** screen. |
285 | | - |
286 | | -4. Add the service user to the "build" team on the source provider. |
287 | | - |
288 | | -5. Sign in to Docker Hub as an owner, switch to the organization, and follow the instructions to [link to source code repository](link-source.md) using the service account. |
289 | | - |
290 | | - > [!NOTE] |
291 | | - > |
292 | | - > You may need to log out of your individual account on the source code provider to create the link to the service account. |
293 | | -
|
294 | | -6. Optional: Use the SSH keys you generated to set up any builds with private submodules, using the service account and [the instructions above](troubleshoot.md#build-repositories-with-linked-private-submodules). |
295 | | - |
296 | | -## What's Next? |
297 | | - |
298 | | -- [Customize your build process](advanced.md) with environment variables, hooks, and more |
299 | | -- [Add automated tests](automated-testing.md) |
300 | | -- [Manage your builds](manage-builds.md) |
301 | | -- [Troubleshoot](troubleshoot.md) |
| 28 | +> You can still use `docker push` to push pre-built images to |
| 29 | +repositories with automated builds configured. |
| 30 | + |
| 31 | +If you have automated tests configured, these run after building, but before |
| 32 | +pushing to the registry. You can use these tests to create a continuous |
| 33 | +integration workflow where a build that fails its tests doesn't push the built |
| 34 | +image. Automated tests don't push images to the registry on their own. [Learn about automated image testing](automated-testing.md). |
| 35 | + |
| 36 | +Depending on your [subscription](https://www.docker.com/pricing), |
| 37 | +you may get concurrent builds, which means that `N` autobuilds can be run at the |
| 38 | +same time. `N` is configured according to your subscription. Once `N+1` builds |
| 39 | +are running, any additional builds go into a queue to be run later. |
| 40 | + |
| 41 | +The maximum number of pending builds in the queue is 30 and Docker Hub discards further |
| 42 | +requests. The number of concurrent builds for Pro is 5 and |
| 43 | +for Team and Business is 15. |
| 44 | +Automated builds can handle images of up to 10 GB in size. |
0 commit comments