Skip to content

Commit 7ab74d6

Browse files
xavElyniss
authored andcommitted
feat: add action workspaces (#16)
* feat: add action workspaces: `aws`, `terraform`, `github` --------- Co-authored-by: Szymon Rząd <sz.rzad@gmail.com>
1 parent 39d672c commit 7ab74d6

11 files changed

Lines changed: 329 additions & 0 deletions

File tree

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
tab_width = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
max_line_length = 80
11+
trim_trailing_whitespace = true

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.4.0
4+
hooks:
5+
- id: check-merge-conflict
6+
- id: check-yaml
7+
- id: end-of-file-fixer
8+
- id: trailing-whitespace
9+
- id: detect-aws-credentials
10+
- id: detect-private-key
11+
- id: forbid-new-submodules
12+
- id: no-commit-to-branch
13+
- id: mixed-line-ending

aws/grafana/delete-key/action.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: "Delete Grafana Key"
2+
description: "Delete an access key from a Grafana workspace"
3+
4+
inputs:
5+
key-name:
6+
description: "The name of the key to remove"
7+
required: true
8+
workspace-id:
9+
description: 'The id of the key workspace'
10+
required: true
11+
12+
runs:
13+
using: 'composite'
14+
steps:
15+
- name: Delete Grafana key
16+
id: delete-grafana-key
17+
env:
18+
KEY_NAME: ${{ inputs.key-name }}
19+
WORKSPACE_ID: ${{ inputs.workspace-id }}
20+
shell: bash
21+
run: |
22+
echo "Deleting key name $KEY_NAME"
23+
aws grafana delete-workspace-api-key --key-name "$KEY_NAME" --workspace-id "$WORKSPACE_ID"

aws/grafana/get-details/action.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "Get Grafana Details"
2+
description: "Get endpoint and workspace-id"
3+
4+
inputs:
5+
environment:
6+
description: "The workspace environment"
7+
required: false
8+
default: 'prod'
9+
10+
outputs:
11+
endpoint:
12+
description: "The Grafana endpoint for this environment"
13+
value: ${{ steps.get-grafana-details.outputs.endpoint }}
14+
workspace-id:
15+
description: "The Grafana workspace id of the environment"
16+
value: ${{ steps.get-grafana-details.outputs.workspace-id }}
17+
18+
runs:
19+
using: 'composite'
20+
steps:
21+
- name: Get Grafana Details
22+
id: get-grafana-details
23+
shell: bash
24+
run: |
25+
echo "getting details for '${{ inputs.environment }}'"
26+
WORKSPACE_ID=$(aws grafana list-workspaces | jq -r '.workspaces[] | select( .tags.Env == "${{ inputs.environment }}") | select( .tags.Name == "grafana-9") | .id')
27+
ENDPOINT=$(aws grafana list-workspaces | jq -r '.workspaces[] | select( .tags.Env == "${{ inputs.environment }}") | select( .tags.Name == "grafana-9") | .endpoint')
28+
echo "endpoint=$ENDPOINT" >> $GITHUB_OUTPUT
29+
echo "workspace-id=$WORKSPACE_ID" >> $GITHUB_OUTPUT

aws/grafana/get-key/action.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: "Get Grafana Key"
2+
description: "Generate a key to access a Grafana workspace"
3+
4+
inputs:
5+
key-prefix:
6+
description: "The prefix for the key name"
7+
required: true
8+
workspace-id:
9+
description: "The id of the key workspace"
10+
required: true
11+
12+
outputs:
13+
key-name:
14+
description: "The complete key name"
15+
value: ${{ steps.get-grafana-key.outputs.key-name }}
16+
key:
17+
description: "The key value"
18+
value: ${{ steps.get-grafana-key.outputs.key }}
19+
20+
runs:
21+
using: 'composite'
22+
steps:
23+
- name: Get Grafana key
24+
id: get-grafana-key
25+
env:
26+
KEY_PREFIX: ${{ inputs.key-prefix }}
27+
shell: bash
28+
run: |
29+
KEY_NAME="$KEY_PREFIX-${{ github.run_id }}-${{ github.run_attempt }}-$RANDOM"
30+
echo $KEY_NAME
31+
KEY=$(aws grafana create-workspace-api-key --key-name "$KEY_NAME" --key-role "ADMIN" --seconds-to-live 300 --workspace-id "${{ inputs.workspace-id }}" | jq -r .key)
32+
echo "key-name=$KEY_NAME" >> $GITHUB_OUTPUT
33+
echo "key=$KEY" >> $GITHUB_OUTPUT

github/latest_release/action.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "Get latest release version"
2+
description: "Gets the version number of the latest release"
3+
4+
outputs:
5+
version:
6+
description: "The version number of the latest release"
7+
value: ${{ steps.clean_version.outputs.version }}
8+
9+
runs:
10+
using: 'composite'
11+
steps:
12+
- name: Get latest release
13+
id: latest_release
14+
uses: pozetroninc/github-action-get-latest-release@master
15+
with:
16+
repository: ${{ github.repository }}
17+
excludes: draft
18+
19+
- name: Get release value
20+
id: get_value
21+
uses: actions/github-script@v6
22+
env:
23+
LATEST_TAG: ${{ steps.latest_release.outputs.release }}
24+
with:
25+
result-encoding: string
26+
script: |
27+
if (context.eventName == "release") {
28+
return context.payload.release.tag_name
29+
} else {
30+
return process.env.LATEST_TAG
31+
}
32+
33+
- name: Clean version
34+
id: clean_version
35+
shell: bash
36+
run: |
37+
version=$(echo "${{ steps.get_value.outputs.result }}" | sed 's/v//g')
38+
echo "version=$version" >> $GITHUB_OUTPUT

terraform/apply/action.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: "Apply Terraform changes"
2+
description: "Deploys Terraform infrastructure"
3+
4+
inputs:
5+
environment:
6+
description: "The environment to deploy to"
7+
required: true
8+
terraform-path:
9+
description: "The path passed to Terraform e.g. -chdir=<terraform-path>"
10+
required: false
11+
default: 'terraform'
12+
13+
runs:
14+
using: 'composite'
15+
steps:
16+
- name: Terraform Apply
17+
id: apply
18+
shell: bash
19+
env:
20+
TF_WORKSPACE: ${{ inputs.environment }}
21+
run: terraform -chdir=${{ inputs.terraform-path }} apply -var-file="vars/${{ inputs.environment }}.tfvars" -auto-approve -no-color

terraform/check-fmt/action.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "Check Terraform format"
2+
description: "Validates that Terraform fmt is valid"
3+
4+
inputs:
5+
terraform-path:
6+
description: "The path passed to Terraform e.g. -chdir=<terraform-path>"
7+
required: false
8+
default: 'terraform'
9+
10+
runs:
11+
using: 'composite'
12+
steps:
13+
- name: Check Terraform Formatting
14+
id: fmt
15+
shell: bash
16+
continue-on-error: false
17+
run: terraform -chdir=${{ inputs.terraform-path }} fmt -recursive -check

terraform/init/action.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: "Init Terraform"
2+
description: "Initializes Terraform"
3+
4+
inputs:
5+
environment:
6+
description: "The running environment"
7+
required: true
8+
terraform-path:
9+
description: "The path passed to Terraform e.g. -chdir=<terraform-path>"
10+
required: false
11+
default: 'terraform'
12+
13+
runs:
14+
using: 'composite'
15+
steps:
16+
- name: Init Terraform
17+
id: init
18+
env:
19+
TF_WORKSPACE: ${{ inputs.environment }}
20+
shell: bash
21+
run: terraform -chdir=${{ inputs.terraform-path }} init -var-file="vars/${{ inputs.environment }}.tfvars" -no-color

terraform/plan/action.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: "Terraform Plan"
2+
description: "Run a Terraform plan"
3+
4+
inputs:
5+
environment:
6+
description: "The environment to deploy to"
7+
required: true
8+
terraform-path:
9+
description: "The path passed to Terraform e.g. -chdir=<terraform-path>"
10+
required: false
11+
default: 'terraform'
12+
github-token:
13+
description: "The GitHub token, needed to update PRs"
14+
required: false
15+
default: ''
16+
upload-plan-file:
17+
description: "Upload the plan file to the GitHub artifacts"
18+
required: false
19+
default: 'true'
20+
upload-output-file:
21+
description: "Upload the plan output to the GitHub artifacts"
22+
required: false
23+
default: 'true'
24+
add-output-to-pr:
25+
description: "Add the plan output to the current PR if there is one"
26+
required: false
27+
default: 'true'
28+
29+
outputs:
30+
plan-file:
31+
description: "Plan File"
32+
value: ${{ steps.plan.outputs.plan-file }}
33+
output-file:
34+
description: "Human Readable plan of action"
35+
value: ${{ steps.plan.outputs.output-file }}
36+
plan:
37+
description: "Human Readable plan of action"
38+
value: ${{ steps.plan.outputs.stdout }}
39+
40+
runs:
41+
using: 'composite'
42+
steps:
43+
- name: Terraform Plan
44+
id: plan
45+
shell: bash
46+
env:
47+
TF_WORKSPACE: ${{ inputs.environment }}
48+
run: |
49+
terraform -chdir=${{ inputs.terraform-path }} plan -var-file="vars/${{ inputs.environment }}.tfvars" -no-color -out=/tmp/plan.tfplan
50+
echo "plan-file=/tmp/plan.tfplan" >> $GITHUB_OUTPUT
51+
terraform -chdir=${{ inputs.terraform-path }} show -no-color /tmp/plan.tfplan > /tmp/plan.txt
52+
echo "output-file=/tmp/plan.txt" >> $GITHUB_OUTPUT
53+
54+
- uses: actions/upload-artifact@v3
55+
if: ${{ inputs.upload-plan-file == 'true' }}
56+
with:
57+
name: plan.tfplan
58+
path: ${{ steps.plan.outputs.plan-file }}
59+
60+
- uses: actions/upload-artifact@v3
61+
if: ${{ inputs.upload-output-file == 'true' }}
62+
with:
63+
name: plan.txt
64+
path: ${{ steps.plan.outputs.output-file }}
65+
66+
- name: Add Plan to PR
67+
if: ${{ github.event_name == 'pull_request' || inputs.add-output-to-pr == 'true' }}
68+
uses: actions/github-script@v6
69+
env:
70+
PLAN_FILE: ${{ steps.plan.outputs.output-file }}
71+
with:
72+
github-token: ${{ inputs.github-token }}
73+
script: |
74+
const { promises: fs } = require('fs');
75+
76+
const MAX_LENGTH = 65535;
77+
const ellipsis = `\n...\n`;
78+
79+
const prefix = `<details><summary>Show Plan</summary>
80+
81+
\`\`\`\n
82+
`;
83+
const postfix = `
84+
\`\`\`
85+
86+
</details>
87+
88+
*Action: \`${{ github.event_name }}\`*`;
89+
90+
let content = await fs.readFile(process.env.PLAN_FILE, 'utf8')
91+
let output = prefix + content + postfix;
92+
93+
if (output.length > MAX_LENGTH) {
94+
let l = MAX_LENGTH - prefix.length - postfix.length - ellipsis.length;
95+
content = content.slice(0, l);
96+
output = prefix + content + ellipsis + postfix;
97+
}
98+
99+
github.rest.issues.createComment({
100+
issue_number: context.issue.number,
101+
owner: context.repo.owner,
102+
repo: context.repo.repo,
103+
body: output
104+
});

0 commit comments

Comments
 (0)