-
Notifications
You must be signed in to change notification settings - Fork 9
69 lines (59 loc) · 2.33 KB
/
prod-ci.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
name: prod-api-deployment
on:
workflow_dispatch:
env:
CONTAINER_IMAGE: platform-api
CONTAINER_NAME: api
CONTAINER_PORT: 8000
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- id: get_version
name: get API version from package.json
run: |
version=`cat ./apps/api/package.json | jq -r '.version'`
# end of optional handling for multi line json
echo "version=$version" >> $GITHUB_OUTPUT
- name: Build and push
id: build_and_push
uses: docker/build-push-action@v4
with:
context: .
file: ./apps/api/Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{env.CONTAINER_IMAGE}}:latest, ${{ secrets.DOCKERHUB_USERNAME }}/${{env.CONTAINER_IMAGE}}:v${{steps.get_version.outputs.version}}
outputs:
container_tag: v${{steps.get_version.outputs.version}}
deploy:
runs-on: ubuntu-latest
needs: build
env:
CONTAINER_TAG: ${{needs.build.outputs.container_tag}}
steps:
- name: deploy via ssh
uses: appleboy/[email protected]
with:
host: ${{ secrets.PROD_SSH_HOST }}
username: ${{ secrets.PROD_SSH_USERNAME }}
key: ${{ secrets.PROD_SSH_KEY }}
envs: CONTAINER_NAME,CONTAINER_PORT,CONTAINER_TAG,CONTAINER_IMAGE
passphrase: ${{ secrets.PROD_SSH_PASSPHRASE }}
script: |
# pull image
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$(echo $CONTAINER_IMAGE):$(echo $CONTAINER_TAG)
# stop existing one
docker stop $(echo $CONTAINER_NAME) || echo 'no container to stop'
# Remove old container
docker rm $(echo $CONTAINER_NAME) || echo 'no container to remove'
# Run a new container from a new image
docker run -d --restart always --name $(echo $CONTAINER_NAME) -p $(echo $CONTAINER_PORT):8000 --env-file ~/.env.$(echo $CONTAINER_NAME) ${{ secrets.DOCKERHUB_USERNAME }}/$(echo $CONTAINER_IMAGE):$(echo $CONTAINER_TAG)