【问题标题】:How to make a GitHub Actions script publish a package only after direct changes on main or *after* a P.R. gets merged?How to make a GitHub Actions script publish a package only after direct changes on main or *after* a P.R. gets merged?
【发布时间】:2022-11-20 20:47:30
【问题描述】:

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 somethingcloseto 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 isMERGED.

Ido notwant a package to be published if the Pull Request isonly 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?

【问题讨论】:

  • Both conditions 1 and 2 are covered by the push trigger that you already have defined so... yeah, remove pull_request trigger.

标签: package github-actions workflow publish continuous-deployment


【解决方案1】:

You can add an activity type closed onpull_requestin list of event that trigger your workflow, like this:

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

But with that you can only know if your pull request is closed but not if it has been merged. And activity type merged does not exist forpull_request.

Then you can add a filter on a job or a step to execute it only if the pull request is merged

jobs:
  samplejob:
    if: github.event_name == 'pull_request' && github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "This job is triggered when a pull request is merged" 

Ref:

【讨论】:

    猜你喜欢
    • 2016-02-01
    • 2022-12-02
    • 2023-02-25
    • 2022-12-27
    • 2022-12-16
    • 2022-12-19
    • 2022-12-02
    • 2022-12-02
    • 2022-12-01
    相关资源
    最近更新 更多