-
Notifications
You must be signed in to change notification settings - Fork 10
148 lines (116 loc) · 5.22 KB
/
deploy-poller.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
name: Update Deploy Branch
on:
schedule:
- cron: '*/5 * * * *' # Runs every 5 minutes
workflow_dispatch: # Allows manual triggering of the workflow
inputs:
CHAIN_URL:
description: 'The URL of the chain. Example: https://emerynet.api.agoric.net'
required: false
default: ''
DEPLOY_BRANCH:
description: 'The branch to deploy. Example: deploy-emerynet'
required: false
default: ''
COMMIT_URL:
description: '(optional) Override ReferenceUI value with the url of the commit to deploy'
required: false
default: ''
jobs:
run-command:
runs-on: ubuntu-latest
env:
CHAIN_URL: ${{ github.event.inputs.CHAIN_URL || vars.CHAIN_URL }}
DEPLOY_BRANCH: ${{ github.event.inputs.DEPLOY_BRANCH || vars.DEPLOY_BRANCH }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install jq
run: sudo apt-get install -y jq
- name: Check for required variables
id: check_variables
run: |
SKIP_RUN=false
if [ -z "${CHAIN_URL}" ]; then
echo "CHAIN_URL is not set. Skipping workflow."
SKIP_RUN=true
fi
if [ -z "${DEPLOY_BRANCH}" ]; then
echo "CHAIN_URL is not set. Skipping workflow."
SKIP_RUN=true
fi
echo "SKIP_RUN=$SKIP_RUN" >> $GITHUB_ENV
- name: Check if deploy branch needs to be updated
if: env.SKIP_RUN == 'false'
run: |
# Define the URL from the environment variable
URL="${{ env.CHAIN_URL }}/agoric/vstorage/data/published.vaultFactory.governance"
# Fetch the new value
NEW_REFERENCED_UI=$(curl -s $URL | jq -r '.value' | jq -r '.values[]' | jq -r .body | sed 's/^#//' | jq -r .current.ReferencedUI.value)
if [ ${{ github.event.inputs.COMMIT_URL }} ]; then
NEW_REFERENCED_UI="${{ github.event.inputs.COMMIT_URL }}"
fi
# Check if the deploy branch exists on the remote
if git ls-remote --exit-code --heads origin ${{ env.DEPLOY_BRANCH }}; then
echo "Deploy branch exists. Checking out the branch."
git fetch origin
git checkout ${{ env.DEPLOY_BRANCH }}
# Path to store the previous value
OLD_REFERENCED_UI_FILE="./DEPLOYED_HASH"
# Check if the previous value exists
if [ -f "$OLD_REFERENCED_UI_FILE" ]; then
# Read the previous value
OLD_REFERENCED_UI=$(cat $OLD_REFERENCED_UI_FILE)
else
# File does not exist, set previous value to empty
OLD_REFERENCED_UI=""
fi
else
echo "Deploy branch does not exist. Skipping checkout."
# File does not exist, set previous value to empty
OLD_REFERENCED_UI=""
fi
echo "New value: $NEW_REFERENCED_UI"
echo "Old value: $OLD_REFERENCED_UI"
# Output the values for the next step
if [ "$NEW_REFERENCED_UI" != "$OLD_REFERENCED_UI" ]; then
SHOULD_DEPLOY=true
else
SHOULD_DEPLOY=false
fi
echo "SHOULD_DEPLOY=$SHOULD_DEPLOY" >> $GITHUB_ENV
echo "NEW_VALUE=$NEW_REFERENCED_UI" >> $GITHUB_ENV
- name: Deploy commit to branch
if: env.SHOULD_DEPLOY == 'true' && env.SKIP_RUN == 'false'
run: |
# Check out the specific commit
git fetch origin
REPO_URL=$(echo "${{ env.NEW_VALUE }}" | sed -E 's|/commit/.*||').git
COMMIT_HASH=$(echo "${{ env.NEW_VALUE }}" | awk -F'/commit/' '{print $2}')
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
# Clone the old repository and checkout the specific commit
git clone --single-branch $REPO_URL old-repo
cd old-repo
# Configure the remote URL with credentials
git remote add new-repo "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git"
git fetch new-repo
git checkout -b deploy $COMMIT_HASH
# GitHub Actions worker doesn't have the correct permissions to checked in
# modified workflow files. To workaround this, we'll remove all files in
# .github/workflows that doesn't exist on the remote
# Remove files not on the remote
rm -rf .github/workflows
# Check if the deploy branch exists on the remote
if git ls-remote --exit-code --heads origin ${{ env.DEPLOY_BRANCH }}; then
# Mirror remote workflows if they exist
if git ls-tree --name-only -r new-repo/${{ vars.DEPLOY_BRANCH }}:.github/workflows &> /dev/null; then
git checkout new-repo/${{ vars.DEPLOY_BRANCH }} .github/workflows
fi
fi
# Save the new value to the DEPLOYED_HASH file
echo "$NEW_VALUE" > ./DEPLOYED_HASH
git add ./DEPLOYED_HASH
git commit -am "Prepare branch for deploy"
# # Push the commit to the deploy branch in the new repository
git push -f new-repo HEAD:${{ env.DEPLOY_BRANCH }}