Search code examples
packagegithub-actionsworkflowpublishcontinuous-deployment

How to make a GitHub Actions script publish a package only after direct changes on main or *after* a P.R. gets merged?


I am trying to build a Continuous Deployment workflow via GitHub Actions.

As a background context, this is a Clojure/ClojureScript project - specifically, a dependency on a dynamic web app.

As the outcome of the CD workflow, I want to have Maven packages published on GitHub packages after every time the file project.clj is changed.

Why this file? Because it holds the project version! Usually, when someone edit this file it is because it is a new version. Hence, it makes sense for a new version to be automatically published as a dependency.

Ok. I have achieved something close to what I want. Packages have been automatically published!

However, they are being published even when someone JUST submits a Pull Request.

I want the package to be published (CD to be triggered) on the following conditions:

1 - after direct changes on main branch; or,

2 - after a Pull Request is MERGED.

I do not want a package to be published if the Pull Request is only submitted.

This is my cd.yml file:

name: 'cd'

on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - 'project.clj'
  pull_request:
    paths:
      - 'project.clj'

What do I need to change on the workflow dispatch?

Only removing the last 3 lines will do the trick?


Solution

  • As hinted by my own questions and endorsed by user @tmt, removing the last 3 lines worked out as expected:

    name: 'cd'
    
    on:
      workflow_dispatch:
      push:
        branches:
          - main
        paths:
          - 'project.clj'