Note: GitHub-hosted runners are not currently supported on Servidor de GitHub Enterprise. You can see more information about planned future support on the Itinerario público de GitHub.
Introduction
This guide shows you how to create a workflow that performs a Docker build, and then publishes Docker images to Docker Hub or Paquetes de GitHub. With a single workflow, you can publish images to a single registry or to multiple registries.
Note: If you want to push to another third-party Docker registry, the example in the "Publishing images to Paquetes de GitHub" section can serve as a good template.
Prerequisites
We recommend that you have a basic understanding of workflow configuration options and how to create a workflow file. For more information, see "Learn GitHub Actions."
You might also find it helpful to have a basic understanding of the following:
- "Encrypted secrets"
- "Authentication in a workflow"
- "Configuring Docker for use with Paquetes de GitHub"
About image configuration
This guide assumes that you have a complete definition for a Docker image stored in a GitHub repository. For example, your repository must contain a Dockerfile, and any other files needed to perform a Docker build to create an image.
In this guide, we will use the Docker build-push-action
action to build the Docker image and push it to one or more Docker registries. For more information, see build-push-action
.
Note: GitHub Actions on tu instancia de servidor de GitHub Enterprise may have limited access to actions on GitHub.com or Mercado GitHub. Para obtener más información, consulta "La comunicación entre ejecutores autoalojados y GitHub."
Publishing images to Docker Hub
Cada vez que creas un nuevo lanzamiento en GitHub, puedes activar un flujo de trabajo para publicar tu imagen. El flujo de trabajo en el ejemplo siguiente se ejecuta cuando el se activa el evento release
con el tipo de actividad created
. Para obtener más información en el evento release
, consulta la sección"Eventos que activan flujos de trabajo.
In the example workflow below, we use the Docker build-push-action
action to build the Docker image and, if the build succeeds, push the built image to Docker Hub.
To push to Docker Hub, you will need to have a Docker Hub account, and have a Docker Hub repository created. For more information, see "Pushing a Docker container image to Docker Hub" in the Docker documentation.
The build-push-action
options required for Docker Hub are:
username
andpassword
: This is your Docker Hub username and password. We recommend storing your Docker Hub username and password as secrets so they aren't exposed in your workflow file. For more information, see "Creating and using encrypted secrets."repository
: Your Docker Hub repository in the formatDOCKER-HUB-NAMESPACE/DOCKER-HUB-REPOSITORY
.
name: Publish Docker image
on:
release:
types: [published]
jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: my-docker-hub-namespace/my-docker-hub-repository
tag_with_ref: true
El flujo de trabajo anterior verifica el repositorio GitHub y utiliza la acción build-push-action
para construir y subir la imagen de Docker. Éste configura la opción tag_with_ref
de build-push-action
para que etiquete automáticamente la imagen construida de Docker con la referencia de Git del evento del flujo de trabajo. Este flujo de trabajo se desencadena cuando se publica un lanzamiento de GitHub, para que la referencia sea la etiqueta de Git para el lanzamiento.
Publishing images to Paquetes de GitHub
Cada vez que creas un nuevo lanzamiento en GitHub, puedes activar un flujo de trabajo para publicar tu imagen. El flujo de trabajo en el ejemplo siguiente se ejecuta cuando el se activa el evento release
con el tipo de actividad created
. Para obtener más información en el evento release
, consulta la sección"Eventos que activan flujos de trabajo.
In the example workflow below, we use the Docker build-push-action
action to build the Docker image, and if the build succeeds, push the built image to Paquetes de GitHub.
The build-push-action
options required for Paquetes de GitHub are:
username
: You can use the${{ github.actor }}
context to automatically use the username of the user that triggered the workflow run. For more information, see "Context and expression syntax for GitHub Actions."password
: You can use the automatically-generatedGITHUB_TOKEN
secret for the password. For more information, see "Authenticating with the GITHUB_TOKEN."registry
: Must be set todocker.pkg.github.com
.repository
: Must be set in the formatOWNER/REPOSITORY/IMAGE_NAME
. For example, for an image namedocto-image
stored on GitHub athttp://github.com/octo-org/octo-repo
, therepository
option should be set toocto-org/octo-repo/octo-image
.
name: Publish Docker image
on:
release:
types: [published]
jobs:
push_to_registry:
name: Push Docker image to GitHub Packages
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: my-org/my-repo/my-image
tag_with_ref: true
El flujo de trabajo anterior verifica el repositorio GitHub y utiliza la acción build-push-action
para construir y subir la imagen de Docker. Éste configura la opción tag_with_ref
de build-push-action
para que etiquete automáticamente la imagen construida de Docker con la referencia de Git del evento del flujo de trabajo. Este flujo de trabajo se desencadena cuando se publica un lanzamiento de GitHub, para que la referencia sea la etiqueta de Git para el lanzamiento.
Publishing images to Docker Hub and Paquetes de GitHub
In a single workflow, you can publish your Docker image to multiple registries by using the build-push-action
action for each registry.
The following example workflow uses the build-push-action
steps from the previous sections ("Publishing images to Docker Hub" and "Publishing images to Paquetes de GitHub") to create a single workflow that pushes to both registries.
name: Publish Docker image
on:
release:
types: [published]
jobs:
push_to_registries:
name: Push Docker image to multiple registries
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: my-docker-hub-namespace/my-docker-hub-repository
tag_with_ref: true
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: my-org/my-repo/my-image
tag_with_ref: true
The above workflow checks out the GitHub repository, and uses the build-push-action
action twice to build and push the Docker image to Docker Hub and Paquetes de GitHub. For both steps, it sets the build-push-action
option tag_with_ref
to automatically tag the built Docker image with the Git reference of the workflow event. This workflow is triggered on publishing a GitHub release, so the reference for both registries will be the Git tag for the release.