Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Peiman Jafari committed Oct 28, 2020
0 parents commit 043ade4
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

## v0.0.1
Initial release.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine:3

RUN ["/bin/sh", "-c", "apk add --update --no-cache bash ca-certificates curl git jq openssh"]

RUN ["bin/sh", "-c", "mkdir -p /src"]

COPY ["src", "/src/"]

ENTRYPOINT ["/src/main.sh"]
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# amomtool GitHub Actions

amomtool GitHub Actions allow you to check Prometheus Alertmanager config within GitHub Actions.

The output of the actions can be viewed from the Actions tab in the main repository view. If the actions are executed on a pull request event, a comment may be posted on the pull request.

## Success Criteria

An exit code of `0` is considered a successful execution.

## Usage

amomtool GitHub Actions are a single GitHub Action that executes amtool check-config subcommand.

```yaml
name: Check Prometheus Alertmanager Config

on:
pull_request:
paths:
- 'alertmanager/config.yml'

jobs:
on-pull-request:
name: On Pull Request
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@master

- name: Check Prometheus Alertmanager Config
uses: peimanja/amtool-github-actions@master
with:
amtool_actions_config: 'alertmanager/config.yml'
amtool_actions_version: '0.21.0'
amtool_actions_comment: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
## Inputs
Inputs configure amtool GitHub Actions to perform different actions.
* `amtool_actions_config` - (Required) Path to Alertmanager config file.
* `amtool_actions_version` - (Optional) amtool version to install and execute (Alertmanager bundle version). The default is set to `latest` and the latest stable version will be pulled down automatically.
* `amtool_actions_comment` - (Optional) Whether or not to comment on GitHub pull requests. Defaults to `true`.

## Secrets

Secrets are similar to inputs except that they are encrypted and only used by GitHub Actions. It's a convenient way to keep sensitive data out of the GitHub Actions workflow YAML file.

* `GITHUB_TOKEN` - (Optional) The GitHub API token used to post comments to pull requests. Not required if the `amtool_actions_comment` input is set to `false`.
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'amtool GitHub Actions'
description: 'Runs Prometheus Alertmanager amtool commands via GitHub Actions.'
author: 'Peiman Jafari'
branding:
icon: 'terminal'
color: 'purple'
inputs:
amtool_actions_config:
description: 'Path to Alertmanager config file.'
required: true
amtool_actions_version:
description: 'amtool version to install.'
default: 'latest'
amtool_actions_comment:
description: 'Whether or not to comment on pull requests.'
default: true
runs:
using: 'docker'
image: './Dockerfile'
4 changes: 4 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh -l

amtool check-config $1

45 changes: 45 additions & 0 deletions src/amtool_check_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

function amtoolCheckConfig {
echo "info: checking if Alertmanager config files are valid or not"
checkconfigOut=$(amtool check-config ${amConfig} ${*} 2>&1)
checkconfigExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
if [ ${checkconfigExitCode} -eq 0 ]; then
echo "checkconfig: info: Alertmanager config files ${amConfig} are valid."
echo "${checkconfigOut}"
echo
checkconfigCommentStatus="Success"
fi

# Exit code of !0 indicates failure.
if [ ${checkconfigExitCode} -ne 0 ]; then
echo "checkconfig: error: Alertmanager config files ${amConfig} are invalid."
echo "${checkconfigOut}"
echo
checkconfigCommentStatus="Failed"
fi

# Comment on the pull request if necessary.
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${amtoolComment}" == "1" ]; then
checkconfigCommentWrapper="#### \`amtool check-config\` ${checkconfigCommentStatus}
<details><summary>Show Output</summary>
\`\`\`
${checkconfigOut}
\`\`\`
</details>
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Files: \`${amConfig}\`*"

echo "checkconfig: info: creating JSON"
checkconfigPayload=$(echo "${checkconfigCommentWrapper}" | jq -R --slurp '{body: .}')
checkconfigCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "checkconfig: info: commenting on the pull request"
echo "${checkconfigPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${checkconfigCommentsURL}" > /dev/null
fi

exit ${checkconfigExitCode}
}
67 changes: 67 additions & 0 deletions src/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

function parseInputs {
# Required inputs
if [ "${INPUT_AMTOOL_ACTIONS_CONFIG}" != "" ]; then
amConfig=${INPUT_AMTOOL_ACTIONS_CONFIG}
else
echo "Input amtool_files cannot be empty"
exit 1
fi

# Optional inputs
amtoolVersion="latest"
if [ "${INPUT_AMTOOL_ACTIONS_VERSION}" != "" ] || [ "${INPUT_AMTOOL_ACTIONS_VERSION}" != "latest" ]; then
amtoolVersion=${INPUT_AMTOOL_ACTIONS_VERSION}
fi

amtoolComment=0
if [ "${INPUT_AMTOOL_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_AMTOOL_ACTIONS_COMMENT}" == "true" ]; then
amtoolComment=1
fi
}


function installAmtool {
if [[ "${amtoolVersion}" == "latest" ]]; then
echo "Checking the latest version of Amtool"
amtoolVersion=$(git ls-remote --tags --refs --sort="v:refname" git://github.com/prometheus/alertmanager | grep -v '[-].*' | tail -n1 | sed 's/.*\///' | cut -c 2-)
if [[ -z "${amtoolVersion}" ]]; then
echo "Failed to fetch the latest version"
exit 1
fi
fi


url="https://github.com/prometheus/alertmanager/releases/download/v${amtoolVersion}/alertmanager-${amtoolVersion}.linux-amd64.tar.gz"

echo "Downloading Amtool v${amtoolVersion}"
curl -s -S -L -o /tmp/amtool_${amtoolVersion} ${url}
if [ "${?}" -ne 0 ]; then
echo "Failed to download Amtool v${amtoolVersion}"
exit 1
fi
echo "Successfully downloaded Amtool v${amtoolVersion}"

echo "Unzipping Amtool v${amtoolVersion}"
tar -zxf /tmp/amtool_${amtoolVersion} --strip-components=1 --directory /usr/local/bin &> /dev/null
if [ "${?}" -ne 0 ]; then
echo "Failed to unzip Amtool v${amtoolVersion}"
exit 1
fi
echo "Successfully unzipped Amtool v${amtoolVersion}"
}

function main {
# Source the other files to gain access to their functions
scriptDir=$(dirname ${0})
source ${scriptDir}/amtool_check_config.sh

parseInputs
cd ${GITHUB_WORKSPACE}
installAmtool
amtoolCheckConfig ${*}
esac
}

main "${*}"

0 comments on commit 043ade4

Please sign in to comment.