Skip to content

Commit

Permalink
Merge pull request #31 from voxpupuli/add_mm
Browse files Browse the repository at this point in the history
feat: add mattermost notification
  • Loading branch information
rwaffen authored Oct 23, 2024
2 parents ddcc24f + c5b29fc commit 93a7762
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
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"

Check warning on line 44 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_PROJECT_URL' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

Check warning on line 44 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_PROJECT_URL' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

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}."

Check warning on line 48 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_PROJECT_NAME' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

Check warning on line 48 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_COMMIT_AUTHOR' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

Check warning on line 48 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_COMMIT_AUTHOR' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

Check warning on line 48 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_PROJECT_NAME' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/
ENV MATTERMOST_HOOK_URL="https://mattermost.example.com/hooks/here_be_dragons"
ENV MATTERMOST_TAGS_URL="${CI_PROJECT_URL}/-/tags"

Check warning on line 50 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_PROJECT_URL' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

Check warning on line 50 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-and-push-container

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$CI_PROJECT_URL' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

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

0 comments on commit 93a7762

Please sign in to comment.