2

I have the following two workflows:

Now when I push my code, both workflows started. I want deployment Work to only start once Test Suite gets passed.

How can I do this?

Workflow that runs tests:

name: CI
on:
  push:
    branches: [setup_github]
jobs:
  test:
    runs-on: ubuntu-18.04
    services:
      postgres:
        image: postgres:10
    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Set up Ruby
      uses: ruby/setup-ruby@v1
      with:
        ruby-version:  2.5.3
    - uses: borales/actions-yarn@v2.0.2
      with:
        cmd: install
    - name: Install Dependencies
      run: |
        sudo apt-get -yqq install libpq-dev
    - name: Install Gems
      run: |
        gem install bundler
    - name: prepare Database
    - name: RSpec
      run: |
        bundle exec rspec specs

Workflow that deploys:

name: Deploy on server
on:
  push:
    branches:
    - setup_github
jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v1
    - uses: ruby/setup-ruby@v1
      with:
        ruby-version: 2.5.3
        bundler-cache: true
    - uses: miloserdow/capistrano-deploy@master
      with:
        target: staging
        deploy_key: ${{ secrets.DEPLOY_ENC_KEY }}
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
kashif
  • 1,097
  • 4
  • 17
  • 32
  • 3
    Why not make them two jobs in one workflow so you can use [`jobs..needs`](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds)? Watch out for e.g. https://stackoverflow.com/q/64856287/3001761. – jonrsharpe Nov 22 '20 at 08:38
  • I have this same issue. For me, the primary reason for them to be separate is because the tests should run on all commits, while deployments should only happen on commits to certain branches (or if I were using GitHub releases, I might only want deployments triggered by release events). Unfortunately, I haven't found a good way to solve this yet. – Ian Greenleaf Young Dec 11 '20 at 21:22
  • Does this answer your question? [Only run job on specific branch with GitHub Actions](https://stackoverflow.com/questions/58139406/only-run-job-on-specific-branch-with-github-actions) – Ian Greenleaf Young Dec 11 '20 at 21:38
  • In 2021 there's `workflow_run`: https://stackoverflow.com/a/69743847 – adiabatic Jan 03 '23 at 00:03

1 Answers1

2

Your goal should be achievable by ensuring the following are true:

  • The CI workflow has run
  • The CI workflow was success
name: Deploy on server
on:
  workflow_run:
      workflows: [CI]
      branches: [setup_github]
      types:
        - completed
jobs:
  deploy:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/checkout@v1
    - uses: ruby/setup-ruby@v1
      with:
        ruby-version: 2.5.3
        bundler-cache: true
    - uses: miloserdow/capistrano-deploy@master
      with:
        target: staging
        deploy_key: ${{ secrets.DEPLOY_ENC_KEY }}

This is described in the Github Actions docs on workflow_run.

alukach
  • 5,921
  • 3
  • 39
  • 40