-
Notifications
You must be signed in to change notification settings - Fork 3
98 lines (86 loc) · 3.02 KB
/
Copy pathcd.yml
File metadata and controls
98 lines (86 loc) · 3.02 KB
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
name: Build and Deploy
on:
workflow_dispatch:
push:
branches:
- 'main'
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./docker/Dockerfile
target: 'prod'
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
needs: build
strategy:
matrix:
include:
# - environment: staging
# branch: staging
# target_path: "~/staging.mapdb.cncnet.org"
# compose_file: "docker-compose.prod.yml"
# nginx_conf: "docker/nginx.prod.conf"
# host-s-name: "STAGING_SSH_HOST"
- environment: production
branch: main
target_path: "~/mapdb.cncnet.org"
compose_file: "docker-compose.prod.yml"
nginx_conf: "docker/nginx.prod.conf"
host-s-name: "PROD_SSH_HOST"
steps:
- name: "Exit if not matching branch"
if: github.ref != format('refs/heads/{0}', matrix.branch)
run: echo "Not target branch for this deployment. Skipping..." && exit 0
- name: Checkout repository
uses: actions/checkout@v4
- name: Copy docker-compose and nginx config over ssh
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets[matrix.host-s-name] }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "${{ matrix.compose_file }},${{ matrix.nginx_conf }}"
target: "${{ matrix.target_path }}"
- name: SSH into server and deploy
uses: appleboy/ssh-action@v1.2.1
with:
host: ${{ secrets[matrix.host-s-name] }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd ${{ matrix.target_path }}
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
docker compose -f ${{ matrix.compose_file }} pull
docker compose -f ${{ matrix.compose_file }} down
docker compose -f ${{ matrix.compose_file }} up -d