-
Notifications
You must be signed in to change notification settings - Fork 0
122 lines (106 loc) · 3.45 KB
/
cicd.yml
File metadata and controls
122 lines (106 loc) · 3.45 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
112
113
114
115
116
117
118
119
120
121
122
name: backend-ci-cd
on:
push:
branches: [ "main" ]
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "21"
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Ensure Gradle wrapper executable
run: |
sed -i 's/\r$//' gradlew
chmod +x gradlew
- name: Run tests
run: ./gradlew test
build-push:
runs-on: ubuntu-latest
needs: test
if: ${{ github.event_name != 'pull_request' }}
env:
REGISTRY_IMAGE: ${{ secrets.REGISTRY_IMAGE }}
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Resolve registry host
run: |
if [ -z "${REGISTRY_IMAGE}" ]; then
echo "REGISTRY_IMAGE is required"
exit 1
fi
echo "REGISTRY_HOST=${REGISTRY_IMAGE%%/*}" >> $GITHUB_ENV
- name: Allow insecure registry
run: |
echo "{\"insecure-registries\":[\"${REGISTRY_HOST}\"]}" | sudo tee /etc/docker/daemon.json
sudo systemctl restart docker
- name: Write Buildx config
run: |
cat > buildkitd.toml <<EOF
[registry."${REGISTRY_HOST}"]
http = true
insecure = true
EOF
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-config: buildkitd.toml
- name: Login to registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY_HOST }}
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ env.REGISTRY_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
platforms: linux/arm64
tags: |
${{ env.REGISTRY_IMAGE }}:latest
${{ env.REGISTRY_IMAGE }}:${{ github.sha }}
deploy:
runs-on: ubuntu-latest
needs: build-push
if: ${{ github.event_name != 'pull_request' }}
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
REGISTRY_IMAGE: ${{ secrets.REGISTRY_IMAGE }}
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
steps:
- name: Deploy via SSH
if: ${{ env.DEPLOY_HOST != '' }}
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ env.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_KEY }}
envs: REGISTRY_IMAGE,REGISTRY_USERNAME,REGISTRY_PASSWORD
script: |
set -e
REGISTRY_HOST="${REGISTRY_IMAGE%%/*}"
if [ -n "${REGISTRY_USERNAME}" ] && [ -n "${REGISTRY_PASSWORD}" ]; then
echo "${REGISTRY_PASSWORD}" | docker login "${REGISTRY_HOST}" -u "${REGISTRY_USERNAME}" --password-stdin
fi
cd /apps/algorithm-blog
docker compose --env-file .env pull algorithm-blog
docker compose --env-file .env up -d --no-deps algorithm-blog
docker image prune -f