-
-
Notifications
You must be signed in to change notification settings - Fork 5
188 lines (169 loc) · 6.42 KB
/
Copy pathdeploy.yml
File metadata and controls
188 lines (169 loc) · 6.42 KB
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
186
187
188
name: deploy
on:
workflow_call:
inputs:
channel:
description: 'Deploy target (main or canary)'
required: true
type: string
deploy_title:
description: 'Dokploy deployment title (e.g. triggering commit subject)'
required: false
type: string
default: ''
deploy_version:
description: 'App version baked into the image (without v prefix)'
required: false
type: string
default: ''
deploy_description:
description: 'Dokploy deployment description (e.g. version tag)'
required: false
type: string
default: ''
workflow_dispatch:
inputs:
channel:
description: 'Deploy target (main or canary); leave empty to auto-detect'
required: false
type: string
default: ''
deploy_title:
description: 'Dokploy deployment title'
required: false
type: string
default: ''
deploy_version:
description: 'App version baked into the image (without v prefix)'
required: false
type: string
default: ''
deploy_description:
description: 'Dokploy deployment description'
required: false
type: string
default: ''
jobs:
resolve:
runs-on: ubuntu-latest
outputs:
channel: ${{ steps.meta.outputs.channel }}
environment_url: ${{ steps.meta.outputs.environment_url }}
steps:
- name: Checkout repository
if: inputs.channel == ''
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Resolve deploy channel
id: meta
run: |
CHANNEL="${{ inputs.channel }}"
if [[ -z "$CHANNEL" ]]; then
REF="${GITHUB_REF#refs/heads/}"
if [[ "$REF" == "main" || "$REF" == "canary" ]]; then
CHANNEL="$REF"
else
git fetch origin main canary
SHA="${{ github.sha }}"
MAIN_HEAD=$(git rev-parse origin/main)
CANARY_HEAD=$(git rev-parse origin/canary)
if [[ "$SHA" == "$MAIN_HEAD" ]]; then
CHANNEL=main
elif [[ "$SHA" == "$CANARY_HEAD" ]]; then
CHANNEL=canary
elif git branch -r --contains "$SHA" | grep -qE 'origin/canary$'; then
CHANNEL=canary
elif git branch -r --contains "$SHA" | grep -qE 'origin/main$'; then
CHANNEL=main
else
echo "Could not resolve deploy channel" >&2
exit 1
fi
fi
fi
if [[ "$CHANNEL" != "main" && "$CHANNEL" != "canary" ]]; then
echo "Invalid channel: $CHANNEL" >&2
exit 1
fi
case "$CHANNEL" in
main) ENVIRONMENT_URL="https://pfcontrol.com" ;;
canary) ENVIRONMENT_URL="https://canary.pfcontrol.com" ;;
esac
echo "channel=$CHANNEL" >> "$GITHUB_OUTPUT"
echo "environment_url=$ENVIRONMENT_URL" >> "$GITHUB_OUTPUT"
echo "Deploying to $CHANNEL ($ENVIRONMENT_URL)"
deploy:
needs: resolve
runs-on: ubuntu-latest
permissions:
contents: read
deployments: write
environment:
name: ${{ needs.resolve.outputs.channel }}
url: ${{ needs.resolve.outputs.environment_url }}
steps:
- name: Configure Dokploy app version
if: inputs.deploy_version != ''
env:
DEPLOY_VERSION: ${{ inputs.deploy_version }}
run: |
case "${{ needs.resolve.outputs.channel }}" in
canary) APP_ID="IIakyrrLChtjnjuKKVFiB" ;;
main) APP_ID="C4Vzp1quDF4mKt2AQPjkU" ;;
*)
echo "Unknown channel: ${{ needs.resolve.outputs.channel }}" >&2
exit 1
;;
esac
API_BASE="https://${{ secrets.DOKPLOY_HOST }}/api"
AUTH=(-H "x-api-key: ${{ secrets.DOKPLOY_API_TOKEN }}")
APP_JSON=$(curl -fsS "${API_BASE}/application.one?applicationId=${APP_ID}" "${AUTH[@]}")
# Runtime env (syncVersionFromEnv) and Docker build-args (ARG APP_VERSION) must both be set;
# Dokploy does not pass application env into docker build (see dokploy/dokploy#1244).
CURRENT_ENV=$(echo "$APP_JSON" | jq -r '.env // ""')
NEW_ENV=$(printf '%s\n' "$CURRENT_ENV" | grep -v '^APP_VERSION=' | sed '/^$/d')
NEW_ENV="${NEW_ENV}"$'\n'"APP_VERSION=${DEPLOY_VERSION}"
CURRENT_ARGS=$(echo "$APP_JSON" | jq -r '.buildArgs // ""')
NEW_ARGS=$(printf '%s\n' "$CURRENT_ARGS" | grep -v '^APP_VERSION=' | sed '/^$/d')
NEW_ARGS="${NEW_ARGS}"$'\n'"APP_VERSION=${DEPLOY_VERSION}"
PAYLOAD=$(echo "$APP_JSON" | jq \
--arg applicationId "$APP_ID" \
--arg env "$NEW_ENV" \
--arg buildArgs "$NEW_ARGS" \
'{
applicationId: $applicationId,
env: $env,
buildArgs: $buildArgs,
buildSecrets: (.buildSecrets // ""),
createEnvFile: (.createEnvFile // true)
}')
curl -fsS -X POST "${API_BASE}/application.saveEnvironment" \
"${AUTH[@]}" \
-H "Content-Type: application/json" \
-d "$PAYLOAD"
echo "Configured APP_VERSION=${DEPLOY_VERSION} for ${{ needs.resolve.outputs.channel }}"
- name: Trigger Dokploy deploy
env:
DEPLOY_TITLE: ${{ inputs.deploy_title }}
DEPLOY_DESCRIPTION: ${{ inputs.deploy_description }}
run: |
case "${{ needs.resolve.outputs.channel }}" in
canary) APP_ID="IIakyrrLChtjnjuKKVFiB" ;;
main) APP_ID="C4Vzp1quDF4mKt2AQPjkU" ;;
*)
echo "Unknown channel: ${{ needs.resolve.outputs.channel }}" >&2
exit 1
;;
esac
PAYLOAD=$(jq -n \
--arg applicationId "$APP_ID" \
--arg title "$DEPLOY_TITLE" \
--arg description "$DEPLOY_DESCRIPTION" \
'{applicationId: $applicationId}
+ (if ($title | length) > 0 then {title: $title} else {} end)
+ (if ($description | length) > 0 then {description: $description} else {} end)')
curl -fsS -X POST "https://${{ secrets.DOKPLOY_HOST }}/api/application.deploy" \
-H "x-api-key: ${{ secrets.DOKPLOY_API_TOKEN }}" \
-H "Content-Type: application/json" \
-d "$PAYLOAD"