CD(prodution) - pull_request #24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CD(prodution) | |
run-name: CD(prodution) - ${{ github.event_name }} | |
on: | |
pull_request: | |
branches: | |
- main | |
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: [self-hosted, garm] | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
ref: main | |
- name: Docker meta | |
id: docker_meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ghcr.io/sohosai/sos24-server | |
tags: prd | |
- 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: 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: [self-hosted, garm] | |
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: main | |
- name: install dependencies | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y rsync | |
env: | |
DEBIAN_FRONTEND: noninteractive | |
- name: Setup SSH | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "${{ secrets.SSH_PRIV_KEY }}" > ~/.ssh/id_ed25519 | |
echo "${{ secrets.SSH_PUB_KEY }}" > ~/.ssh/id_ed25519.pub | |
chmod 600 ~/.ssh/id_ed25519 | |
chmod 600 ~/.ssh/id_ed25519.pub | |
ssh-keyscan -H ${{ secrets.DEPLOYSERVER_IP }} >> ~/.ssh/known_hosts | |
- name: old container dwon | |
run: | | |
# down database container | |
ssh ubuntu@${{ secrets.DEPLOYSERVER_IP }} "if [ -f /home/ubuntu/docker-compose-db.yml ]; then \ | |
if ! docker context ls | grep -q remote; then \ | |
docker context create remote --docker 'host=ssh://[email protected]' && \ | |
ssh-keyscan -H 10.0.0.2 >> ~/.ssh/known_hosts; \ | |
fi; \ | |
docker --context remote compose -f docker-compose-db.yml down; \ | |
fi" | |
# down server container | |
ssh ubuntu@${{ secrets.DEPLOYSERVER_IP }} "if [ -f /home/ubuntu/docker-compose-prd.yml ]; then \ | |
docker compose -f docker-compose-prd.yml down; \ | |
fi" | |
- name: create .env | |
run: | | |
echo "${{ secrets.PRD_VARIABLE }}" > .env | |
- name: copy files | |
run: | | |
rsync -vrm .env docker-compose-db.yml docker-compose-prd.yml ubuntu@${{ secrets.DEPLOYSERVER_IP }}:/home/ubuntu/ | |
- name: Deploy server | |
run: | | |
ssh ubuntu@${{ secrets.DEPLOYSERVER_IP }} "docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} && \ | |
if ! docker context ls | grep -q remote; then \ | |
docker context create remote --docker 'host=ssh://[email protected]' && \ | |
ssh-keyscan -H 10.0.0.2 >> ~/.ssh/known_hosts; \ | |
fi; \ | |
docker --context remote compose --env-file .env -f docker-compose-db.yml pull && \ | |
docker --context remote compose --env-file .env -f docker-compose-db.yml up -d && \ | |
docker compose --env-file .env -f docker-compose-prd.yml pull && \ | |
docker compose --env-file .env -f docker-compose-prd.yml up -d && \ | |
docker logout ghcr.io && \ | |
rm .env" | |
notify: | |
runs-on: [self-hosted, garm] | |
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-prodution." | |
color: 0x0000ff | |
url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
username: GitHub Actions | |
avatar_url: https://r2.sohosai.com/logo.png |