-
Notifications
You must be signed in to change notification settings - Fork 6
71 lines (67 loc) · 2.4 KB
/
format.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
name: 'Terraform Format'
on:
workflow_call:
inputs:
terraform_version:
description: Which version of terraform to use
type: string
required: true
base_path:
description: Base path to all terraform files
type: string
required: true
jobs:
format:
name: Format
runs-on: [self-hosted, Linux, AWS]
timeout-minutes: 10
steps:
- uses: Brightspace/third-party-actions@hashicorp/setup-terraform
with:
terraform_version: ${{ inputs.terraform_version }}
terraform_wrapper: false
- uses: Brightspace/third-party-actions@actions/checkout
with:
ref: ${{ github.head_ref || github.ref }}
- name: format
working-directory: ${{ inputs.base_path }}
id: format
env:
GIT_AUTHOR_NAME: Terraform Format Bot
GIT_AUTHOR_EMAIL: [email protected]
GIT_COMMITTER_NAME: Terraform Format Bot
GIT_COMMITTER_EMAIL: [email protected]
shell: bash
run: |
if [ "$( terraform fmt -recursive )" == "" ]; then
exit 0
fi
git add *
git commit -m 'auto format'
RANDOM_STR=$( head /dev/urandom | tr -dc a-z0-9 | head -c 4 )
git push origin "HEAD:refs/heads/auto-format-$RANDOM_STR"
echo "format_branch=auto-format-$RANDOM_STR" >> $GITHUB_OUTPUT
exit 1
- name: create format-fixing PR
if: failure() && steps.format.outputs.format_branch
uses: Brightspace/third-party-actions@actions/github-script
env:
BRANCH_NAME: ${{ steps.format.outputs.format_branch }}
with:
script: |
const { BRANCH_NAME } = process.env
const { data: newPr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `Auto format #${context.payload.number}`,
body: `Auto format #${context.payload.number}`,
head: BRANCH_NAME,
base: context.payload.pull_request.head.ref,
draft: true
});
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `PR #${newPr.number} created to fix Terraform format.`
})