Skip to content

Commit

Permalink
Merge pull request #50 from lizrabuya/master
Browse files Browse the repository at this point in the history
SUP-1408 - Remove support of the ENV vars and renaming existing parameters to follow naming conventions for configuration variables
  • Loading branch information
lizrabuya committed Sep 20, 2023
2 parents b1bb995 + 0b11382 commit 97c6c35
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 191 deletions.
21 changes: 0 additions & 21 deletions .github/main.workflow

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Unit Test

on: push

jobs:
build:
name: build
runs-on: ubuntu-latest
steps:

- name: Check out code
uses: actions/checkout@v3

- name: Verify that the Docker image for the action builds
run: docker build . --file Dockerfile

tests:
name: unit-test
runs-on: ubuntu-latest
steps:
- name: Run unit test for the action
run: docker run --rm -v "$PWD":/plugin buildkite/plugin-tester:v4.0.0

38 changes: 38 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Verify

on: push

jobs:
tests:
name: Integration tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Verify that the Docker image for the action builds
run: docker build . --file Dockerfile

- name: Run action with minimal parameters
uses: ./
with:
buildkite_api_access_token: ${{ secrets.BUILDKITE_API_ACCESS_TOKEN }}
pipeline: "lzrinc/experimental-pipeline"
message: Triggered from GHA using minimal parameters
branch: master
commit: HEAD

- name: Run action with all parameters
uses: ./
id: tests
with:
buildkite_api_access_token: ${{ secrets.BUILDKITE_API_ACCESS_TOKEN }}
pipeline: "lzrinc/experimental-pipeline"
branch: dev
commit: HEAD
message: ":buildkite::github: 🚀🚀🚀 Triggered from GHA using commit message: ${{ github.event.head_commit.message }}"
build_env_vars: '{"TRIGGERED_FROM_GHA": "true"}'
build_meta_data: '{"FOO": "bar"}'
ignore_pipeline_branch_filter: true

- name: Verify output
run: echo ${{join(steps.tests.outputs.*, '\n')}}
44 changes: 6 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,9 @@ Create a [Buildkite API Access Token](https://buildkite.com/docs/apis/rest-api#a

## Configuration Options

### Configuration as Environment Variables

For example, the following workflow creates a new Buildkite build on every commit by setting environment variables:

```
on: [push]
steps:
- name: Trigger a Buildkite Build
uses: "buildkite/trigger-pipeline-action@v1.6.0"
env:
BUILDKITE_API_ACCESS_TOKEN: ${{ secrets.TRIGGER_BK_BUILD_TOKEN }}
PIPELINE: "my-org/my-deploy-pipeline"
BRANCH: "master"
COMMIT: "HEAD"
MESSAGE: ":github: Triggered from a GitHub Action"
```



The following environment variable options can be configured:

|Env var|Description|Default|
|-|-|-|
|PIPELINE|The pipeline to create a build on, in the format `<org-slug>/<pipeline-slug>`||
|COMMIT|The commit SHA of the build. Optional.|`$GITHUB_SHA`|
|BRANCH|The branch of the build. Optional.|`$GITHUB_REF`|
|MESSAGE|The message for the build. Optional.||
|BUILD_ENV_VARS|Additional environment variables to set on the build, in JSON format. e.g. `{"FOO": "bar"}`. Optional. ||
|BUILD_META_DATA|Meta data to set on the build, in JSON format. e.g. `{"FOO": "bar"}`. Optional. ||
|IGNORE_PIPELINE_BRANCH_FILTER | Ignore pipeline branch filtering when creating a new build. true or false. Optional. ||

### Configuration as Input Parameters

From v1.6.0, optional input parameters can now be used to pass in the configuration options. However, configuration defined as environment variables take precedence over the input parameters.
The following workflow creates a new Buildkite build to the target `pipeline` on every commit.

```
on: [push]
Expand All @@ -57,14 +25,14 @@ steps:
- name: Trigger a Buildkite Build
uses: "buildkite/trigger-pipeline-action@v1.6.0"
with:
buildkite-token: ${{ secrets.TRIGGER_BK_BUILD_TOKEN }}
buildkite_api_access_token: ${{ secrets.TRIGGER_BK_BUILD_TOKEN }}
pipeline: "my-org/my-deploy-pipeline"
branch: "master"
commit: "HEAD"
message: ":github: Triggered from a GitHub Action"
build-env-vars: '{"TRIGGERED_FROM_GHA": "true"}'
build-meta-data: '{"FOO": "bar"}'
ignore-pipeline-branch-filter: true
build_env_vars: '{"TRIGGERED_FROM_GHA": "true"}'
build_meta_data: '{"FOO": "bar"}'
ignore_pipeline_branch_filter: true
```

## Outputs
Expand All @@ -82,7 +50,7 @@ The following outputs are provided by the action:
To run the test workflow, you use [act](https://github.com/nektos/act) which will run it just as it does on GitHub:

```bash
act
act -n
```

## Testing
Expand Down
24 changes: 8 additions & 16 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,32 @@ name: 'Trigger Buildkite Pipeline'
author: 'Buildkite'
description: 'A GitHub Action for triggering a build on a Buildkite pipeline.'
inputs:
buildkite-token:
buildkite_api_access_token:
description: 'Buildkite API Access Token'
required: false
required: true
pipeline:
description: 'The pipline to create a build on, in the format <org-slug>/<pipeline-slug>'
required: false
required: true
commit:
description: 'The commit SHA of the build'
required: false
default: HEAD
branch:
description: 'The branch of the build'
required: false
message:
description: 'The message for the build'
required: false
build-env-vars:
build_env_vars:
description: 'Additional environment variables to set on the build, in JSON format'
required: false
build-meta-data:
build_meta_data:
description: 'Meta-data to set on the build, in JSON format'
required: false
ignore-pipeline-branch-filter:
ignore_pipeline_branch_filter:
description: 'Ignore pipeline branch filtering when creating a new build. true or false'
required: false

runs:
using: 'docker'
image: 'Dockerfile'
env:
BUILDKITE_API_ACCESS_TOKEN: ${{ inputs.buildkite-token }}
PIPELINE: ${{ inputs.pipeline }}
BRANCH: ${{ inputs.branch }}
COMMIT: ${{ inputs.commit }}
MESSAGE: ${{ inputs.message }}
BUILD_ENV_VARS: ${{ inputs.build-env-vars }}
BUILD_META_DATA: ${{ inputs.build-meta-data }}
IGNORE_PIPELINE_BRANCH_FILTER: ${{ inputs.ignore-pipeline-branch-filter }}
image: 'Dockerfile'
97 changes: 48 additions & 49 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,63 +42,64 @@ function get_github_env_json() {
echo "$GITHUB_EVENT_JSON"
}

function get_build_env_vars_json() {
BUILD_ENV_VARS=$(
jq -c -s 'add' \
<(echo "$1") \
<(echo "$2") \
<(echo "$3")
)
echo "$BUILD_ENV_VARS"
function get_INPUT_BUILD_ENV_VARS_json() {
INPUT_BUILD_ENV_VARS=$(
jq -c -s 'add' \
<(echo "$1") \
<(echo "$2") \
<(echo "$3")
)

echo "$INPUT_BUILD_ENV_VARS"
}

if [[ -z "${BUILDKITE_API_ACCESS_TOKEN:-}" ]]; then
echo "You must set the BUILDKITE_API_ACCESS_TOKEN environment variable (e.g. BUILDKITE_API_ACCESS_TOKEN = \"xyz\")"
if [[ -z "${INPUT_BUILDKITE_API_ACCESS_TOKEN:-}" ]]; then
echo "You must set the buildkite_api_access_token input parameter (e.g. buildkite_api_access_token: \"1234567890\")"
exit 1
fi

if [[ -z "${PIPELINE:-}" ]]; then
echo "You must set the PIPELINE environment variable (e.g. PIPELINE = \"my-org/my-pipeline\")"
if [[ -z "${INPUT_PIPELINE:-}" ]]; then
echo "You must set the pipeline input parameter (e.g. pipeline: \"my-org/my-pipeline\")"
exit 1
fi

ORG_SLUG=$(echo "${PIPELINE}" | cut -d'/' -f1)
PIPELINE_SLUG=$(echo "${PIPELINE}" | cut -d'/' -f2)
ORG_SLUG=$(echo "${INPUT_PIPELINE}" | cut -d'/' -f1)
PIPELINE_SLUG=$(echo "${INPUT_PIPELINE}" | cut -d'/' -f2)

COMMIT="${COMMIT:-${GITHUB_SHA}}"
BRANCH="${BRANCH:-${GITHUB_REF#"refs/heads/"}}"
MESSAGE="${MESSAGE:-}"
COMMIT="${INPUT_COMMIT:-${GITHUB_SHA}}"
BRANCH="${INPUT_BRANCH:-${GITHUB_REF#"refs/heads/"}}"
MESSAGE="${INPUT_MESSAGE:-}"

NAME=$(jq -r ".pusher.name" "$GITHUB_EVENT_PATH")
EMAIL=$(jq -r ".pusher.email" "$GITHUB_EVENT_PATH")
PULL_REQUEST_ID=$(jq -r '.pull_request.number // ""' "$GITHUB_EVENT_PATH")

BUILD_ENV_VARS="${BUILD_ENV_VARS:-}"
INPUT_BUILD_ENV_VARS="${INPUT_BUILD_ENV_VARS:-}"

DELETE_EVENT_JSON=""
if is_delete_event; then
DELETE_EVENT_JSON="$(get_delete_event_json)"
DELETE_EVENT_JSON="$(get_delete_event_json)"
fi

if [[ "$BUILD_ENV_VARS" ]]; then
if ! echo "$BUILD_ENV_VARS" | jq empty; then
echo ""
echo "Error: BUILD_ENV_VARS provided invalid JSON: $BUILD_ENV_VARS"
exit 1
if [[ "$INPUT_BUILD_ENV_VARS" ]]; then
if ! echo "$INPUT_BUILD_ENV_VARS" | jq empty; then
echo ""
echo "Error: build_env_vars provided invalid JSON: $INPUT_BUILD_ENV_VARS"
exit 1
fi
fi

BUILD_ENV_VARS_JSON="$(get_build_env_vars_json "$DELETE_EVENT_JSON" "$BUILD_ENV_VARS" "$(get_github_env_json)")"


INPUT_BUILD_ENV_VARS_JSON="$(get_INPUT_BUILD_ENV_VARS_json "$DELETE_EVENT_JSON" "$INPUT_BUILD_ENV_VARS" "$(get_github_env_json)")"

# Use jq’s --arg properly escapes string values for us
JSON=$(
jq -c -n \
--arg COMMIT "$COMMIT" \
--arg BRANCH "$BRANCH" \
--arg COMMIT "$COMMIT" \
--arg BRANCH "$BRANCH" \
--arg MESSAGE "$MESSAGE" \
--arg NAME "$NAME" \
--arg EMAIL "$EMAIL" \
--arg NAME "$NAME" \
--arg EMAIL "$EMAIL" \
'{
"commit": $COMMIT,
"branch": $BRANCH,
Expand All @@ -111,22 +112,22 @@ JSON=$(
)

# Link pull request if pull request id is specified
if [[ ! -z "$PULL_REQUEST_ID" ]]; then
if [[ -n "$PULL_REQUEST_ID" ]]; then
JSON=$(echo "$JSON" | jq -c --arg PULL_REQUEST_ID "$PULL_REQUEST_ID" '. + {pull_request_id: $PULL_REQUEST_ID}')
fi

# Set build meta data, if specified
if [[ "${BUILD_META_DATA:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson BUILD_META_DATA "$BUILD_META_DATA" '. + {meta_data: $BUILD_META_DATA}'); then
if [[ "${INPUT_BUILD_META_DATA:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson INPUT_BUILD_META_DATA "$INPUT_BUILD_META_DATA" '. + {meta_data: $INPUT_BUILD_META_DATA}'); then
echo ""
echo "Error: BUILD_META_DATA provided invalid JSON: $BUILD_META_DATA"
echo "Error: build_meta_data provided invalid JSON: $INPUT_BUILD_META_DATA"
exit 1
fi
fi

# Merge in ignore_pipeline_branch_filters, if they specified a value
if [[ "${IGNORE_PIPELINE_BRANCH_FILTER:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson IGNORE_PIPELINE_BRANCH_FILTER "$IGNORE_PIPELINE_BRANCH_FILTER" '. + {ignore_pipeline_branch_filters: $IGNORE_PIPELINE_BRANCH_FILTER}'); then
if [[ "${INPUT_IGNORE_PIPELINE_BRANCH_FILTER:-}" ]]; then
if ! JSON=$(echo "$JSON" | jq -c --argjson INPUT_IGNORE_PIPELINE_BRANCH_FILTER "$INPUT_IGNORE_PIPELINE_BRANCH_FILTER" '. + {ignore_pipeline_branch_filters: $INPUT_IGNORE_PIPELINE_BRANCH_FILTER}'); then
echo ""
echo "Error: Could not set ignore_pipeline_branch_filters"
exit 1
Expand All @@ -135,10 +136,10 @@ fi

# Add additional env vars as a nested object
FINAL_JSON=""
if [[ "$BUILD_ENV_VARS_JSON" ]]; then
FINAL_JSON=$(
echo "$JSON" | jq -c --argjson env "$BUILD_ENV_VARS_JSON" '. + {env: $env}'
)
if [[ "$INPUT_BUILD_ENV_VARS_JSON" ]]; then
FINAL_JSON=$(
echo "$JSON" | jq -c --argjson env "$INPUT_BUILD_ENV_VARS_JSON" '. + {env: $env}'
)
else
FINAL_JSON=$JSON
fi
Expand All @@ -150,13 +151,13 @@ RESPONSE=$(
--silent \
--show-error \
-X POST \
-H "Authorization: Bearer ${BUILDKITE_API_ACCESS_TOKEN}" \
-H "Authorization: Bearer ${INPUT_BUILDKITE_API_ACCESS_TOKEN}" \
"https://api.buildkite.com/v2/organizations/${ORG_SLUG}/pipelines/${PIPELINE_SLUG}/builds" \
-d "$FINAL_JSON" | tr -d '\n'
) || CODE=$?

if [ $CODE -ne 0 ]; then
MESSAGE=$(echo "$RESPONSE" | jq .message 2> /dev/null || true)
MESSAGE=$(echo "$RESPONSE" | jq .message 2>/dev/null || true)
if [[ -n "$MESSAGE" ]] && [[ "$MESSAGE" != 'null' ]]; then
echo -n "Buildkite API call failed: $MESSAGE"
fi
Expand All @@ -166,17 +167,15 @@ fi
echo ""
echo "Build created:"
URL=$(echo "$RESPONSE" | jq --raw-output ".web_url")
echo $URL
echo "$URL"

# Provide JSON and Web URL as outputs for downstream actions
# use environment variable $GITHUB_OUTPUT, or fall back to deprecated set-output command
# https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
if [[ -n "${GITHUB_OUTPUT:-}" ]]
then
echo "json=$RESPONSE" >> ${GITHUB_OUTPUT}
echo "url=$URL" >> ${GITHUB_OUTPUT}
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
echo "json=$RESPONSE" >>"${GITHUB_OUTPUT}"
echo "url=$URL" >>"${GITHUB_OUTPUT}"
else
echo "::set-output name=json::$RESPONSE"
echo "::set-output name=url::$URL"
fi

fi

0 comments on commit 97c6c35

Please sign in to comment.