Skip to content

Commit

Permalink
support using comments for triggering (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalhoun authored Jun 2, 2023
1 parent b193b37 commit 5443168
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 53 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ It's 100% Open Source and licensed under the [APACHE2](LICENSE).
## Introduction

This repo contains a GitHub Action that determines the affected [Atmos](https://atmos.tools) stacks for a PR, then
triggers the corresponding spacelift stacks.
creates a comment on the PR which Spacelift can use to trigger the corresponding stacks via a push policy.

Optionally, you can use the `spacectl` trigger method, which uses the `spacectl` CLI to trigger the corresponding
spacelift stacks directly rather than via comment/push policy.



Expand All @@ -82,15 +85,24 @@ triggers the corresponding spacelift stacks.
context:
runs-on: ubuntu-latest
steps:
- name: Atmos Affected Stacks Trigger Spacelift
- name: Atmos Affected Stacks Trigger Spacelift (via comment)
uses: cloudposse/github-action-atmos-affected-trigger-spacelift@main
id: example
with:
atmos-config-path: ./rootfs/usr/local/etc/atmos
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Atmos Affected Stacks Trigger Spacelift (direct)
uses: cloudposse/github-action-atmos-affected-trigger-spacelift@main
id: example
with:
atmos-config-path: ./rootfs/usr/local/etc/atmos
github-token: ${{ secrets.GITHUB_TOKEN }}
trigger-method: spacectl
spacelift-endpoint: https://unicorn.app.spacelift.io
spacelift-api-key-id: ${{ secrets.SPACELIFT_API_KEY_ID }}
spacelift-api-key-secret: ${{ secrets.SPACELIFT_API_KEY_SECRET }}

```


Expand All @@ -117,10 +129,11 @@ triggers the corresponding spacelift stacks.
| jq-force | Whether to force the installation of jq | true | false |
| jq-version | The version of jq to install if install-jq is true | 1.6 | false |
| spacectl-version | The version of spacectl to install if install-spacectl is true | latest | false |
| spacelift-api-key-id | The SPACELIFT\_API\_KEY\_ID | N/A | true |
| spacelift-api-key-secret | The SPACELIFT\_API\_KEY\_SECRET | N/A | true |
| spacelift-endpoint | The Spacelift endpoint. For example, https://unicorn.app.spacelift.io | N/A | true |
| spacelift-api-key-id | The SPACELIFT\_API\_KEY\_ID | N/A | false |
| spacelift-api-key-secret | The SPACELIFT\_API\_KEY\_SECRET | N/A | false |
| spacelift-endpoint | The Spacelift endpoint. For example, https://unicorn.app.spacelift.io | N/A | false |
| terraform-version | The version of terraform to install if install-terraform is true | latest | false |
| trigger-method | The method to use to trigger the Spacelift stack. Valid values are `comment` and `spacectl` | comment | false |


<!-- markdownlint-restore -->
Expand Down
16 changes: 14 additions & 2 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ description: GitHub Action for Triggering Affected Spacelift Stacks

introduction: |-
This repo contains a GitHub Action that determines the affected [Atmos](https://atmos.tools) stacks for a PR, then
triggers the corresponding spacelift stacks.
creates a comment on the PR which Spacelift can use to trigger the corresponding stacks via a push policy.
Optionally, you can use the `spacectl` trigger method, which uses the `spacectl` CLI to trigger the corresponding
spacelift stacks directly rather than via comment/push policy.
references:
- name: "github-actions-workflows"
Expand All @@ -60,15 +63,24 @@ usage: |-
context:
runs-on: ubuntu-latest
steps:
- name: Atmos Affected Stacks Trigger Spacelift
- name: Atmos Affected Stacks Trigger Spacelift (via comment)
uses: cloudposse/github-action-atmos-affected-trigger-spacelift@main
id: example
with:
atmos-config-path: ./rootfs/usr/local/etc/atmos
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Atmos Affected Stacks Trigger Spacelift (direct)
uses: cloudposse/github-action-atmos-affected-trigger-spacelift@main
id: example
with:
atmos-config-path: ./rootfs/usr/local/etc/atmos
github-token: ${{ secrets.GITHUB_TOKEN }}
trigger-method: spacectl
spacelift-endpoint: https://unicorn.app.spacelift.io
spacelift-api-key-id: ${{ secrets.SPACELIFT_API_KEY_ID }}
spacelift-api-key-secret: ${{ secrets.SPACELIFT_API_KEY_SECRET }}
```
include:
Expand Down
115 changes: 72 additions & 43 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,72 @@ branding:
icon: "file"
color: "white"
inputs:
spacelift-endpoint:
required: true
description: "The Spacelift endpoint. For example, https://unicorn.app.spacelift.io"
spacelift-api-key-id:
required: true
description: The SPACELIFT_API_KEY_ID
spacelift-api-key-secret:
required: true
description: The SPACELIFT_API_KEY_SECRET
github-token:
required: true
description: A GitHub token for running the spacelift-io/setup-spacectl action
deploy:
default: "false"
description: A flag to indicate if a deployment should be triggered. If false, a preview will be triggered.
atmos-config-path:
default: .
description: A path to the folder where atmos.yaml is located
required: false
atmos-version:
default: latest
description: The version of atmos to install if install-atmos is true
required: false
default-branch:
default: ${{ github.event.repository.default_branch }}
description: The default branch to use for the base ref.
required: false
default: ${{ github.event.repository.default_branch }}
deploy:
default: 'false'
description: A flag to indicate if a deployment should be triggered. If false, a preview will be triggered.
github-token:
description: A GitHub token for running the spacelift-io/setup-spacectl action
required: true
head-ref:
description: The head ref to checkout. If not provided, the head default branch is used.
required: false
install-atmos:
default: 'true'
description: Whether to install atmos
required: false
default: "true"
atmos-version:
description: The version of atmos to install if install-atmos is true
required: false
default: "latest"
atmos-config-path:
description: A path to the folder where atmos.yaml is located
install-jq:
default: 'false'
description: Whether to install jq
required: false
default: .
install-spacectl:
default: 'true'
description: Whether to install spacectl
required: false
default: "true"
spacectl-version:
description: The version of spacectl to install if install-spacectl is true
required: false
default: "latest"
install-terraform:
default: 'true'
description: Whether to install terraform
required: false
default: "true"
terraform-version:
description: The version of terraform to install if install-terraform is true
required: false
default: "latest"
install-jq:
description: Whether to install jq
jq-force:
default: 'true'
description: Whether to force the installation of jq
required: false
default: "false"
jq-version:
default: '1.6'
description: The version of jq to install if install-jq is true
required: false
default: "1.6"
jq-force:
description: Whether to force the installation of jq
spacectl-version:
default: latest
description: The version of spacectl to install if install-spacectl is true
required: false
spacelift-api-key-id:
description: The SPACELIFT_API_KEY_ID
required: false
spacelift-api-key-secret:
description: The SPACELIFT_API_KEY_SECRET
required: false
spacelift-endpoint:
description: The Spacelift endpoint. For example, https://unicorn.app.spacelift.io
required: false
terraform-version:
default: latest
description: The version of terraform to install if install-terraform is true
required: false
trigger-method:
default: 'comment'
description: The method to use to trigger the Spacelift stack. Valid values are `comment` and `spacectl`
required: false
default: "true"

runs:
using: "composite"
Expand All @@ -90,14 +94,15 @@ runs:
jq-force: ${{inputs.jq-force}}

- name: Setup Spacectl
if: ${{ inputs.install-spacectl == 'true' }}
if: ${{ steps.affected-stacks.outputs.has-affected-stacks == 'true' && inputs.install-spacectl == 'true' && inputs.trigger-method == 'spacectl' }}
uses: spacelift-io/setup-spacectl@v0.3.0
env:
GITHUB_TOKEN: ${{inputs.github-token}}
with:
version: ${{inputs.spacectl-version}}

- name: Trigger Spacelift Stacks
- name: Trigger Spacelift Stacks with Spacectl
if: ${{ steps.affected-stacks.outputs.has-affected-stacks == 'true' && inputs.trigger-method == 'spacectl' }}
shell: bash
env:
SPACELIFT_API_KEY_ENDPOINT: ${{inputs.spacelift-endpoint}}
Expand All @@ -111,3 +116,27 @@ runs:
fi
printf "%s\n" '${{ steps.affected-stacks.outputs.affected }}' >affected-stacks.json
${GITHUB_ACTION_PATH}/scripts/spacelift-trigger-affected-stacks.sh $spacectl_deploy
- name: Create PR Comment Body with Affected Stacks
if: ${{ steps.affected-stacks.outputs.has-affected-stacks == 'true' && inputs.trigger-method == 'comment' }}
id: create-pr-comment-body
shell: bash
run: |
spacectl_deploy="false"
if [ "${{inputs.deploy}}" = "true" ]; then
spacectl_deploy="true"
fi
printf "%s\n" '${{ steps.affected-stacks.outputs.affected }}' >affected-stacks.json
body=$(${GITHUB_ACTION_PATH}/scripts/spacelift-generate-pr-comment-body.sh $spacectl_deploy)
printf "body=%s" "$body" >>$GITHUB_OUTPUT
- name: Create PR Comment with Affected Stacks
uses: marocchino/sticky-pull-request-comment@f61b6cf21ef2fcc468f4345cdfcc9bda741d2343 # v2.6.2
if: ${{ steps.affected-stacks.outputs.has-affected-stacks == 'true' && inputs.trigger-method == 'comment' }}
with:
header: atmos-affected-stacks
recreate: true
path: comment-body.txt
env:
GITHUB_TOKEN: ${{inputs.github-token}}

7 changes: 4 additions & 3 deletions docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
| jq-force | Whether to force the installation of jq | true | false |
| jq-version | The version of jq to install if install-jq is true | 1.6 | false |
| spacectl-version | The version of spacectl to install if install-spacectl is true | latest | false |
| spacelift-api-key-id | The SPACELIFT\_API\_KEY\_ID | N/A | true |
| spacelift-api-key-secret | The SPACELIFT\_API\_KEY\_SECRET | N/A | true |
| spacelift-endpoint | The Spacelift endpoint. For example, https://unicorn.app.spacelift.io | N/A | true |
| spacelift-api-key-id | The SPACELIFT\_API\_KEY\_ID | N/A | false |
| spacelift-api-key-secret | The SPACELIFT\_API\_KEY\_SECRET | N/A | false |
| spacelift-endpoint | The Spacelift endpoint. For example, https://unicorn.app.spacelift.io | N/A | false |
| terraform-version | The version of terraform to install if install-terraform is true | latest | false |
| trigger-method | The method to use to trigger the Spacelift stack. Valid values are `comment` and `spacectl` | comment | false |


<!-- markdownlint-restore -->
12 changes: 12 additions & 0 deletions scripts/spacelift-generate-pr-comment-body.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

spacectl_command="preview"

if [ "$1" = "true" ]; then
spacectl_command="deploy"
fi

# Use jq to extract the spacelift_stack values and iterate through them
for spacelift_stack in $(jq -r '.[].spacelift_stack' < "affected-stacks.json" | grep -v null); do
printf "/spacelift %s %s\n" "$spacectl_command" "$spacelift_stack" >> "comment-body.txt"
done

0 comments on commit 5443168

Please sign in to comment.