> For the complete documentation index, see [llms.txt](https://hezhiqiang.gitbook.io/docker/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hezhiqiang.gitbook.io/docker/ci/travis.md).

# Travis CI

当代码提交到 GitHub 时，[Travis CI](https://travis-ci.com/) 会根据项目根目录 `.travis.yml` 文件设置的指令，执行一系列操作。

本小节介绍如何在 Travis CI 中使用 Docker 进行持续集成/持续部署（CI/CD）。这里以当代码提交到 GitHub 时自动构建 Docker 镜像并推送到 Docker Hub 为例进行介绍。

## 准备

首先登录 <https://travis-ci.com/account/repositories> 选择 GitHub 仓库，按照指引安装 GitHub App 来启用 GitHub 仓库构建。

在项目根目录新建一个 `Dockerfile` 文件。

```
FROM alpine

RUN echo "Hello World"
```

新建 Travis CI 配置文件 `.travis.yml` 文件。

```
language: bash

dist: xenial

services:
  - docker

before_script:
  # 登录到 docker hub
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

script:
  # 这里编写测试代码的命令
  - echo "test code"

after_success:
  # 当代码测试通过后执行的命令
  - docker build -t username/alpine .
  - docker push username/alpine
```

> 请提前在 Travis CI 仓库设置页面配置 `DOCKER_PASSWORD` `DOCKER_USERNAME` 变量

## 查看结果

将项目推送到 GitHub，登录 [Travis CI](https://travis-ci.com/) 查看构建详情。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hezhiqiang.gitbook.io/docker/ci/travis.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
