-
Notifications
You must be signed in to change notification settings - Fork 3
111 lines (90 loc) · 3.09 KB
/
deploy.yml
File metadata and controls
111 lines (90 loc) · 3.09 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
99
100
101
102
103
104
105
106
107
108
109
110
111
name: Deploy
on:
release:
types: [published]
concurrency:
group: deploy-${{ github.event.release.tag_name }}
cancel-in-progress: false
jobs:
build-and-push:
name: Build and Push Image
runs-on: ubuntu-24.04
outputs:
image: ${{ steps.out.outputs.image }}
digest: ${{ steps.push.outputs.digest }}
permissions:
contents: read
packages: write
id-token: write
attestations: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
steps:
- uses: actions/checkout@v6
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: type=ref,event=tag
- uses: docker/build-push-action@v6
id: push
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Output image ref
id: out
run: |
echo "image=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> "$GITHUB_OUTPUT"
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v3
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
deploy:
name: Deploy to Server
runs-on: ubuntu-24.04
needs: build-and-push
permissions:
contents: read
packages: read
env:
IMAGE: ${{ needs.build-and-push.outputs.image }}
DIGEST: ${{ needs.build-and-push.outputs.digest }}
CONTAINER_NAME: ${{ vars.DOCKER_CONTAINER_NAME }}
GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }}
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
steps:
- name: Deploy over SSH
uses: appleboy/ssh-action@v1.2.4
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
envs: IMAGE,DIGEST,CONTAINER_NAME,GHCR_USERNAME,GHCR_TOKEN
script: |
set -euo pipefail
cd github-bot || exit 1
printf '%s' "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USERNAME" --password-stdin
IMAGE_REF="$IMAGE@$DIGEST"
echo "Deploying $IMAGE_REF"
docker pull "$IMAGE_REF"
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
API_PORT=$(grep ^API_PORT= .env | cut -d '=' -f2)
docker run -d -p ${API_PORT}:${API_PORT} --env-file .env -v ./data:/app/data --restart on-failure:16 --name "$CONTAINER_NAME" "$IMAGE_REF"
docker logout ghcr.io