├── docker-compose.yml ├── plugin.yml ├── .github └── workflows │ └── tests.yml ├── README.md ├── hooks └── command └── tests └── command.bats /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.4' 2 | services: 3 | tests: 4 | image: buildkite/plugin-tester 5 | volumes: 6 | - ".:/plugin" 7 | lint: 8 | image: buildkite/plugin-linter 9 | command: ['--id', 'my-org/my-plugin'] 10 | volumes: 11 | - ".:/plugin" 12 | -------------------------------------------------------------------------------- /plugin.yml: -------------------------------------------------------------------------------- 1 | name: Samson Deploy 2 | description: Sends webhooks to Samson to run deploys 3 | author: https://github.com/envato 4 | requirements: 5 | - jq 6 | configuration: 7 | properties: 8 | url: 9 | type: string 10 | required: 11 | - url 12 | additionalProperties: false 13 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: tests 3 | on: [push, pull_request] 4 | jobs: 5 | plugin-tests: 6 | name: Tests 7 | runs-on: ubuntu-latest 8 | container: 9 | image: buildkite/plugin-tester:latest 10 | volumes: 11 | - "${{github.workspace}}:/plugin" 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: tests 15 | run: bats tests/ 16 | plugin-lint: 17 | name: Lint 18 | runs-on: ubuntu-latest 19 | container: 20 | image: buildkite/plugin-linter:latest 21 | volumes: 22 | - "${{github.workspace}}:/plugin" 23 | steps: 24 | - uses: actions/checkout@v2 25 | - name: lint 26 | run: lint --id envato/samson-deploy 27 | plugin-shellcheck: 28 | name: Shellcheck 29 | runs-on: ubuntu-latest 30 | steps: 31 | - uses: actions/checkout@v2 32 | - name: shellcheck 33 | uses: ludeeus/action-shellcheck@1.1.0 34 | with: 35 | check_together: 'yes' 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Samson Deploy Buildkite Plugin 2 | 3 | [![MIT License](https://img.shields.io/badge/License-MIT-brightgreen.svg)](LICENSE) 4 | [![tests](https://github.com/envato/samson-deploy-buildkite-plugin/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/envato/samson-deploy-buildkite-plugin/actions/workflows/tests.yml) 5 | 6 | A [Buildkite plugin](https://buildkite.com/docs/agent/v3/plugins) that lets you specify the Samson webhooks for your project that Buildkite can use to send data to Samson. 7 | This is an alternative to using Buildkite Notifications, 8 | which can only be configured by Administrators and hide webhook failures. 9 | 10 | ## Example 11 | 12 | The only required configuration is the Samson URL, available from the Project "Webhook" tab: 13 | 14 | ```yml 15 | steps: 16 | - label: ":rocket: Deploy with Samson" 17 | plugins: 18 | - envato/samson-deploy#v0.1.2: 19 | url: "https://example.com/integrations/buildkite/578dc36a28ab49b2998603f0475211c3" 20 | ``` 21 | 22 | ## Configuration 23 | 24 | ### `url` 25 | 26 | The Samson webhook URL for your Project. 27 | 28 | ## Development 29 | 30 | To run the tests: 31 | 32 | ```sh 33 | docker-compose run --rm tests 34 | ``` 35 | 36 | To run the [Buildkite Plugin 37 | Linter](https://github.com/buildkite-plugins/buildkite-plugin-linter): 38 | 39 | ```sh 40 | docker-compose run --rm lint 41 | ``` 42 | -------------------------------------------------------------------------------- /hooks/command: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | 4 | echo "--- Sending Samson webhook" 5 | 6 | url=${BUILDKITE_PLUGIN_SAMSON_DEPLOY_URL?} 7 | 8 | if [[ "${BUILDKITE_PLUGIN_SAMSON_DEPLOY_DEBUG:-false}" =~ (true|on|1) ]] ; then 9 | echo "--- :hammer: Enabling debug mode" 10 | set -x 11 | fi 12 | 13 | headers=( "-H" "HTTP_X_BUILDKITE_EVENT: build.finished" "-H" "Content-Type: application/json") 14 | 15 | payload=$(jq -n --arg id "$BUILDKITE_BUILD_ID" \ 16 | --arg url "$BUILDKITE_BUILD_URL" \ 17 | --arg message "$BUILDKITE_MESSAGE" \ 18 | --arg num "$BUILDKITE_BUILD_NUMBER" \ 19 | --arg commit "$BUILDKITE_COMMIT" \ 20 | --arg branch "$BUILDKITE_BRANCH" \ 21 | --arg source "$BUILDKITE_SOURCE" '{ 22 | "event": "build.finished", 23 | "build": { 24 | "id": $id, 25 | "url": "UNIMPLEMENTED", 26 | "web_url": $url, 27 | "number": $num | tonumber, 28 | "state": "passed", 29 | "blocked": false, 30 | "message": $message, 31 | "commit": $commit, 32 | "branch": $branch, 33 | "tag": null, 34 | "source": $source, 35 | "creator": null, 36 | "created_at": "1970-01-01 00:00:00 UTC", 37 | "scheduled_at": "1970-01-01 00:00:00 UTC", 38 | "started_at": "1970-01-01 00:00:00 UTC", 39 | "finished_at": "1970-01-01 00:00:00 UTC", 40 | "meta_data": {}, 41 | "pull_request": null 42 | }, 43 | "pipeline": {}, 44 | "sender": null 45 | }') 46 | 47 | tmpdir="$(mktemp -d)" 48 | code=$(curl --request POST \ 49 | --silent \ 50 | -o "$tmpdir/samson_output" \ 51 | -w "%{http_code}" \ 52 | "${headers[@]}" \ 53 | -d "${payload}" \ 54 | "$url") 55 | 56 | case "$code" in 57 | 2*) 58 | ret=0 59 | ;; 60 | 4*) 61 | ret=1 62 | ;; 63 | 5*) 64 | ret=2 65 | ;; 66 | *) 67 | ret=3 68 | ;; 69 | esac 70 | 71 | if [[ $ret != 0 ]] ; then 72 | echo "+++ Samson submission" 73 | echo "HTTP code: $code" 74 | cat "$tmpdir/samson_output" 75 | echo 76 | exit $ret 77 | else 78 | echo "--- Samson submission" 79 | echo "HTTP code: $code" 80 | fi 81 | -------------------------------------------------------------------------------- /tests/command.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load "$BATS_PATH/load.bash" 4 | 5 | # Uncomment to enable stub debugging 6 | # export CURL_STUB_DEBUG=/dev/tty 7 | 8 | setup() { 9 | export BUILDKITE_COMMIT="abc123" 10 | export BUILDKITE_PLUGIN_SAMSON_DEPLOY_URL="https://example.com" 11 | export BUILDKITE_BUILD_ID="1" 12 | export BUILDKITE_BUILD_NUMBER="1" 13 | export BUILDKITE_BUILD_URL="example.com" 14 | export BUILDKITE_MESSAGE="message" 15 | export BUILDKITE_BRANCH="branch" 16 | export BUILDKITE_SOURCE="source" 17 | stub mktemp "-d : mkdir -p /tmp/foo ; echo /tmp/foo" 18 | } 19 | 20 | @test "curl succeeds" { 21 | 22 | stub curl "* : echo 201 ; echo 'curl_output' > /tmp/foo/samson_output" 23 | 24 | run $PWD/hooks/command 25 | 26 | assert_output --partial "--- Sending Samson webhook" 27 | assert_output --partial "HTTP code: 201" 28 | refute_output --partial "curl_output" 29 | assert_success 30 | unstub curl 31 | } 32 | 33 | @test "curl fails 4xx" { 34 | expected_payload='{\n "event": "build.finished",\n "build": {\n "id": "1",\n "url": "UNIMPLEMENTED",\n "web_url": "example.com",\n "number": 1,\n "state": "passed",\n "blocked": false,\n "message": "message",\n "commit": "abc123",\n "branch": "branch",\n "tag": null,\n "source": "source",\n "creator": null,\n "created_at": "1970-01-01 00:00:00 UTC",\n "scheduled_at": "1970-01-01 00:00:00 UTC",\n "started_at": "1970-01-01 00:00:00 UTC",\n "finished_at": "1970-01-01 00:00:00 UTC",\n "meta_data": {},\n "pull_request": null\n },\n "pipeline": {},\n "sender": null\n}' 35 | 36 | stub curl "* : echo 400 ; echo 'curl_output' > /tmp/foo/samson_output" 37 | 38 | run $PWD/hooks/command 39 | 40 | assert_output --partial "--- Sending Samson webhook" 41 | assert_output --partial "HTTP code: 400" 42 | assert_output --partial "curl_output" 43 | assert_failure 1 44 | unstub curl 45 | } 46 | 47 | @test "curl fails 5xx" { 48 | expected_payload='{\n "event": "build.finished",\n "build": {\n "id": "1",\n "url": "UNIMPLEMENTED",\n "web_url": "example.com",\n "number": 1,\n "state": "passed",\n "blocked": false,\n "message": "message",\n "commit": "abc123",\n "branch": "branch",\n "tag": null,\n "source": "source",\n "creator": null,\n "created_at": "1970-01-01 00:00:00 UTC",\n "scheduled_at": "1970-01-01 00:00:00 UTC",\n "started_at": "1970-01-01 00:00:00 UTC",\n "finished_at": "1970-01-01 00:00:00 UTC",\n "meta_data": {},\n "pull_request": null\n },\n "pipeline": {},\n "sender": null\n}' 49 | 50 | stub curl "* : echo 500 ; echo 'curl_output' > /tmp/foo/samson_output" 51 | 52 | run $PWD/hooks/command 53 | 54 | assert_output --partial "--- Sending Samson webhook" 55 | assert_output --partial "HTTP code: 500" 56 | assert_output --partial "curl_output" 57 | assert_failure 2 58 | unstub curl 59 | } 60 | --------------------------------------------------------------------------------