69

Can I have multiple workflow files?

I have a few repo's utilizing GitHub Actions and they work great! In my specific use case, I auto-deploy to my dev environment on "push," and I auto-deploy to production on "release." These are two separate workflows.

I know I can have these two workflows in the same main.workflow file and that would work just fine, but I would prefer to have them in separate workflow files completely. Would it be possible for example to have a dev.workflow file, and a prod.workflow file?

I have tried creating a dev.workflow and prod.workflow file, but they don't seem to be picked up by Actions. It appears a main.workflow file is required. If that is the case, is there a way to source other workflow files into the main.workflow?

ItsKarma
  • 840
  • 1
  • 6
  • 8

3 Answers3

90

You can have multiple files in the .github/workflows folder. All files will be read and run as independent tests. The 'on' parameter on each file will tell when it must be called.

Following your idea you could have:

dev.workflow.yml - To run some kind of testing maybe (only on dev branch, when push)

name: Dev Workflow - Test and check thing
on:
  push:
    branches:
      - dev
jobs:
  ...

prod.workflow.yml- To build and deploy your project (only on master branch, when a PR is closed)

name: Master Workflow - Build and deploy to production
on:
  pull_request:
    types: closed
    branches:
      - master
jobs:
  ...
Saurabh
  • 5,176
  • 4
  • 32
  • 46
Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
49

Since this question was asked, GitHub has made a few changes to workflows. They are now written in YAML syntax rather than HCL, and instead of being stored in a .github/main.workflow file, they are stored in a .github/workflows directory. The documentation says that "You must store workflow files" (note the plural) "in the .github/workflows directory of your repository."

So once you port your main.workflow file to YAML syntax, you should be able to store each workflow in a single file the way you wanted.

rmunn
  • 34,942
  • 10
  • 74
  • 105
  • could you help please https://stackoverflow.com/questions/72276452/run-github-actions-in-monorepo?noredirect=1#comment127692674_72276452 – Asking May 17 '22 at 17:16
0

Yes you can have multiple yml files under the workflow directory, and you can also have all of them have the same condition so they could be run parallelly. However it's not recommended. as to the answer from [https://github.community/t/execution-order-for-multiple-workflows-in-one-repo/116780]

Every workflow needs an event trigger defined in the yaml file, if your operation triggers the event, the workflow will be started, they can be in parallel.

star
  • 691
  • 7
  • 12