Skip to content

Added Configure GitHub Actions step to the Python guide #22676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 0 additions & 133 deletions content/guides/python/configure-ci-cd.md

This file was deleted.

111 changes: 111 additions & 0 deletions content/guides/python/configure-github-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
title: Automate your builds with GitHub Actions
linkTitle: Automate your builds with GitHub Actions
weight: 20
keywords: ci/cd, github actions, python, flask

Check failure on line 5 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Python' instead of 'python'. Raw Output: {"message": "[Vale.Terms] Use 'Python' instead of 'python'.", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 5, "column": 34}}}, "severity": "ERROR"}

Check failure on line 5 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'GitHub( Actions)?' instead of 'github actions'. Raw Output: {"message": "[Vale.Terms] Use 'GitHub( Actions)?' instead of 'github actions'.", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 5, "column": 18}}}, "severity": "ERROR"}
description: Learn how to configure CI/CD using GitHub Actions for your Python application.

Check warning on line 6 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'CD' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'CD' has no definition.", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 6, "column": 40}}}, "severity": "WARNING"}
aliases:
- /language/python/configure-ci-cd/
- /guides/language/python/configure-ci-cd/
- /guides/python/configure-ci-cd/
---

## Prerequisites

Complete all the previous sections of this guide, starting with [Containerize a Python application](containerize.md). You must have a [GitHub](https://github.com/signup) account and a [Docker](https://hub.docker.com/signup) account to complete this section.

If you didn't create a [GitHub repository](https://github.com/new) for your project yet, it is time to do it. After creating the repository, don't forget to [add a remote](https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories) and ensure you can commit and [push your code](https://docs.github.com/en/get-started/using-git/pushing-commits-to-a-remote-repository#about-git-push) to GitHub.

1. In your project's GitHub repository, open **Settings**, and go to **Secrets and variables** > **Actions**.

2. Under the **Variables** tab, create a new **Repository variable** named `DOCKER_USERNAME` and your Docker ID as a value.

3. Create a new [Personal Access Token (PAT)](/manuals/security/for-developers/access-tokens.md#create-an-access-token) for Docker Hub. You can name this token `docker-tutorial`. Make sure access permissions include Read and Write.

4. Add the PAT as a **Repository secret** in your GitHub repository, with the name
`DOCKERHUB_TOKEN`.

## Overview

GitHub Actions is a CI/CD (Continuous Integration and Continuous Deployment) automation tool built into GitHub. It allows you to define custom workflows for building, testing, and deploying your code when specific events occur (e.g., pushing code, creating a pull request, etc.). A workflow is a YAML-based automation script that defines a sequence of steps to be executed when triggered. Workflows are stored in the `.github/workflows/` directory of a repository.

Check warning on line 30 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.Acronyms] 'CD' has no definition. Raw Output: {"message": "[Docker.Acronyms] 'CD' has no definition.", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 30, "column": 24}}}, "severity": "WARNING"}

In this section, you'll learn how to set up and use GitHub Actions to build your Docker image as well as push it to Docker Hub. You will complete the following steps:

1. Define the GitHub Actions workflow.
2. Run the workflow.

## 1. Define the GitHub Actions workflow

You can create a GitHub Actions workflow by creating a YAML file in the `.github/workflows/` directory of your repository. To do this use your favorite text editor or the GitHub web interface. The following steps show you how to create a workflow file using the GitHub web interface.

If you prefer to use the GitHub web interface, follow these steps:

1. Go to your repository on GitHub and then select the **Actions** tab.

2. Select **set up a workflow yourself**.

This takes you to a page for creating a new GitHub Actions workflow file in
your repository. By default, the file is created under `.github/workflows/main.yml`, let's change it name to `build.yml`.

If you prefer to use your text editor, create a new file named `build.yml` in the `.github/workflows/` directory of your repository.

Add the following content to the file:

```yaml
name: Build and push Docker image

on:
push:
branches:
- main

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push
uses: docker/build-push-action@v6
with:
push: true
tags: ${{ vars.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
```

Each GitHub Actions workflow includes one or several jobs. Each job consists of steps. Each step can either run a set of commands or use already [existing actions](https://github.com/marketplace?type=actions). The action above has three steps:

Check warning on line 82 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.RecommendedWords] Consider using 'previous' instead of 'above' Raw Output: {"message": "[Docker.RecommendedWords] Consider using 'previous' instead of 'above'", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 82, "column": 222}}}, "severity": "INFO"}

1. [**Login to Docker Hub**](https://github.com/docker/login-action): Action logs in to Docker Hub using the Docker ID and Personal Access Token (PAT) you created earlier.

Check warning on line 84 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.RecommendedWords] Consider using 'sign in' instead of 'Login' Raw Output: {"message": "[Docker.RecommendedWords] Consider using 'sign in' instead of 'Login'", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 84, "column": 7}}}, "severity": "INFO"}

2. [**Set up Docker Buildx**](https://github.com/docker/setup-buildx-action): Action sets up Docker [Buildx](https://github.com/docker/buildx), a CLI plugin that extends the capabilities of the Docker CLI.

3. [**Build and push**](https://github.com/docker/build-push-action): Action builds and pushes the Docker image to Docker Hub. The `tags` parameter specifies the image name and tag. The `latest` tag is used in this example.

## 2. Run the workflow

Let's commit the changes, push them to the `main` branch. In the workflow above, the trigger is set to `push` events on the `main` branch. This means that the workflow will run every time you push changes to the `main` branch. You can find more information about the workflow triggers [here](https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows).

Check warning on line 92 in content/guides/python/configure-github-actions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [Docker.RecommendedWords] Consider using 'previous' instead of 'above' Raw Output: {"message": "[Docker.RecommendedWords] Consider using 'previous' instead of 'above'", "location": {"path": "content/guides/python/configure-github-actions.md", "range": {"start": {"line": 92, "column": 75}}}, "severity": "INFO"}

Go to the **Actions** tab of you GitHub repository. It displays the workflow. Selecting the workflow shows you the breakdown of all the steps.

When the workflow is complete, go to your [repositories on Docker Hub](https://hub.docker.com/repositories). If you see the new repository in that list, it means the GitHub Actions workflow successfully pushed the image to Docker Hub.

## Summary

In this section, you learned how to set up a GitHub Actions workflow for your Python application.

Related information:

- [Introduction to GitHub Actions](/guides/gha.md)
- [Docker Build GitHub Actions](/manuals/build/ci/github-actions/_index.md)
- [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)

## Next steps

In the next section, you'll learn how you can develop your application using containers.

5 changes: 3 additions & 2 deletions content/guides/python/containerize.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,6 @@ Related information:

## Next steps

In the next section, you'll learn how you can develop your application using
containers.
In the next section, you'll take a look at how to set up a CI pipeline using GitHub Actions.


4 changes: 2 additions & 2 deletions content/guides/python/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ data:
In your `python-docker-dev-example` directory, create a file named
`docker-python-kubernetes.yaml`. Replace `DOCKER_USERNAME/REPO_NAME` with your
Docker username and the repository name that you created in [Configure CI/CD for
your Python application](./configure-ci-cd.md).
your Python application](./configure-github-actions.md).

```yaml
apiVersion: apps/v1
Expand Down Expand Up @@ -158,7 +158,7 @@ In these Kubernetes YAML file, there are various objects, separated by the `---`
you'll get just one replica, or copy of your pod. That pod, which is
described under `template`, has just one container in it. The
container is created from the image built by GitHub Actions in [Configure CI/CD for
your Python application](configure-ci-cd.md).
your Python application](configure-github-actions.md).
- A Service, which will define how the ports are mapped in the containers.
- A PersistentVolumeClaim, to define a storage that will be persistent through restarts for the database.
- A Secret, Keeping the database password as an example using secret kubernetes resource.
Expand Down
4 changes: 2 additions & 2 deletions content/guides/python/develop.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Use containers for Python development
linkTitle: Develop your app
weight: 20
weight: 40
keywords: python, local, development
description: Learn how to develop your Python application locally.
aliases:
Expand Down Expand Up @@ -569,4 +569,4 @@ Related information:

## Next steps

In the next section, you'll take a look at how to set up a CI/CD pipeline using GitHub Actions.
In the next section, you'll learn how you can locally test and debug your workloads on Kubernetes before deploying.