【问题标题】:GitHub Actions: share common actions between jobsGitHub Actions:在作业之间共享常用操作
【发布时间】:2021-04-10 17:00:29
【问题描述】:

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

【问题讨论】:

    标签: github continuous-integration travis-ci github-actions


    【解决方案1】:

    现在可以在复合动作中使用用途 - 请查看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,则目前是不可能的。

    应该由composite actions but处理

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

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

    • 姓名
    • 身份证
    • 运行
    • 环境
    • 外壳
    • 工作目录

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

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

    复合运行步骤不支持什么

    我们目前不支持在复合操作中的各个步骤上设置条件、错误继续、超时分钟、“使用”和机密。

    【讨论】:

    • 有趣,也许他们将来也会支持使用。谢谢!
    • 我希望他们会尽快添加!我知道这不是您想要得到的答案,但由于存在技术限制,如果没有人给您更好的主意,请考虑接受此答案。
    • 复合动作接受uses: 现在github.blog/changelog/…
    【解决方案2】:

    我看了你的工作。他们每个人都有needs: setup。这不是为每个作业运行setup 中的步骤吗?

    【讨论】:

    • test* 作业需要并行运行
    猜你喜欢
    • 2020-07-27
    • 2019-12-21
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多