-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaction.yml
168 lines (153 loc) · 7.74 KB
/
action.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
name: 'Neon Database Create Branch Action'
author: 'Neon Database'
description: 'Creates a new Neon Postgres branch based a parent branch. If the branch already exists it will return the branch details'
branding:
icon: 'box'
color: 'green'
inputs:
project_id:
required: true
description: 'The project id'
branch_name:
required: false
description: 'The branch name'
api_key:
description: 'The Neon API key'
required: true
api_host:
description: 'The Neon API Host'
default: 'https://console.neon.tech/api/v2'
username:
description: 'The db role name'
required: true
database:
description: 'The database name'
default: neondb
prisma:
description: 'Use prisma or not'
default: 'false'
parent:
description: 'The parent branch name or id or LSN or timestamp. By default the primary branch is used'
suspend_timeout:
description: >
Duration of inactivity in seconds after which the compute endpoint is
For more information, see [Auto-suspend configuration](https://neon.tech/docs/manage/endpoints#auto-suspend-configuration).
default: '0'
ssl:
description: >
Add sslmode to the connection string. Supported values are: "require", "verify-ca", "verify-full", "omit".
default: 'require'
outputs:
db_url:
description: 'New branch DATABASE_URL'
value: ${{ steps.create-branch.outputs.db_url }}
db_url_with_pooler:
description: 'New branch DATABASE_URL with pooling enabled'
value: ${{ steps.create-branch.outputs.db_url_with_pooler }}
host:
description: 'New branch host'
value: ${{ steps.create-branch.outputs.host }}
host_with_pooler:
description: 'New branch host with pooling enabled'
value: ${{ steps.create-branch.outputs.host_with_pooler }}
branch_id:
description: 'New branch id'
value: ${{ steps.create-branch.outputs.branch_id }}
password:
description: 'Password for connecting to the new branch database with the input username'
value: ${{ steps.create-branch.outputs.password }}
runs:
using: 'composite'
steps:
- uses: actions/setup-node@v4
- run: npm i -g neonctl@2
shell: bash
- name: Create new Neon branch
env:
NEON_API_KEY: ${{ inputs.api_key }}
NEON_API_HOST: ${{ inputs.api_host }}
# Annotation data, extracted from github.* variables to be displayed in the Console.
# Must be moved to env due to security recommendations.
# Action can be triggered by different events (push, pull_request, for example).
# Values are extracted the most generic way possible.
ANNOTATION_REPOSITORY: ${{ github.repository }}
ANNOTATION_ACTOR_LOGIN: ${{ github.actor }}
ANNOTATION_PR_NUMBER: ${{ github.event.pull_request.number || '' }}
ANNOTATION_PR_TITLE: ${{ github.event.pull_request.title || '' }}
ANNOTATION_COMMIT_SHA: ${{ github.event.after || '' }}
ANNOTATION_COMMIT_REF: ${{ github.event.ref || github.event.pull_request.head.ref || '' }}
ANNOTATION_COMMIT_MESSAGE: ${{ github.event.head_commit.message || '' }}
ANNOTATION_ACTION_REF: ${{ github.action_ref || '' }}
id: create-branch
shell: bash
run: |
# branch name can be prefixed with "refs/heads/"
ANNOTATION_COMMIT_REF=${ANNOTATION_COMMIT_REF/refs\/heads\//}
# construct compact annotation JSON from scratch with all the parameters and filter our all empty values.
# if there is no values at all, annotation_json will be empty string.
annotation_json=$(jq -cnr '$ARGS.named | with_entries(select(.value != ""))' \
--arg github-commit-repo "${ANNOTATION_REPOSITORY}" \
--arg github-commit-author-login "${ANNOTATION_ACTOR_LOGIN}" \
--arg github-commit-sha "${ANNOTATION_COMMIT_SHA}" \
--arg github-commit-message "${ANNOTATION_COMMIT_MESSAGE}" \
--arg github-pr-number "${ANNOTATION_PR_NUMBER}" \
--arg github-pr-title "${ANNOTATION_PR_TITLE}" \
--arg github-commit-ref "${ANNOTATION_COMMIT_REF}" \
--arg github-action-ref "${ANNOTATION_ACTION_REF}" \
)
# set IFS (internal file separator) to \n to avoid JSON space being an arguments separator
IFS=$'\n'
neonctl branches create \
--project-id ${{ inputs.project_id }} \
--name "${{ inputs.branch_name }}" \
--suspend-timeout ${{ inputs.suspend_timeout }} \
$(if [[ -n "${{ inputs.parent }}" ]]; then echo "--parent=${{ inputs.parent }}"; fi) \
--output json \
$(if [[ -n "${annotation_json}" ]]; then echo "--annotation='$annotation_json'"; fi) \
2> branch_err > branch_out || true
echo "::add-mask::$(cat branch_out | jq -r '.connection_uris[0].connection_parameters.password')"
echo "branch create out:"
cat branch_out
if [[ -s branch_err ]]; then
echo "branch create err:"
cat branch_err
fi
if [[ $(cat branch_err) == *"already exists"* ]]; then
# Get the branch id by its name. We list all branches and filter by name
branch_id=$(neonctl branches list --project-id ${{ inputs.project_id }} -o json \
| jq -r '.[] | select(.name == "${{ inputs.branch_name }}") | .id')
echo "branch exists, branch id: ${branch_id}"
echo "branch_id=${branch_id}" >> $GITHUB_OUTPUT
NEON_CS=$(neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --ssl ${{ inputs.ssl }} --prisma ${{ inputs.prisma }} --extended -o json)
DB_URL=$(echo $NEON_CS | jq -r '.connection_string')
DB_HOST=$(echo $NEON_CS | jq -r '.host')
DB_PASSWORD=$(echo $NEON_CS | jq -r '.password')
NEON_CS_POOLER=$(neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --ssl ${{ inputs.ssl }} --pooled --prisma ${{ inputs.prisma }} --extended -o json)
DB_URL_WITH_POOLER=$(echo $NEON_CS_POOLER | jq -r '.connection_string')
DB_HOST_WITH_POOLER=$(echo $NEON_CS_POOLER | jq -r '.host')
elif [[ $(cat branch_err) == *"ERROR:"* ]]; then
echo "ERROR: branch creation failed"
cat branch_err
exit 1
else
branch_id=$(cat branch_out | jq --raw-output '.branch.id')
if [[ -z "${branch_id}" ]]; then
echo "ERROR: didn't get the branch id"
exit 1
fi
echo "branch doesn't exist, branch id: ${branch_id}"
echo "branch_id=${branch_id}" >> $GITHUB_OUTPUT
NEON_CS=$(neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --ssl ${{ inputs.ssl }} --prisma ${{ inputs.prisma }} --extended -o json)
DB_URL=$(echo $NEON_CS | jq -r '.connection_string')
DB_HOST=$(echo $NEON_CS | jq -r '.host')
DB_PASSWORD=$(echo $NEON_CS | jq -r '.password')
NEON_CS_POOLER=$(neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --ssl ${{ inputs.ssl }} --pooled --prisma ${{ inputs.prisma }} --extended -o json)
DB_URL_WITH_POOLER=$(echo $NEON_CS_POOLER | jq -r '.connection_string')
DB_HOST_WITH_POOLER=$(echo $NEON_CS_POOLER | jq -r '.host')
fi
echo "::add-mask::$DB_PASSWORD"
echo "db_url=${DB_URL}" >> $GITHUB_OUTPUT
echo "db_url_with_pooler=${DB_URL_WITH_POOLER}" >> $GITHUB_OUTPUT
echo "password=${DB_PASSWORD}" >> $GITHUB_OUTPUT
echo "host=${DB_HOST}" >> $GITHUB_OUTPUT
echo "host_with_pooler=${DB_HOST_WITH_POOLER}" >> $GITHUB_OUTPUT