forked from neondatabase/create-branch-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
91 lines (78 loc) · 3.33 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
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
type: string
branch_name:
required: false
type: string
api_key:
required: true
username:
required: true
database:
type: string
default: neondb
prisma:
type: boolean
default: false
outputs:
db_url:
description: 'New branch DATABASE_URL'
value: ${{ steps.create-branch.outputs.db_url }}
db_url_with_pooler:
description: 'New branch DATABASE_URL'
value: ${{ steps.create-branch.outputs.db_url_with_pooler }}
host:
description: 'New branch host'
value: ${{ steps.create-branch.outputs.host }}
branch_id:
description: 'New branch id'
value: ${{ steps.create-branch.outputs.branch_id }}
runs:
using: 'composite'
steps:
- uses: actions/setup-node@v3
- run: yarn add global [email protected]
shell: bash
- name: Create new Neon branch
env:
NEON_API_KEY: ${{ inputs.api_key }}
id: create-branch
shell: bash
run: |
yarn -s neonctl branches create \
--project-id ${{ inputs.project_id }} \
--name ${{ inputs.branch_name }} \
--type read_write -o json \
2> branch_err > branch_out || true
echo "branch create result:\n" >> debug.log
cat branch_out >> debug.log
if [[ $(cat branch_err) == *"already exists"* ]]; then
# Get the branch id by its name. We list all branches and filter by name
branch_id=$(yarn -s neonctl branches list --project-id ${{ inputs.project_id }} -o json \
| jq -c '.[] | select(.name | contains("'${{ inputs.branch_name }}'")) .id' \
| jq -r)
echo "branch exists, branch id: ${branch_id}" >> debug.log
echo "branch_id=${branch_id}" >> $GITHUB_OUTPUT
echo "db_url=$(yarn -s neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --prisma ${{ inputs.prisma }})" >> $GITHUB_OUTPUT
echo "db_url_with_pooler=$(yarn -s neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --pooled --prisma ${{ inputs.prisma }})" >> $GITHUB_OUTPUT
else
branch_id=$(cat branch_out | jq --raw-output '.branch.id')
host=$(cat branch_out | jq --raw-output '.endpoints[0].host')
echo "branch doesn't exist, branch id: ${branch_id}" >> debug.log
echo "branch_id=${branch_id}" >> $GITHUB_OUTPUT
echo "host=${host}" >> $GITHUB_OUTPUT
echo "db_url=$(yarn -s neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --prisma ${{ inputs.prisma }})" >> $GITHUB_OUTPUT
echo "db_url_with_pooler=$(yarn -s neonctl cs ${branch_id} --project-id ${{ inputs.project_id }} --role-name ${{ inputs.username }} --database-name ${{ inputs.database }} --pooled --prisma ${{ inputs.prisma }})" >> $GITHUB_OUTPUT
fi
- uses: actions/upload-artifact@v3
if: always()
with:
name: Create branch log
path: debug.log