如何将Travis CI配置转换为GitHub Actions并使用几个版本的Python进行测试



我们已经使用Travis CI大约5年了,但最近我们遇到了问题,他们的支持也无济于事。我正在考虑使用GitHub Actions。我们使用Travis CI用4个版本的Python(3.6到3.9(测试我们的代码,我想用GitHub Actions做同样的事情。这是我们的.travis.yml文件:

language: python
python:
- "3.6"
- "3.7"
- "3.8"
- "3.9"
sudo: required
dist: bionic
addons:
postgresql: "9.6"
services:
- postgresql
before_script:
- psql --version
- psql -c 'create user pguser;' -U postgres
- psql -c 'alter user pguser createdb; ' -U postgres
install:
- cp env.ini.tests env.ini
- pip install --upgrade -r requirements.txt
- pip freeze
- pwd
- lsb_release -a
script:
- cd speedy/net
- python tests_manage.py test # python -W error::DeprecationWarning tests_manage.py test
- cd ../match
- python tests_manage.py test # python -W error::DeprecationWarning tests_manage.py test
- cd ../composer
- python tests_manage.py test # python -W error::DeprecationWarning tests_manage.py test
- cd ../mail
- python tests_manage.py test # python -W error::DeprecationWarning tests_manage.py test

我读过关于GitHub Actions的文章,我正在尝试创建一个文件.github/workflows/main.yml,但我不知道如何定义它。我如何定义这个文件来使用GitHub Action运行测试?一般来说,我想将我们的.travis.yml配置转换为GitHub Actions。

这是我开始使用的.github/workflows/main.yml文件:

name: GitHub Actions Speedy Net
on: [push]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v2
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."

1。Python版本

.travis.yml

language: python
python:
- "3.6"
- "3.7"
- "3.8"
- "3.9"

.github/workflows/main.yml

jobs:
tests:
...
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
steps:
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

参考:https://docs.github.com/en/actions/guides/building-and-testing-python

2。PostgreSQL服务

请注意,trust要求是特定于您的项目的
发件人https://hub.docker.com/_/postgres,对于其他读者:

注意1:不建议使用trust,因为它允许任何人在没有密码的情况下进行连接,即使设置了密码(如通过POSTGRES_PASSWORD(。有关更多信息,请参阅PostgreSQL关于信任验证的文档。

postgresql 9.6

.travis.yml

addons:
postgresql: "9.6"
services:
- postgresql
before_script:
- psql --version
- psql -c 'create user pguser;' -U postgres
- psql -c 'alter user pguser createdb; ' -U postgres

.github/workflows/main.yml

jobs:
tests:
...
steps:
...
- run: |
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql-9.6
sudo sed -i 's/local   all             postgres                                peer/local   all             postgres                                trust/' /etc/postgresql/9.6/main/pg_hba.conf
sudo sed -i 's|host    all             all             127.0.0.1/32            md5|host    all             all             127.0.0.1/32            trust|' /etc/postgresql/9.6/main/pg_hba.conf
! docker stop $(docker ps -q --filter ancestor=postgres)
sudo sed -i 's/port = 5433/port = 5432/' /etc/postgresql/9.6/main/postgresql.conf
sudo service postgresql start
- run: psql --version
- run: |
psql -c 'create user pguser;' -U postgres
psql -c 'alter user pguser createdb; ' -U postgres

postgresql 12.github/workflows/main.yml

jobs:
tests:
...
steps:
...
- run: |
sudo apt-get update
sudo apt-get -y install postgresql
sudo sed -i 's/local   all             postgres                                peer/local   all             postgres                                trust/' /etc/postgresql/12/main/pg_hba.conf
sudo sed -i 's|host    all             all             127.0.0.1/32            md5|host    all             all             127.0.0.1/32            trust|' /etc/postgresql/12/main/pg_hba.conf
! docker stop $(docker ps -q --filter ancestor=postgres)
sudo sed -i 's/port = 5433/port = 5432/' /etc/postgresql/12/main/postgresql.conf
sudo service postgresql start
- run: psql --version
- run: |
psql -c 'create user pguser;' -U postgres
psql -c 'alter user pguser createdb; ' -U postgres

postgresql 13.github/workflows/main.yml

jobs:
tests:
...
services:
postgres:
image: postgres
env:
POSTGRES_HOST_AUTH_METHOD: trust
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
...
- run: psql --version
- run: |
psql -c 'create user pguser;' -U postgres
psql -c 'alter user pguser createdb; ' -U postgres
env:
PGHOST: 127.0.0.1

参考:https://docs.github.com/en/actions/guides/creating-postgresql-service-containers

相关内容

  • 没有找到相关文章

最新更新