-
Notifications
You must be signed in to change notification settings - Fork 21
149 lines (128 loc) · 6.47 KB
/
deploy-with-docker-prod.yaml
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
# This is a basic workflow to help you get started with Actions
name: CI/CD For docker deploy
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches:
- main
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build-push-docker:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_SERVER_KEY }}
name: id_rsa # optional
known_hosts: ${{ secrets.KNOWN_HOSTS }}
# - uses: whoan/docker-build-with-cache-action@v5
# with:
# username: fredkiss3
# password: ${{ secrets.DCR_PASSWD }}
# image_name: gh-next
# image_tag: latest
# push_git_tag: true
# registry: dcr.fredkiss.dev
# dockerfile: docker/Dockerfile.prod
# context: .
# build_extra_args: "--build-arg NEXT_PUBLIC_VERCEL_URL=gh.fredkiss.dev --build-arg GITHUB_REDIRECT_URI=https://gh.fredkiss.dev/api/auth/callback --build-arg SESSION_SECRET=${{ secrets.SESSION_SECRET }} --build-arg DATABASE_URL=${{ secrets.POSTGRES_DB_URL }} --build-arg GITHUB_CLIENT_ID=${{ secrets.GH_CLIENT_ID }} --build-arg GITHUB_SECRET=${{ secrets.GH_SECRET }} --build-arg GITHUB_PERSONAL_ACCESS_TOKEN=${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} --build-arg REDIS_HTTP_USERNAME=${{ secrets.REDIS_HTTP_USERNAME }} --build-arg REDIS_HTTP_PASSWORD=${{ secrets.REDIS_HTTP_PASSWORD }} --build-arg KV_PREFIX=__gh_next__cache_prod_"
- name: Deploy to Server
run: |
ssh -p $DEPLOY_PORT $DEPLOY_USER@$DEPLOY_DOMAIN "
source ~/.zshrc
set -e -o errexit
cd $DEPLOY_DIR
echo Login to docker registry...
echo ${{ secrets.DCR_PASSWD }} | docker login --username=fredkiss3 --password-stdin dcr.fredkiss.dev
echo succesfully logged in to registry dcr.fredkiss.dev ✅
echo use node version 20
nvm use 20
echo Pulling latest version...
git pull origin ${GITHUB_REF##*/}
echo Build with docker (and cache)...🔄
export DOCKER_BUILDKIT=1
# Use cache from remote repository, tag as latest, keep cache metadata
docker buildx build . -f docker/Dockerfile.prod \
-t dcr.fredkiss.dev/gh-next:${GITHUB_SHA} -t dcr.fredkiss.dev/gh-next:latest \
--cache-from type=registry,ref=dcr.fredkiss.dev/gh-next:tag-buildcache,mode=max \
--cache-to type=registry,ref=dcr.fredkiss.dev/gh-next:tag-buildcache,mode=max \
--build-arg NEXT_PUBLIC_VERCEL_URL=gh.fredkiss.dev \
--build-arg GITHUB_REDIRECT_URI=https://gh.fredkiss.dev/api/auth/callback \
--build-arg SESSION_SECRET=${{ secrets.SESSION_SECRET }} \
--build-arg DATABASE_URL=${{ secrets.POSTGRES_DB_URL }} \
--build-arg GITHUB_CLIENT_ID=${{ secrets.GH_CLIENT_ID }} \
--build-arg GITHUB_SECRET=${{ secrets.GH_SECRET }} \
--build-arg GITHUB_PERSONAL_ACCESS_TOKEN=${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} \
--build-arg REDIS_HTTP_USERNAME=${{ secrets.REDIS_HTTP_USERNAME }} \
--build-arg REDIS_HTTP_PASSWORD=${{ secrets.REDIS_HTTP_PASSWORD }} \
--build-arg KV_PREFIX=__gh_next__cache_prod_
echo build successful ✅
echo Pushing docker tags...🔄
docker push dcr.fredkiss.dev/gh-next:${GITHUB_SHA}
docker push dcr.fredkiss.dev/gh-next:latest
echo docker tags pushed succesfully .✅
# Start docker instances
echo updating docker services...🔄
docker stack deploy --with-registry-auth --compose-file ./docker/docker-stack.prod.yaml gh-stack-prod
echo services updated succesfully ✅
# Copying the build cache for faster builds
echo copying build cache...🔄
docker pull dcr.fredkiss.dev/gh-next:latest
CONTAINER_ID=$(docker create dcr.fredkiss.dev/gh-next:latest)
mkdir -p .next/
docker cp ${CONTAINER_ID}:/app/.next/cache .next/
docker rm ${CONTAINER_ID}
"
env:
DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
DEPLOY_DIR: ${{ secrets.DEPLOY_DIR }}
DEPLOY_DOMAIN: ${{ secrets.DEPLOY_DOMAIN }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
# - name: copy build cache
# run: |
# ls -l ./.next/cache 2> /dev/null || true
# echo ${{ secrets.DCR_PASSWD }} | docker login --username=fredkiss3 --password-stdin dcr.fredkiss.dev
# docker pull dcr.fredkiss.dev/gh-next:latest
# CONTAINER_ID=$(docker create dcr.fredkiss.dev/gh-next:latest)
# mkdir -p .next/
# docker cp ${CONTAINER_ID}:/app/.next/cache .next/
# docker rm ${CONTAINER_ID}
# ls -l ./.next/cache
# deploy:
# # The type of runner that the job will run on
# needs:
# - build-push-docker
# runs-on: ubuntu-latest
# environment: dev
# # Steps represent a sequence of tasks that will be executed as part of the job
# steps:
# - uses: actions/checkout@v2
# - name: Install SSH key
# uses: shimataro/ssh-key-action@v2
# with:
# key: ${{ secrets.SSH_SERVER_KEY }}
# name: id_rsa # optional
# known_hosts: ${{ secrets.KNOWN_HOSTS }}
# - name: Restart docker-stack
# run: |
# scp -P $DEPLOY_PORT ./docker/docker-stack.prod.yaml $DEPLOY_USER@$DEPLOY_DOMAIN:$DCR_DEPLOY_DIR/docker-stack.prod.yaml
# ssh -p $DEPLOY_PORT $DEPLOY_USER@$DEPLOY_DOMAIN "
# cd $DCR_DEPLOY_DIR
# echo updating docker services for dev environment...
# echo ${{ secrets.DCR_PASSWD }} | docker login --username=fredkiss3 --password-stdin dcr.fredkiss.dev
# docker stack deploy --with-registry-auth --compose-file ./docker-stack.prod.yaml gh-stack-prod
# echo services updated succesfully ✅
# "
# env:
# DCR_DEPLOY_DIR: ${{ secrets.DCR_DEPLOY_DIR }}
# DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
# DEPLOY_DOMAIN: ${{ secrets.DEPLOY_DOMAIN }}
# DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
# DCR_PASSWD: ${{ secrets.DCR_PASSWD }}
# DCR_USER: ${{ secrets.DCR_USER }}