-
Notifications
You must be signed in to change notification settings - Fork 1
70 lines (61 loc) · 2.57 KB
/
build-and-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
name: CI
# 1
# Controls when the workflow will run
on:
# Triggers the workflow on push events but only for the main branch
push:
branches: [main]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
version:
description: 'Image version'
required: true
#2
env:
REGISTRY: 'registry.digitalocean.com/dovu'
IMAGE_NAME: 'guardian-middleware-api'
#3
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v2
- name: Build container image
run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) .
- name: Install doctl
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: Push image to DigitalOcean Container Registry
run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
deploy:
runs-on: ubuntu-latest
needs: build_and_push
steps:
- name: Deploy to Digital Ocean droplet via SSH action
uses: appleboy/[email protected]
with:
host: ${{ secrets.DROPLET_HOST }}
username: ${{ secrets.DROPLET_USERNAME }}
key: ${{ secrets.DROPLET_SSH_KEY }}
envs: IMAGE_NAME,REGISTRY,{{ secrets.DIGITALOCEAN_ACCESS_TOKEN }},GITHUB_SHA
script: |
# Login to registry
docker login -u ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} -p ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} registry.digitalocean.com
# Stop running containers
docker compose down
# Set environment vars
DOCKER_CONTAINER_REGISTRY=$(echo $REGISTRY)
DOCKER_IMAGE_NAME=$(echo $IMAGE_NAME)
DOCKER_IMAGE_TAG=$(echo $GITHUB_SHA | head -c7)
export DOCKER_CONTAINER_REGISTRY
export DOCKER_IMAGE_NAME
export DOCKER_IMAGE_TAG
# Run a new container from a new image
docker compose up -d
# Clean out old images
docker image prune -af