diff --git a/.github/workflows/network-explorer.yml b/.github/workflows/network-explorer.yml index d187888708..97ebdfcbf0 100644 --- a/.github/workflows/network-explorer.yml +++ b/.github/workflows/network-explorer.yml @@ -40,23 +40,19 @@ jobs: REMOTE_USER: ${{ secrets.CI_WWW_REMOTE_USER }} TARGET: ${{ secrets.CI_WWW_REMOTE_TARGET }}/${{ env.GITHUB_REF_SLUG }} EXCLUDE: "/dist/, /node_modules/" - - name: Setup message creation - run: npm install -g hbs-cli - - name: Setup message data - run: | - echo "{ \"github\": ${{ toJSON(github) }}, \"env\": ${{ toJSON(env) }}, \"secrets\": { \"NYM_CI_WWW_BASE\": \"${{ secrets.NYM_CI_WWW_BASE }}\" } }" > message_data.json - - name: Create success message - if: ${{ success() }} - run: hbs --data message_data.json --stdout .github/workflows/support-files/messages/success > message.txt - - name: Create failure message - if: ${{ failure() }} - run: hbs --data message_data.json --stdout .github/workflows/support-files/messages/failure > message.txt - - name: Keybase Notification - uses: docker://keybaseio/client:stable + - name: Keybase - Node Install + run: npm install + working-directory: .github/workflows/support-files/messages + - name: Keybase - Send Notification env: - KEYBASE_USERNAME: "${{ secrets.KEYBASE_NYMBOT_USERNAME }}" - KEYBASE_PAPERKEY: "${{ secrets.KEYBASE_NYMBOT_PAPERKEY }}" - KEYBASE_SERVICE: "1" + NYM_PROJECT_NAME: "Network Explorer" + NYM_CI_WWW_BASE: "${{ secrets.NYM_CI_WWW_BASE }}" + GIT_COMMIT_MESSAGE: "${{ github.event.head_commit.message }}" + GIT_BRANCH: "${GITHUB_REF##*/}" + KEYBASE_NYMBOT_USERNAME: "${{ secrets.KEYBASE_NYMBOT_USERNAME }}" + KEYBASE_NYMBOT_PAPERKEY: "${{ secrets.KEYBASE_NYMBOT_PAPERKEY }}" + KEYBASE_NYMBOT_TEAM: "${{ secrets.KEYBASE_NYMBOT_TEAM }}" + IS_SUCCESS: "${{ job.status == 'success' }}" + uses: docker://keybaseio/client:stable-node with: - args: | - cat message.txt | keybase chat send --channel "#dev-product-ci" ${{ secrets.KEYBASE_NYMBOT_TEAM }} + args: .github/workflows/support-files/messages/entry_point_notifications.sh diff --git a/.github/workflows/support-files/messages/.gitignore b/.github/workflows/support-files/messages/.gitignore new file mode 100644 index 0000000000..34977ee7df --- /dev/null +++ b/.github/workflows/support-files/messages/.gitignore @@ -0,0 +1,2 @@ +node_modules +.idea \ No newline at end of file diff --git a/.github/workflows/support-files/messages/.prettierrc b/.github/workflows/support-files/messages/.prettierrc new file mode 100644 index 0000000000..0238bf0f00 --- /dev/null +++ b/.github/workflows/support-files/messages/.prettierrc @@ -0,0 +1,6 @@ +{ + "trailingComma": "all", + "singleQuote": true, + "printWidth": 80, + "tabWidth": 2 +} diff --git a/.github/workflows/support-files/messages/entry_point_notifications.sh b/.github/workflows/support-files/messages/entry_point_notifications.sh new file mode 100755 index 0000000000..04607fb979 --- /dev/null +++ b/.github/workflows/support-files/messages/entry_point_notifications.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +# pass exit codes out to GitHub Actions +set -euxo pipefail + +# change to the directory that contains this script +cd "${0%/*}" + +# run the node script +node send_message.js \ No newline at end of file diff --git a/.github/workflows/support-files/messages/failure b/.github/workflows/support-files/messages/failure index cb16004bb0..adc2896ace 100644 --- a/.github/workflows/support-files/messages/failure +++ b/.github/workflows/support-files/messages/failure @@ -1,9 +1,11 @@ 🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥 -> :rocket: Network Explorer +> :rocket: {{ env.NYM_PROJECT_NAME }} > 🔴 **FAILURE** :cry: -> `{{ github.sha }}` `{{ github.ref }}` https://{{ github.server_url }}/{{ github.repository }}/tree/{{ github.ref }} -> `build job` https://{{ github.server_url }}/{{ github.repository }}/actions/runs/{{ github.run_id }} +> `branch` {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/tree/{{ env.GIT_BRANCH_NAME }} +> `commit` {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/commit/{{ env.GITHUB_SHA }} +> `build ` {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/actions/runs/{{ env.GITHUB_RUN_ID }} Commit message: ``` -{{ github.event.head_commit.message }} \ No newline at end of file +{{ env.GIT_COMMIT_MESSAGE }} +``` diff --git a/.github/workflows/support-files/messages/package.json b/.github/workflows/support-files/messages/package.json new file mode 100644 index 0000000000..5e9ec33e9e --- /dev/null +++ b/.github/workflows/support-files/messages/package.json @@ -0,0 +1,16 @@ +{ + "name": "send-keybase-message", + "description": "Sends a notification message with the keybase package that fails when piped into the keybase CLI", + "version": "1.0.0", + "private": true, + "scripts": { + "format": "prettier --write send_message.js" + }, + "dependencies": { + "handlebars": "^4.7.7", + "keybase-bot": "^3.6.1" + }, + "devDependencies": { + "prettier": "2.3.2" + } +} diff --git a/.github/workflows/support-files/messages/send_message.js b/.github/workflows/support-files/messages/send_message.js new file mode 100644 index 0000000000..f1b37f5513 --- /dev/null +++ b/.github/workflows/support-files/messages/send_message.js @@ -0,0 +1,65 @@ +const Bot = require('keybase-bot'); +const Handlebars = require('handlebars'); +const fs = require('fs'); + +async function main() { + const data = { env: process.env }; + // const data = { ...PASTE TEST DATA HERE ... }; // -- DEV: uncomment to set test data + + // validation of environment + if(!(process.env.NYM_PROJECT_NAME || data.env.NYM_PROJECT_NAME)) { + throw new Error('Please set env var NYM_PROJECT_NAME with the project name for displaying in notification messages'); + } + + // extract the git branch name + const GIT_BRANCH_NAME = (process.env.GITHUB_REF || data.env.GITHUB_REF).split('/').slice(2).join('/'); + + data.env.GIT_BRANCH_NAME = GIT_BRANCH_NAME; + const source = fs + .readFileSync(process.env.IS_SUCCESS === 'true' ? 'success' : 'failure') + .toString(); + const template = Handlebars.compile(source); + const result = template(data); + + // -- DEV: uncomment to show what is available in the handlebars template / show the result + // console.dir({ data }, { depth: null }); + // console.log(result); + + const bot = new Bot(); + try { + const username = process.env.KEYBASE_NYMBOT_USERNAME; + const paperkey = process.env.KEYBASE_NYMBOT_PAPERKEY; + + if(!username) { + throw new Error('Username is not defined. Please set env var KEYBASE_NYMBOT_USERNAME'); + } + if(!paperkey) { + throw new Error('Paperkey is not defined. Please set env var KEYBASE_NYMBOT_PAPERKEY'); + } + + console.log(`Initialising keybase with user "${username}" and key: "${'*'.repeat(paperkey.length)}"...`); + await bot.init(username, paperkey, { verbose: false }); + + const channel = { + name: 'nymtech_bot', + membersType: 'team', + topicName: 'testing', + topic_type: 'CHAT', + }; + const message = { + body: result, + }; + + console.log(`Sending to ${channel.name}#${channel.topicName}...`); + await bot.chat.send(channel, message); + + console.log('Message sent!'); + } catch (error) { + console.error(error); + process.exitCode = -1; + } finally { + await bot.deinit(); + } +} + +main(); diff --git a/.github/workflows/support-files/messages/success b/.github/workflows/support-files/messages/success index d41dbdce6c..a5e33f0feb 100644 --- a/.github/workflows/support-files/messages/success +++ b/.github/workflows/support-files/messages/success @@ -1,10 +1,11 @@ 🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 -> :rocket: Network Explorer ➡️➡️➡️➡️➡️ **View output:** https://{{ env.GITHUB_REF_SLUG }}.{{ secrets.NYM_CI_WWW_BASE }}/ +> :rocket: {{ env.NYM_PROJECT_NAME }} ➡️➡️➡️➡️➡️ **View output:** https://{{ env.GITHUB_REF_SLUG }}.{{ env.NYM_CI_WWW_BASE }}/ > ✅ **SUCCESS** -> `{{ github.sha }}` `{{ github.ref }}` https://{{ github.server_url }}/{{ github.repository }}/tree/{{ github.ref }} -> `build job` https://{{ github.server_url }}/{{ github.repository }}/actions/runs/{{ github.run_id }} +> `branch` {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/tree/{{ env.GIT_BRANCH_NAME }} +> `commit` {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/commit/{{ env.GITHUB_SHA }} +> `build ` {{ env.GITHUB_SERVER_URL }}/{{ env.GITHUB_REPOSITORY }}/actions/runs/{{ env.GITHUB_RUN_ID }} Commit message: ``` -{{ github.event.head_commit.message }} +{{ env.GIT_COMMIT_MESSAGE }} ```