GitHub Actions:在作业之间共享通用的操作

9

在从Travis CI切换到GitHub Actions时,我想知道是否有一种方法可以共享作业之间的常见步骤。 对于一个项目,我需要每个作业都以3个操作开始:检查存储库代码,安装Node.js v12,从缓存中恢复node_modules(如果可用)。实际上,我已经为每个作业添加了这3个操作,它们是有效的,但有点冗长。 是否有一种方式可以说:“每个作业必须首先运行这些操作”或类似的内容?

name: ci
on: [push, workflow_dispatch]

jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Cache node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Install dependencies
        run: npm install

  test_mysql:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test MySQL 5
        run: npm run test-mysql
        env:
          DOCKER_MYSQL_TAG: 5

      - name: Test MySQL 8
        run: npm run test-mysql
        env:
          DOCKER_MYSQL_TAG: 8

  test_postgres:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test Postgres 10
        run: npm run test-postgres
        env:
          DOCKER_POSTGRES_TAG: 10

      - name: Test Postgres 11
        run: npm run test-postgres
        env:
          DOCKER_POSTGRES_TAG: 11

      - name: Test Postgres 12
        run: npm run test-postgres
        env:
          DOCKER_POSTGRES_TAG: 12

  test_mariadb:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test MariaDB 10.4
        run: npm run test-mariadb
        env:
          DOCKER_MARIADB_TAG: 10.4.12

  test_mssql:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test MSSQL 2017
        run: npm run test-mssql
        env:
          DOCKER_MSSQL_TAG: 2017-CU17-ubuntu

      - name: Test MSSQL 2019
        run: npm run test-mssql
        env:
          DOCKER_MSSQL_TAG: 2019-latest

  test_sqlite:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test SQLite
        run: npm run test-sqlite

  publish:
    runs-on: ubuntu-latest
    needs: [test_mysql, test_postgres, test_mariadb, test_mssql, test_sqlite]
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Build
        run: npm run build

      - name: Check version changes
        uses: EndBug/version-check@v1
        id: check

      - name: Publish
        if: steps.check.outputs.changed == 'true' && github.ref == 'refs/heads/master'
        run: |
          npm set registry "https://registry.npmjs.org"
          npm set //registry.npmjs.org/:_authToken ${{ secrets.NPM_PUBLISH_TOKEN }}
          npm publish
1个回答

2
现在可以在组合操作中使用uses - 请检查this link
组合操作:
name: "Publish to Docker"
description: "Pushes built artifacts to Docker"

inputs:
  registry_username:
    description: “Username for image registry”
    required: true
  registry_password:
    description: “Password for image registry”
    required: true

runs:
  using: "composite"
  steps:
      - uses: docker/setup-buildx-action@v1

      - uses: docker/login-action@v1
        with:
          username: ${{inputs.registry_username}}
          password: ${{inputs.registry_password}}

      - uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: user/app:latest

然后:
on: [push]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: my-org/publish-docker@v1
        with:
          registry_username: ${{secrets.REGISTRY_USERNAME}}
          registry_password: ${{secrets.REGISTRY_PASSWORD}}

原回复:

目前看来,如果您想要共享的步骤中包含uses,则不可能。

这应该由组合操作处理。

目前复合运行步骤支持什么?

对于复合操作中的每个运行步骤,我们支持:

  • 名称
  • id
  • run
  • env
  • shell
  • 工作目录

此外,我们支持在整个操作中映射输入和输出。

有关更多信息,请参阅文档。

复合运行步骤不支持什么

我们目前不支持在复合操作的各个步骤中设置条件、继续执行错误、超时分钟、"uses"和密码。


有趣,也许他们将来会支持用户。谢谢! - revy
我希望他们很快就会添加这个功能!我知道这不是你想要得到的答案,但由于技术限制,请考虑接受这个答案,如果没有人给出更好的建议。 - Krzysztof Madej
现在,复合操作接受 uses:。请参阅 https://github.blog/changelog/2021-08-25-github-actions-reduce-duplication-with-action-composition/。 - Jonas Eberle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接