简体   繁体   English

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

[英]How do I convert Travis CI configuration to GitHub Actions and test with several versions of Python?

We have been using Travis CI for about 5 years now, but we have problems recently and their support doesn't help.我们已经使用 Travis CI 大约 5 年了,但我们最近遇到了问题,他们的支持无济于事。 I'm thinking about using GitHub Actions instead.我正在考虑改用 GitHub Actions。 We use Travis CI to test our code with 4 versions of Python (3.6 to 3.9), and I want to do the same with GitHub Actions.我们使用 Travis CI 用 4 个版本的 Python(3.6 到 3.9)测试我们的代码,我想用 GitHub Actions 做同样的事情。 Here is our .travis.yml file:这是我们的.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

I read about GitHub Actions and I'm trying to create a file .github/workflows/main.yml , but I don't know how to define it.我阅读了 GitHub Actions 并且我正在尝试创建一个文件.github/workflows/main.yml ,但我不知道如何定义它。 How do I define this file to run tests with GitHub Actions?如何定义此文件以使用 GitHub Actions 运行测试? In general I want to convert our .travis.yml configuration to GitHub Actions.一般来说,我想将我们的.travis.yml配置转换为 GitHub Actions。

This is the .github/workflows/main.yml file I'm starting with:这是我开始的.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 versions 1. Python 版本

.travis.yml .travis.yml

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

.github/workflows/main.yml .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 }}

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

2. PostgreSQL service 2. PostgreSQL 服务

Note the trust requirements are specific to your project.请注意, trust要求特定于您的项目。
From https://hub.docker.com/_/postgres , for other readers:来自https://hub.docker.com/_/postgres ,对于其他读者:

Note 1: It is not recommended to use trust since it allows anyone to connect without a password, even if one is set (like via POSTGRES_PASSWORD ).注意 1:不建议使用trust因为它允许任何人在没有密码的情况下进行连接,即使设置了密码(例如通过POSTGRES_PASSWORD )。 For more information see the PostgreSQL documentation on Trust Authentication .有关更多信息,请参阅有关Trust Authentication的 PostgreSQL 文档。

PostgreSQL 9.6 PostgreSQL 9.6

.travis.yml .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 .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 PostgreSQL 12

.github/workflows/main.yml .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 PostgreSQL 13

.github/workflows/main.yml .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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM