-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (139 loc) · 5.71 KB
/
cd-staging.yaml
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: CD(staging)
run-name: CD(staging) - ${{ github.event_name }}
on:
pull_request:
branches:
- develop
types:
- closed
workflow_dispatch:
inputs:
push-image-only:
type: boolean
default: false
required: false
description: "push image only(not deploy)"
deploy-only:
type: boolean
default: false
required: false
description: "deploy only(not push image)"
jobs:
push-image:
if: github.event.pull_request.merged == true || github.event.inputs.deploy-only == 'false'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: develop
- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/sohosai/sos24-server
tags: stg
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Cache
uses: actions/cache@v4
id: cache
with:
path: |
usr-local-cargo-registry
app-target
key: cache-${{ hashFiles('Dockerfile') }}-${{ hashFiles('Cargo.*') }}
- name: inject cache into docker
# v3.0.0のcommitを指定
uses: reproducible-containers/buildkit-cache-dance@0fc239dcc207d7ce9fd659f4f92fefb84549c182
with:
cache-map: |
{
"usr-local-cargo-registry": "/usr/local/cargo/registry",
"app-target": "/app/target"
}
skip-extraction: ${{ steps.cache.outputs.cache-hit }}
- name: create demo.sql
run: |
echo "${{ secrets.DEMO_SQL }}" > migrations/99999999999999_demo.sql
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
runs-on: ubuntu-latest
needs: push-image
if: |
(github.event.pull_request.merged == true && always() && !failure() && !cancelled()) ||
(github.event.inputs.push-image-only == 'false' && always() && !failure() && !cancelled())
steps:
- uses: actions/checkout@v4
with:
ref: develop
- name: ubuntu mirror
run: sudo sed -i.bak -r 's@http://(jp\.)?archive\.ubuntu\.com/ubuntu/?@https://ftp.udx.icscoe.jp/Linux/ubuntu/@g' /etc/apt/sources.list
- name: Install dependencies
env:
DEBIAN_FRONTEND: noninteractive
run: sudo apt-get update && sudo apt-get install -y jq
- name: Deploy to Portainer
run: |
# List stacks
STACKS=$(curl -s -X GET "${{ secrets.PORTAINER_URL }}/api/stacks" -H "Content-Type: application/json" -gH "x-api-key:${{ secrets.PORTAINER_APIKEY }}")
# Get stack ID by name
STACK_ID=$(echo "${STACKS}" | jq -r --arg STACK_NAME "${STACK_NAME}" '.[] | select(.Name == $STACK_NAME) | .Id')
# PORTAINER_VARIABLE to json
mapfile -t env_array <<< "${{ secrets.STG_VARIABLE }}"
env_data=$(for item in "${env_array[@]}"; do if [ -n "$item" ]; then key=${item%%=*}; value=${item#*=}; echo "{\"name\": \"$key\", \"value\": \"$value\"},"; fi; done)
env_data="${env_data%,}"
# Create or update stack
if [ -n "${STACK_ID}" ]; then
UPDATE_DATA="{\"Prune\": ${PRUNE}, \"Env\": [$env_data], \"pullImage\": ${PULL_IMAGE}, \"stackFileContent\": \"$(cat ${COMPOSE_FILE} | tr -d '\r' | sed 's/"/\\"/g' | sed 's/$/\\n/' | tr -d '\n')\"}"
result=$(curl -sS -X PUT "${{ secrets.PORTAINER_URL }}/api/stacks/${STACK_ID}?endpointId=${ENDPOINT_ID}" -H "Content-Type: application/json" -gH "x-api-key:${{ secrets.PORTAINER_APIKEY }}" --data-binary "${UPDATE_DATA}")
result=$(echo "$result" | jq '.Env = ["***"]')
if [[ $result == *"failed to deploy a stack:"* ]]; then
exit 1
fi
else
CREATE_DATA="{\"name\": \"${STACK_NAME}\", \"Env\": [$env_data], \"stackFileContent\": \"$(cat ${COMPOSE_FILE} | tr -d '\r' | sed 's/"/\\"/g' | sed 's/$/\\n/' | tr -d '\n')\"}"
result=$(curl -sS -X POST "${{ secrets.PORTAINER_URL }}/api/stacks?type=${STACK_TYPE}&method=string&endpointId=${ENDPOINT_ID}" -H "Content-Type: application/json" -gH "x-api-key:${{ secrets.PORTAINER_APIKEY }}" --data-binary "${CREATE_DATA}")
result=$(echo "$result" | jq '.Env = ["***"]')
if [[ $result == *"failed to deploy a stack:"* ]]; then
exit 1
fi
fi
env:
STACK_NAME: "sos24-server-stg"
STACK_TYPE: 2
ENDPOINT_ID: 2
COMPOSE_FILE: "docker-compose-stg.yml"
PRUNE: "true"
PULL_IMAGE: "true"
VERIFY_SSL: "true"
notify:
runs-on: ubuntu-latest
needs: [push-image, deploy]
if: always() && !cancelled()
steps:
- name: Notify
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
content: "push-image: ${{ needs.push-image.result }}\ndeploy: ${{ needs.deploy.result }}"
title: "${{ github.workflow }}"
description: "Build and deploy status of sos24-server-staging."
color: 0x0000ff
url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
username: GitHub Actions
avatar_url: https://r2.sohosai.com/logo.png