본문 바로가기
DevOps

Slack Notification

by 이강복 2023. 10. 31.

Slack

https://워크스페이스.slack.com/apps 에 접속하여 Jenkins Ci 앱 설치

Jenkins Ci 설정 지침 단계에 따라 구성

Jenkins

Plugins → slack notification 설치

시스템 → Slack 설정 (workspace & token)

podTemplate(yaml: '''
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: alpine
        image: alpine/git:latest
        command:
        - sleep
        args:
        - 99d
      - name: kaniko
        image: gcr.io/kaniko-project/executor:debug
        command:
        - sleep
        args:
        - 99d
        volumeMounts:
        - name: kaniko-secret
          mountPath: /kaniko/.docker
      restartPolicy: Never
      volumes:
      - name: kaniko-secret
        secret:
          secretName: docker-config-secret
          items:
            - key: .dockerconfigjson
              path: config.json
''') {

    node(POD_LABEL) {
        try {
            stage("Set Variable") {
                git url: 'https://github.com/kangbock/kaniko.git', branch: 'main'
                script(){
                    env.GIT_COMMIT = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
                    GIT_TAG = sh (script: 'git describe --always', returnStdout: true).trim();
                    SLACK_CHANNEL = "#devops";
                    SLACK_SUCCESS_COLOR = "#2C953C";
                    SLACK_FAIL_COLOR = "#FF3232";
                    // Git Commit 계정
                    GIT_COMMIT_AUTHOR = sh(script: "git --no-pager show -s --format=%an ${env.GIT_COMMIT}", returnStdout: true).trim();
                    // Git Commit 메시지
                    GIT_COMMIT_MESSAGE = sh(script: "git --no-pager show -s --format=%B ${env.GIT_COMMIT}", returnStdout: true).trim();
                }
            }
            slackSend (
                channel: SLACK_CHANNEL,
                color: SLACK_SUCCESS_COLOR,
                message: "========================================\\n The ${env.JOB_NAME}(${env.BUILD_NUMBER}) pipeline has started.\\n\\n          Author : ${GIT_COMMIT_AUTHOR} \\n          Commit Message : ${GIT_COMMIT_MESSAGE}\\n\\n${env.BUILD_URL}"
            )
        } catch(git) {
                slackSend (
                    channel: SLACK_CHANNEL,
                    color: SLACK_SUCCESS_COLOR,
                    message: "========================================\\n${env.JOB_NAME}(${env.BUILD_NUMBER}) pipeline failed.\\n\\n          Author : ${GIT_COMMIT_AUTHOR} \\n          Commit Message : ${GIT_COMMIT_MESSAGE}\\n\\n${env.BUILD_URL}"
                )
            throw git;
        }
        

        stage('Kaniko Build and Push') {
            git url: 'https://github.com/kangbock/kaniko.git', branch: 'main'
            
            container('kaniko') {
                try {
                    stage('nginx') {
                        sh '/kaniko/executor -f `pwd`/nginx/Dockerfile -c `pwd` --insecure --cache=true --destination=harbor.k-tech.cloud/kaniko/nginx:${BUILD_NUMBER}'
                    }
                    slackSend (
                        channel: SLACK_CHANNEL,
                        color: SLACK_SUCCESS_COLOR,
                        message: "NGINX Image Deployment and Push Successfully."
                    )
                    stage('nginx') {
                        sh '/kaniko/executor -f `pwd`/nodejs/Dockerfile -c `pwd` --insecure --cache=true --destination=harbor.k-tech.cloud/kaniko/nodejs:${BUILD_NUMBER}'
                    }
                    slackSend (
                        channel: SLACK_CHANNEL,
                        color: SLACK_SUCCESS_COLOR,
                        message: "NodeJS Image Deployment and Push Successfully."
                    )
                } catch(Build) {
                    slackSend (
                        channel: SLACK_CHANNEL,
                        color: SLACK_FAIL_COLOR,
                        message: "Image deployment and push failed."
                    )
                    throw Build;
                }
            }
            
            git url: 'https://github.com/kangbock/ArgoCD.git', branch: 'main'
            container('alpine') {
                try {
                    stage('GitHub Push') {
                        sh 'sed -i s/newTag:.*/newTag:${BUILD_NUMBER}/g ./overlays/dev/kustomization.yaml'
                        sh 'sed -i s/nginx:.*/nginx:${BUILD_NUMBER}/g ./base/n1.yaml'
                        sh 'sed -i s/nodejs:.*/nodejs:${BUILD_NUMBER}/g ./base/j1.yaml'
                        sh 'git config --global --add safe.directory /home/jenkins/agent/workspace/test'
                        sh 'git config --global user.email \\'kangbock@naver.com\\''
                        sh 'git config --global user.name \\'kangbock\\''
                        sh 'git add ./'
                        sh 'git commit -a -m "updated the image tag to ${BUILD_NUMBER}" || true'
                        sh 'git remote add kb97 https://[GitHub_Token]@github.com/kangbock/ArgoCD.git'
                        sh 'git push -u kb97 main'
                    }
                    slackSend (
                        channel: SLACK_CHANNEL,
                        color: SLACK_SUCCESS_COLOR,
                        message: "Deployment was successful.\\n========================================"
                    )
                } catch(push) {
                    slackSend (
                        channel: SLACK_CHANNEL,
                        color: SLACK_FAIL_COLOR,
                        message: "Deployment failed.\\n========================================"
                    )
                    throw push;
                }
            }
        }

    }
}

'DevOps' 카테고리의 다른 글

Istio  (0) 2023.11.13
Harbor (cert-manager)  (0) 2023.10.13
FortiGate 방화벽의 SNMP 를 이용한 Grafana Dashboard  (0) 2023.10.10
Jenkins + Argo CD (kaniko, harbor, cert-manager)  (0) 2023.08.11
Dapr with AKS  (0) 2023.06.09