This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
forked from bistaastha/badging-bot
-
Notifications
You must be signed in to change notification settings - Fork 10
82 lines (77 loc) · 2.75 KB
/
deploy.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
name: CI/CD
on:
push:
branches:
- "*"
pull_request:
env:
REGISTRY: "registry.digitalocean.com/event-badging-bot"
IMAGE_NAME: "badging"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm install
- name: Lint code
run: npm run format
docker-build:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v3
- name: Login to DigitalOcean registry
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Log in to DigitalOcean Container Registry with short-lived credentials
run: doctl registry login --expiry-seconds 600
- name: Delete existing images with similar name
run: |
doctl registry repository list-tags $REGISTRY/$IMAGE_NAME --format Name --no-header |
while read -r image_tag; do
doctl registry image delete $REGISTRY/$IMAGE_NAME:$image_tag;
done
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
deploy:
needs: docker-build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Login to DigitalOcean registry
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Stop and remove existing images with the same name on the droplet
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then
docker stop ${{ env.IMAGE_NAME }}
docker rm ${{ env.IMAGE_NAME }}
fi
- name: Deploy Docker image to DigitalOcean Droplet
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
echo ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
docker run -d \
-p ${{ secrets.PORT }}:${{ secrets.PORT }} \
--env-file /home/${{ secrets.USERNAME }}/.env \
--restart=always \
--name ${{ env.IMAGE_NAME }} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}