Skip to content

CD(staging) - pull_request #234

CD(staging) - pull_request

CD(staging) - pull_request #234

Workflow file for this run

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