Giter Site home page Giter Site logo

gitlabci-to-slack's Introduction

Create a new Gitlab Variable SLACK_WEBHOOK with your slack webhook.

Replace <CHANNEL> and other bits for your needs.

touch SUCCESS is a workaround for: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27693 and https://gitlab.com/gitlab-org/gitlab/-/issues/340075

.gitlab-ci.yml example:

stages:
  - build

build:
  image: <IMAGE>
  stage: build
  script:
    - ls -alh
    - touch SUCCESS
  after_script:
    - if [[ -f "SUCCESS" ]]; then EXIT_STATUS=0; else EXIT_STATUS=1; fi
    - source ./slacknotification.sh $SLACK_WEBHOOK

slacknotification.sh example

#!/bin/bash

function print_slack_summary_build() {

if [[ "${EXIT_STATUS}" == 0 ]]; then
        slack_msg_header=":heavy_check_mark: *Build succeeded*"
        else
        slack_msg_header=":x: *Build failed*"
    fi

cat <<-SLACK
            {   "channel": "#<CHANNEL>",
                "blocks": [
                    {
                        "type": "section",
                        "text": {
                            "type": "mrkdwn",
                            "text": "${slack_msg_header}"
                        }
                    },
                    {
                        "type": "divider"
                    },
                    {
                        "type": "section",
                        "fields": [
                            {
                                "type": "mrkdwn",
                                "text": "*Stage:*\n${CI_JOB_NAME}\n"
                            },
                            {
                                "type": "mrkdwn",
                                "text": "*Author:*\n${GITLAB_USER_NAME}\n"
                            },
                            {
                                "type": "mrkdwn",
                                "text": "*Job URL:*\n${CI_JOB_URL}\n"
                            },
                            {
                                "type": "mrkdwn",
                                "text": "*Project Name:*\n${CI_PROJECT_NAME}\n"
                            },
                            {
                                "type": "mrkdwn",
                                "text": "*Commit Branch:*\n${CI_COMMIT_REF_NAME}\n"
                            }
                        ]
                    },
                    {
                        "type": "divider"
                    }
                ]
}
SLACK
}

curl -X POST --data-urlencode "payload=$(print_slack_summary_build)" $1

gitlabci-to-slack's People

Contributors

23ewrdtf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.