Skip to content

Commit 9be1353

Browse files
committed
Continuous deployment
1 parent 607f8b5 commit 9be1353

File tree

2 files changed

+175
-119
lines changed

2 files changed

+175
-119
lines changed

.github/workflows/all.yml

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
name: Continuous Deployment
2+
3+
env:
4+
CI_IMAGE_NAME: api
5+
CI_DEV_API_PATH: /data/joinimpact/api
6+
7+
on:
8+
push:
9+
branches:
10+
- master
11+
- develop
12+
13+
jobs:
14+
lint:
15+
name: Lint
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- name: Install golint
21+
run: go get -u golang.org/x/lint/golint
22+
23+
- name: Run golint
24+
run: |
25+
export PATH="$PATH:$(dirname $(go list -f {{.Target}} golang.org/x/lint/golint))"
26+
make lint
27+
28+
build-push:
29+
runs-on: ubuntu-latest
30+
needs: test
31+
if: contains('
32+
refs/heads/master
33+
refs/heads/develop
34+
refs/heads/devops'
35+
, github.ref)
36+
37+
steps:
38+
- uses: actions/checkout@v2
39+
40+
- name: log into registry
41+
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
42+
43+
- name: Build image
44+
run: docker build . --pull --file Dockerfile --tag $CI_IMAGE_NAME
45+
46+
- name: Push image
47+
run: |
48+
CI_REGISTRY_IMAGE=$(echo "docker.pkg.github.com/${{ github.repository }}/$CI_IMAGE_NAME" | tr '[A-Z]' '[a-z]')
49+
CI_COMMIT_REF=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,' | tr '[A-Z]' '[a-z]')
50+
51+
echo CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE
52+
echo CI_COMMIT_REF=$CI_COMMIT_REF
53+
54+
#[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
55+
#[ "$LATEST" == "master" ] && LATEST=latest
56+
57+
docker tag $CI_IMAGE_NAME $CI_REGISTRY_IMAGE:$CI_COMMIT_REF
58+
docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF
59+
60+
deploy-dev:
61+
runs-on: ubuntu-latest
62+
needs: build-push
63+
if: contains('
64+
refs/heads/develop'
65+
, github.ref)
66+
67+
steps:
68+
- name: Add ssh key
69+
run: |
70+
printf ${{ secrets.DEV_SSH_KEY }} | base64 -d > /tmp/dev_ssh_key
71+
chmod 400 /tmp/dev_ssh_key
72+
cat /tmp/dev_ssh_key
73+
74+
# Before use add 'AcceptEnv CI_*' into /etc/ssh/sshd_config an restart sshd
75+
76+
- name: Pull from repo
77+
run: |
78+
export CI_REGISTRY_IMAGE=$(echo "docker.pkg.github.com/${{ github.repository }}/$CI_IMAGE_NAME" | tr '[A-Z]' '[a-z]') \
79+
export CI_COMMIT_REF=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') && \
80+
printf "bash -x -c 'cd $CI_DEV_API_PATH \
81+
&& git checkout $CI_COMMIT_REF \
82+
&& git pull origin $CI_COMMIT_REF'" \
83+
| ssh -T -o SendEnv="CI_*" \
84+
-o StrictHostKeyChecking=no \
85+
-o ConnectTimeout=30 \
86+
-o BatchMode=yes \
87+
-i /tmp/dev_ssh_key \
88+
-l ${{ secrets.DEV_SSH_USER }} \
89+
-p ${{ secrets.DEV_SSH_PORT }} \
90+
${{ secrets.DEV_SSH_HOST }}
91+
92+
# Before use do docker login docker.pkg.github.com -u token
93+
94+
- name: Deploy api
95+
run: |
96+
export CI_REGISTRY_IMAGE=$(echo "docker.pkg.github.com/${{ github.repository }}/$CI_IMAGE_NAME" | tr '[A-Z]' '[a-z]') \
97+
export CI_COMMIT_REF=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') && \
98+
printf "bash -x -c 'cd $CI_DEV_API_PATH \
99+
&& docker-compose stop \
100+
$CI_IMAGE_NAME \
101+
&& docker-compose rm -f \
102+
$CI_IMAGE_NAME \
103+
&& docker-compose pull -q \
104+
$CI_IMAGE_NAME \
105+
&& docker-compose up --no-start \
106+
$CI_IMAGE_NAME \
107+
&& docker-compose start \
108+
$CI_IMAGE_NAME'" \
109+
| ssh -T -o SendEnv="CI_*" \
110+
-o StrictHostKeyChecking=no \
111+
-o ConnectTimeout=30 \
112+
-o BatchMode=yes \
113+
-i /tmp/dev_ssh_key \
114+
-l ${{ secrets.DEV_SSH_USER }} \
115+
-p ${{ secrets.DEV_SSH_PORT }} \
116+
${{ secrets.DEV_SSH_HOST }}
117+
118+
deploy:
119+
runs-on: ubuntu-latest
120+
needs: build-push
121+
if: contains('
122+
refs/heads/master
123+
refs/heads/devops'
124+
, github.ref)
125+
126+
steps:
127+
- name: Add ssh key
128+
run: |
129+
printf ${{ secrets.PROD_SSH_KEY }} | base64 -d > /tmp/dev_ssh_key
130+
chmod 400 /tmp/dev_ssh_key
131+
cat /tmp/dev_ssh_key
132+
133+
# Before use add 'AcceptEnv CI_*' into /etc/ssh/sshd_config an restart sshd
134+
135+
- name: Pull from repo
136+
run: |
137+
export CI_REGISTRY_IMAGE=$(echo "docker.pkg.github.com/${{ github.repository }}/$CI_IMAGE_NAME" | tr '[A-Z]' '[a-z]') \
138+
export CI_COMMIT_REF=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') && \
139+
printf "bash -x -c 'cd $CI_DEV_API_PATH \
140+
&& git checkout $CI_COMMIT_REF \
141+
&& git pull origin $CI_COMMIT_REF'" \
142+
| ssh -T -o SendEnv="CI_*" \
143+
-o StrictHostKeyChecking=no \
144+
-o ConnectTimeout=30 \
145+
-o BatchMode=yes \
146+
-i /tmp/dev_ssh_key \
147+
-l ${{ secrets.PROD_SSH_USER }} \
148+
-p ${{ secrets.PROD_SSH_PORT }} \
149+
${{ secrets.PROD_SSH_HOST }}
150+
151+
# Before use do docker login docker.pkg.github.com -u token
152+
153+
- name: Deploy api
154+
run: |
155+
export CI_REGISTRY_IMAGE=$(echo "docker.pkg.github.com/${{ github.repository }}/$CI_IMAGE_NAME" | tr '[A-Z]' '[a-z]') \
156+
export CI_COMMIT_REF=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') && \
157+
printf "bash -x -c 'cd $CI_DEV_API_PATH \
158+
&& docker-compose stop \
159+
$CI_IMAGE_NAME \
160+
&& docker-compose rm -f \
161+
$CI_IMAGE_NAME \
162+
&& docker-compose pull -q \
163+
$CI_IMAGE_NAME \
164+
&& docker-compose up --no-start \
165+
$CI_IMAGE_NAME \
166+
&& docker-compose start \
167+
$CI_IMAGE_NAME'" \
168+
| ssh -T -o SendEnv="CI_*" \
169+
-o StrictHostKeyChecking=no \
170+
-o ConnectTimeout=30 \
171+
-o BatchMode=yes \
172+
-i /tmp/dev_ssh_key \
173+
-l ${{ secrets.PROD_SSH_USER }} \
174+
-p ${{ secrets.PROD_SSH_PORT }} \
175+
${{ secrets.PROD_SSH_HOST }}

0 commit comments

Comments
 (0)