forked from salesagility/SuiteCRM
-
Notifications
You must be signed in to change notification settings - Fork 2
185 lines (185 loc) · 9.29 KB
/
Develop_CreateInstanceOnOpenedPR.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
name: Develop - Create Instance on Opened or Reopened PR
on:
pull_request:
types: [opened, reopened]
branches: # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestbranchestags
- '**' # matches every branch
workflow_dispatch:
env:
DEVELOP_HOST: ${{ secrets.DEVELOP_HOST}}
DEVELOP_SSH_USERNAME: ${{ secrets.DEVELOP_SSH_USERNAME}}
DEVELOP_DOMAIN_NAME: ${{ secrets.DEVELOP_DOMAIN_NAME}}
MAIN_HOST: ${{ secrets.MAIN_HOST}}
MAIN_SSH_USERNAME: ${{ secrets.MAIN_SSH_USERNAME}}
jobs:
create_instance:
name: Create Instance on Opened or Reopened PR
runs-on: ubuntu-latest
steps:
- name: Extract branch name
run: echo ${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}} && echo 'BRANCH='${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}} >> $GITHUB_ENV
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: unnecessary
# - name: Adding Known Hosts Develop
# run: ssh-keyscan -H $DEVELOP_HOST >> ~/.ssh/known_hosts
# - name: Adding Known Hosts Main
# run: ssh-keyscan -H $MAIN_HOST >> ~/.ssh/known_hosts
- name: Get Available database name
run:
DATABASE_NAME=$(ssh -o "StrictHostKeyChecking no" $DEVELOP_SSH_USERNAME@$DEVELOP_HOST "mysql -h 127.0.0.1 -u '${{ secrets.DEVELOP_DB_USERNAME }}' --password='${{ secrets.DEVELOP_DB_PASSWORD }}' -N -e 'select schema_name
from information_schema.schemata
where
SCHEMA_NAME not in (
SELECT
TABLE_SCHEMA
from information_schema.tables
where table_name like \"stic%\"
group by TABLE_SCHEMA
)
and SCHEMA_NAME like \"mydevelop%\"
order by SCHEMA_NAME LIMIT 1'") &&
echo $DATABASE_NAME &&
(echo "DATABASE_NAME=$DATABASE_NAME" >> $GITHUB_ENV)
- name: Extract branch clean
run: echo 'BRANCH_CLEAN='`echo ${{ env.BRANCH }} | sed -e 's/[^a-zA-Z0-9_/]//g'` >> $GITHUB_ENV
- name: Set current date as env variable
run: echo 'NOW='$(date +'%Y-%m-%d %H:%M:%S') >> $GITHUB_ENV
- name: echo environment variables
run: echo ${{ env }}
# - name: Delete develop instance
# run: ssh -o "StrictHostKeyChecking no" $DEVELOP_SSH_USERNAME@$DEVELOP_HOST "test -e /web/${{ env.BRANCH_CLEAN }}" && (echo "Directory branch exists" && ssh -o "StrictHostKeyChecking no" $DEVELOP_SSH_USERNAME@$DEVELOP_HOST "rm -rf /web/${{ env.BRANCH_CLEAN }} && mysql -h 127.0.0.1 -u $DEVELOP_DB_USERNAME --password=$DEVELOP_DB_PASSWORD -e 'drop database if exists ${{ env.DATABASE_NAME }}; create database ${{ env.DATABASE_NAME }};'") || echo "Branch doesn't exist in develop"
- name: Create Instance in develop through Main
run: ssh -o "StrictHostKeyChecking no" $MAIN_SSH_USERNAME@$MAIN_HOST "cd scripts/create && bash create.sh -b ${{ env.BRANCH }} -i $DEVELOP_DOMAIN_NAME -p /web/${{ env.BRANCH_CLEAN }} -db ${{ env.DATABASE_NAME }} -d -y"
- name: Set Clean git branch
run: ssh -o "StrictHostKeyChecking no" $DEVELOP_SSH_USERNAME@$DEVELOP_HOST "test -e /web/${{ env.BRANCH_CLEAN }}" && (echo "Directory branch exists" && ssh -o "StrictHostKeyChecking no" $DEVELOP_SSH_USERNAME@$DEVELOP_HOST "cd /web/${{ env.BRANCH_CLEAN }} && git stash && git reset && git pull")
- name: Get project data
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
ORGANIZATION: SinergiaTIC
PROJECT_NUMBER: 2
PR_ID: ${{ github.event.pull_request.node_id }}
run: |
gh api graphql -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectV2(number: $number) {
id
fields(first:20) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
items(first:100) {
nodes {
... on ProjectV2Item {
id
}
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectV2.id' project_data.json) >> $GITHUB_ENV
echo 'URL_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "URLValidationInstance") | .id' project_data.json) >> $GITHUB_ENV
echo 'DB_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "DBValidationInstance") | .id' project_data.json) >> $GITHUB_ENV
echo 'DATE_FIELD_ID='$(jq '.data.organization.projectV2.fields.nodes[] | select(.name== "DateValidationInstance") | .id' project_data.json) >> $GITHUB_ENV
echo 'CURRENT_DATE='$(date +'%Y-%m-%d') >> $GITHUB_ENV
- name: Get PR Item ID
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
PR_ID: ${{ github.event.pull_request.node_id }}
run: |
item_id="$( gh api graphql -f query='
mutation($project:ID!, $pr:ID!) {
addProjectV2ItemById(input: {projectId: $project, contentId: $pr}) {
item {
id
}
}
}' -f project=$PROJECT_ID -f pr=$PR_ID --jq '.data.addProjectV2ItemById.item.id')"
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
- name: Change Fields
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$url_field: ID!
$url_value: String!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $url_field
value: {
text: $url_value
}
}) {
projectV2Item {
id
}
}
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f url_field=$URL_FIELD_ID -f url_value=https://${{ env.DEVELOP_DOMAIN_NAME}}/${{ env.BRANCH_CLEAN }} --silent
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$db_field: ID!
$db_value: String!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $db_field
value: {
text: $db_value
}
}) {
projectV2Item {
id
}
}
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f db_field=$DB_FIELD_ID -f db_value=${{ env.DATABASE_NAME }} --silent
gh api graphql -f query='
mutation (
$project: ID!
$item: ID!
$date_field: ID!
$date_value: Date!
) {
set_status: updateProjectV2ItemFieldValue(input: {
projectId: $project
itemId: $item
fieldId: $date_field
value: {
date: $date_value
}
}) {
projectV2Item {
id
}
}
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f date_field=$DATE_FIELD_ID -f date_value=$CURRENT_DATE --silent
- name: Create or Edit PR comment
uses: thollander/actions-comment-pull-request@v1
with:
message: |
Actions executed at: ${{ env.NOW }}.
comment_includes: 'Actions executed at:'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}