CD(beta) - pull_request #2
Workflow file for this run
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(beta) | |
run-name: CD(beta) - ${{ 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: [self-hosted, portainer] | |
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: beta | |
- 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') }} | |
- 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, portainer] | |
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: 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.PORTAINER_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 = ["***"]') | |
echo "$result" | |
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 = ["***"]') | |
echo "$result" | |
fi | |
env: | |
STACK_NAME: "sos24-server-beta" | |
STACK_TYPE: 2 | |
ENDPOINT_ID: 2 | |
COMPOSE_FILE: "docker-compose-beta.yml" | |
PRUNE: "true" | |
PULL_IMAGE: "true" | |
VERIFY_SSL: "true" | |
notify: | |
runs-on: [self-hosted, portainer] | |
needs: [push-image, deploy] | |
if: always() && !failure() && !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.run-name }}" | |
description: "Build and deploy status of sos24-server-beta." | |
color: 0x0000ff | |
url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
username: GitHub Actions | |
avatar_url: https://r2.sohosai.com/logo.png |