PostgreSQL GitHub Actions 使用自定义容器镜像连接Postgres 服务
在本文中,我们将介绍如何使用GitHub Actions连接PostgreSQL服务并使用自定义容器镜像进行开发和测试。
阅读更多:PostgreSQL 教程
什么是GitHub Actions
GitHub Actions是GitHub提供的一项功能强大的自动化工作流服务,可以在软件开发过程中自动执行各种任务。通过编写和配置工作流文件,我们可以在代码推送、拉取请求、分支创建等事件触发时自动运行一系列自定义任务。
GitHub Actions中使用PostgreSQL服务
在软件开发过程中,经常需要使用数据库服务来存储和管理数据。通过利用GitHub Actions和PostgreSQL服务,我们可以轻松地在自动化工作流中连接、创建、填充和清理数据库。
首先,我们需要在GitHub仓库的.github/workflows
目录中创建一个新的工作流文件,命名为postgres.yml
。接下来,我们将在该文件中编写我们的工作流配置。
在开始之前,我们需要先在GitHub仓库的Settings -> Secrets
页面中添加PostgreSQL连接所需的凭据。我们可以通过添加如下几个密钥:
– DB_HOST
:PostgreSQL主机地址
– DB_PORT
:PostgreSQL端口号
– DB_NAME
:要连接的数据库名称
– DB_USER
:数据库用户名
– DB_PASSWORD
:数据库密码
现在,我们可以开始编写工作流文件的配置。以下是一个示例:
name: Postgres Workflow
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: {{ secrets.DB_USER }}
POSTGRES_PASSWORD:{{ secrets.DB_PASSWORD }}
POSTGRES_DB: {{ secrets.DB_NAME }}
ports:
-{{ secrets.DB_PORT }}:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
- name: Install Dependencies
run: npm install
- name: Run Tests
env:
DB_HOST: localhost
DB_NAME: {{ secrets.DB_NAME }}
DB_USER:{{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: npm test
在上述示例配置中,我们定义了一个名为Postgres Workflow
的工作流。它包含一个build
作业,该作业定义了一系列步骤。
首先,我们使用了services
关键字来定义一个名为postgres
的服务,它使用了postgres:latest
镜像作为PostgreSQL数据库。我们通过将secrets
中的凭据传递给env
来配置数据库的必要参数。
接下来,我们通过actions/checkout
操作将代码检出到工作区。然后,使用actions/setup-node
将Node.js环境设置为14版本,并使用npm install
安装依赖。
最后,我们在npm test
命令执行之前设置了一些环境变量,包括连接到PostgreSQL数据库所需的凭据。
自定义容器镜像连接Postgres服务
除了使用默认的PostgreSQL镜像,GitHub Actions还支持使用自定义容器镜像进行开发和测试。
假设我们的应用程序需要使用某些自定义依赖项或特定的PostgreSQL扩展。我们可以创建一个自定义的Docker容器镜像,其中包含了我们需要的所有依赖项和扩展。
以下是一个示例Dockerfile:
FROM postgres:latest
RUN apt-get update && apt-get install -y --no-install-recommends \
postgresql-contrib \
&& rm -rf /var/lib/apt/lists/*
COPY my_extension.sql /docker-entrypoint-initdb.d/
在上述示例中,我们通过继承postgres:latest
镜像创建了一个自定义的镜像。然后,我们在容器中安装了postgresql-contrib
包,这是一个常用的PostgreSQL扩展。最后,我们将自定义的SQL脚本my_extension.sql
复制到了/docker-entrypoint-initdb.d/
目录下,以在容器启动时自动执行。
在GitHub Actions的工作流配置中使用自定义容器镜像,只需修改services
部分的配置即可,将image
修改为我们自定义的镜像名称。
总结
通过GitHub Actions和PostgreSQL服务,我们可以轻松地集成数据库操作到自动化工作流中。通过对工作流配置的灵活调整,我们可以使用默认的PostgreSQL镜像或自定义的容器镜像来满足开发和测试的需求。希望本文对您在使用GitHub Actions和连接PostgreSQL服务方面有所帮助。
PostgreSQL GitHub Actions to Connect Postgres service with custom container image
In this article, we will explore how to use GitHub Actions to connect a PostgreSQL service and use a custom container image for development and testing purposes.
What is GitHub Actions
GitHub Actions is a powerful workflow automation service provided by GitHub that allows developers to automate various tasks during the software development process. By writing and configuring workflow files, we can define custom tasks to be automatically executed based on events such as code pushes, pull requests, and branch creations.
Using PostgreSQL service in GitHub Actions
A common requirement during software development is the need for a database service to store and manage data. By leveraging GitHub Actions and PostgreSQL service, we can easily connect, create, populate, and clean up databases within our automated workflows.
To start, we need to create a new workflow file named postgres.yml
in the .github/workflows
directory of our GitHub repository. We will then write our workflow configuration within this file.
Before we begin, we need to add the necessary credentials for connecting to PostgreSQL in the Settings -> Secrets
page of our GitHub repository. We can add the following secrets:
– DB_HOST
: PostgreSQL host address
– DB_PORT
: PostgreSQL port number
– DB_NAME
: Name of the database to connect
– DB_USER
: Database username
– DB_PASSWORD
: Database password
Now, we can start writing our workflow file configuration. Here’s an example:
name: Postgres Workflow
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: {{ secrets.DB_USER }}
POSTGRES_PASSWORD:{{ secrets.DB_PASSWORD }}
POSTGRES_DB: {{ secrets.DB_NAME }}
ports:
-{{ secrets.DB_PORT }}:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
- name: Install Dependencies
run: npm install
- name: Run Tests
env:
DB_HOST: localhost
DB_NAME: {{ secrets.DB_NAME }}
DB_USER:{{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: npm test
In the above example configuration, we define a workflow named Postgres Workflow
which includes a build
job that consists of a series of steps.
First, we use the services
keyword to define a service named postgres
which uses the postgres:latest
image as the PostgreSQL database. We configure the necessary parameters for the database by passing the credentials from secrets
to the env
field.
Next, we check out the code to the workspace using the actions/checkout
action. Then, we set up the Node.js environment to version 14 using the actions/setup-node
action and install dependencies using npm install
.
Finally, we set some environment variables before the npm test
command is run, including the credentials required to connect to the PostgreSQL database.
Connecting with a Custom Container Image
In addition to using the default PostgreSQL image, GitHub Actions also supports using custom container images for development and testing purposes.
Let’s assume our application requires some custom dependencies or specific PostgreSQL extensions. We can create a custom Docker container image that includes all the required dependencies and extensions.
Here’s an example Dockerfile:
FROM postgres:latest
RUN apt-get update && apt-get install -y --no-install-recommends \
postgresql-contrib \
&& rm -rf /var/lib/apt/lists/*
COPY my_extension.sql /docker-entrypoint-initdb.d/
In the above example, we create a custom image by inheriting from the postgres:latest
image. Then, we install the postgresql-contrib
package, which is a commonly used PostgreSQL extension, within the container. Finally, we copy a custom SQL script, my_extension.sql
, to the /docker-entrypoint-initdb.d/
directory to be automatically executed during container startup.
To use a custom container image in the GitHub Actions workflow configuration, we just need to modify the image
value in the services
section to the name of our custom image.
Summary
By combining GitHub Actions and the PostgreSQL service, we can easily integrate database operations into our automated workflows. With flexibility in the workflow configuration, we can choose between the default PostgreSQL image or a custom container image based on our development and testing needs. We hope this article has provided you with guidance on using GitHub Actions to connect to a PostgreSQL service with a custom container image.