github action如何捕获错误,上报action错误告警

github的action提供了持续集成和持续交付的能力。

想在action流程失败时给予告警,github默认提供的方式是可以推送给邮箱。不过大部分人对于邮件的响应速度应该不会太快。所以在action step设置错误告警,则是比较方便快捷了。

默认情况下action 会扫描所有的step,错误则跳过,可以在stage 最后的step里面设置捕获错误
捕获方式:if: ${{ failure() }}

例:

jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!
        
      - name: Step1
        run: |
          echo "${{ github.repository }}"
          echo "${{ github.head_ref }}"
      - name: Step2
        run: |
          go run main.go
      - name: Step3
        run: |
          echo 2222
      - name: Step4 //设置对应的告警
        if: ${{ failure() }}
        run: |
          author=$(git show -s --format=%an)
          curl --location --request POST xxxx
	

如上述代码所示,我们在Step2特意执行报错(没有main.go文件),则Step3会跳过,最终Step4判断出错了,则执行curl(想要的告警)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值