Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mattermost notification #31

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ ENV ROCKETCHAT_MESSAGE_TEXT="A new tag for the project ${CI_PROJECT_NAME} was cr
ENV ROCKETCHAT_HOOK_URL="https://rocketchat.example.com/hooks/here_be_dragons"
ENV ROCKETCHAT_TAGS_URL="${CI_PROJECT_URL}/-/tags"

ENV MATTERMOST_EMOJI=":tada:"
ENV MATTERMOST_USERNAME="Semantic Release"
ENV MATTERMOST_MESSAGE_TEXT="A new tag for the project ${CI_PROJECT_NAME} was created by ${CI_COMMIT_AUTHOR}."
ENV MATTERMOST_HOOK_URL="https://mattermost.example.com/hooks/here_be_dragons"
ENV MATTERMOST_TAGS_URL="${CI_PROJECT_URL}/-/tags"

ENTRYPOINT [ "/docker-entrypoint.sh" ]
CMD [ "--dry-run" ]
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ The container has the following pre-defined environment variables:
| ROCKETCHAT_MESSAGE_TEXT | `A new tag for the project ${CI_PROJECT_NAME} was created by ${CI_COMMIT_AUTHOR}.` |
| ROCKETCHAT_HOOK_URL | `https://rocketchat.example.com/hooks/here_be_dragons` |
| ROCKETCHAT_TAGS_URL | `${CI_PROJECT_URL}/-/tags` |
| MATTERMOST_EMOJI | `:tada:` |
| MATTERMOST_MESSAGE_TEXT | `A new tag for the project ${CI_PROJECT_NAME} was created by ${CI_COMMIT_AUTHOR}.` |
| MATTERMOST_HOOK_URL | `https://mattermost.example.com/hooks/here_be_dragons` |
| MATTERMOST_TAGS_URL | `${CI_PROJECT_URL}/-/tags` |
| MATTERMOST_USERNAME | `Semantic Release` |

### Example `.releaserc.yaml` for a Gitlab project

Expand Down Expand Up @@ -141,8 +146,7 @@ release:
- if-not-present
interruptible: true
script:
- 'for f in /docker-entrypoint.d/*.sh; do echo "INFO: Running ${f}";"${f}";done'
- semantic-release
- /docker-entrypoint.sh
rules:
- if: $CI_COMMIT_BRANCH == "master"
- if: $CI_COMMIT_BRANCH == "main"
Expand All @@ -166,36 +170,59 @@ docker run -it --rm \
ghcr.io/voxpupuli/semantic-release:latest
```

### Notifying RocketChat
### Notifying

There is a helper script in the container, which can send some data over curl to RocketChat.
You need a RocketChat Hook link.
#### RocketChat

#### Script
There is a helper script in the container, which can send some data over curl to RocketChat.
You need a RocketChat hook link.

The script has the parameters `-V`, `-o` and `-d`.

- `-V` specifies the version which should be announced.
- `-o` can specify optional extra curl parameters. Like for example `--insecure`.
- `-d` turn on debug output.

The script accesses the environment Variables:
The script accesses the environment variables:

- `ROCKETCHAT_EMOJI`
- `ROCKETCHAT_MESSAGE_TEXT`
- `ROCKETCHAT_TAGS_URL`
- `ROCKETCHAT_HOOK_URL`

#### Mattermost

There is a helper script in the container, which can send some data over curl to Mattermost.
You need a Mattermost hook link.

The script has the parameters `-V`, `-o` and `-d`.

- `-V` specifies the version which should be announced.
- `-o` can specify optional extra curl parameters. Like for example `--insecure`.
- `-d` turn on debug output.

The script accesses the environment variables:

- `MATTERMOST_EMOJI`
- `MATTERMOST_MESSAGE_TEXT`
- `MATTERMOST_TAGS_URL`
- `MATTERMOST_HOOK_URL`
- `MATTERMOST_USERNAME`

#### .releaserc.yaml

```yaml
---
# ...
plugins:
# Most people will choose between one of those two:
# ...
- path: '@semantic-release/exec'
publishCmd: "/scripts/notify-rocketchat.sh -V v${nextRelease.version} -o '--insecure' -d"
# ...
- path: '@semantic-release/exec'
publishCmd: "/scripts/notify-mattermost.sh -V v${nextRelease.version} -o '--insecure' -d"
# ...

```

Expand All @@ -204,13 +231,21 @@ plugins:
```yaml
---
release:
# Most people will choose between one of those two:
# ...
variables:
ROCKETCHAT_NOTIFY_TOKEN: "Some hidden CI Variable to not expose the token"
ROCKETCHAT_EMOJI: ":tada:"
ROCKETCHAT_MESSAGE_TEXT: "A new tag for the project ${CI_PROJECT_NAME} was created by ${GITLAB_USER_NAME}"
ROCKETCHAT_HOOK_URL: "https://rocketchat.example.com/hooks/${ROCKETCHAT_NOTIFY_TOKEN}"
ROCKETCHAT_TAGS_URL: "${CI_PROJECT_URL}/-/tags"
# ...
MATTERMOST_NOTIFY_TOKEN: "Some hidden CI Variable to not expose the token"
MATTERMOST_EMOJI: ":tada:"
MATTERMOST_MESSAGE_TEXT: "A new tag for the project ${CI_PROJECT_NAME} was created by ${GITLAB_USER_NAME}"
MATTERMOST_HOOK_URL: "https://mattermost.example.com/hooks/${MATTERMOST_NOTIFY_TOKEN}"
MATTERMOST_TAGS_URL: "${CI_PROJECT_URL}/-/tags"
MATTERMOST_USERNAME: "Semantic Release [Bot]"
# ...
```

Expand Down
37 changes: 37 additions & 0 deletions scripts/notify-mattermost.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

while getopts V:o:d flag
do
case "${flag}" in
V) VERSION=${OPTARG};;
o) OPTIONS=${OPTARG};;
d) DEBUG=1;;
esac
done

payload="{
\"icon_emoji\": \"${MATTERMOST_EMOJI}\",
\"username\": \"${MATTERMOST_USERNAME}\",
\"text\": \"${MATTERMOST_MESSAGE_TEXT}\",
\"attachments\": [
{
\"title\": \"Release ${VERSION}\",
\"title_link\": \"${MATTERMOST_TAGS_URL}/${VERSION}\"
}
]
}"

if [ "${DEBUG}" == 1 ]; then
echo "Version is: ${VERSION}"
echo "Options are: ${OPTIONS}"
echo "Payload is:"
echo "${payload}"
fi

if [[ -n ${MATTERMOST_HOOK_URL} ]]; then
curl \
-X POST \
-H 'Content-Type: application/json' \
--data "${payload}" \
${OPTIONS} ${MATTERMOST_HOOK_URL}
fi
Loading