From a49e2120cd5b4d99e4a6cc83ecabdd65bb27d1b5 Mon Sep 17 00:00:00 2001 From: Anjar Fiandrianto Date: Tue, 16 Jul 2024 11:19:26 +0700 Subject: [PATCH] [#1533] Add production composite actions and and simplify frontend build prod --- .../ssh-docker-compose/action.yml | 47 +++++++++++++++++++ .../ssh-docker-compose/build.sh | 19 ++++++++ .../ssh-docker-compose/frontend-build.sh | 19 ++++++++ .../ssh-docker-compose/git-pull.sh | 17 +++++++ .../ssh-docker-compose/up.sh | 18 +++++++ deploy/docker-compose.frontend-build.yml | 2 +- frontend/release.prod.sh | 9 ++++ 7 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 .github/composite-actions/ssh-docker-compose/action.yml create mode 100755 .github/composite-actions/ssh-docker-compose/build.sh create mode 100755 .github/composite-actions/ssh-docker-compose/frontend-build.sh create mode 100755 .github/composite-actions/ssh-docker-compose/git-pull.sh create mode 100755 .github/composite-actions/ssh-docker-compose/up.sh create mode 100755 frontend/release.prod.sh diff --git a/.github/composite-actions/ssh-docker-compose/action.yml b/.github/composite-actions/ssh-docker-compose/action.yml new file mode 100644 index 000000000..f50575521 --- /dev/null +++ b/.github/composite-actions/ssh-docker-compose/action.yml @@ -0,0 +1,47 @@ +name: Docker Compose Build and Restart + +inputs: + server-ip: + description: 'Server IP address' + required: true + server-ssh-port: + description: 'Server SSH port' + required: true + server-ssh-secret-key: + description: 'The SSH secret key from server' + required: true + server-ssh-user: + description: 'SSH User' + required: true + docker-compose-file: + description: 'Docker compose file location' + required: true + docker-compose-file-frontend-build: + description: 'Docker compose for frontend build file location' + required: true + ci_commit: + description: 'Commit ID' + required: true + +runs: + using: "composite" + steps: + - name: Write secret to file + run: echo "${{ inputs.server-ssh-secret-key }}" > priv.key && chmod 600 priv.key + shell: bash + + - name: Git Pull + run: .github/composite-actions/ssh-docker-compose/git-pull.sh ${{ inputs.server-ip }} ${{ inputs.server-ssh-port }} ${{ inputs.server-ssh-user }} + shell: bash + + - name: Rebuild Frontend + run: .github/composite-actions/ssh-docker-compose/frontend-build.sh ${{ inputs.server-ip }} ${{ inputs.server-ssh-port }} ${{ inputs.server-ssh-user }} ${{ inputs.docker-compose-file-frontend-build }} ${{ inputs.ci_commit }} + shell: bash + + - name: Rebuild + run: .github/composite-actions/ssh-docker-compose/build.sh ${{ inputs.server-ip }} ${{ inputs.server-ssh-port }} ${{ inputs.server-ssh-user }} ${{ inputs.docker-compose-file }} ${{ inputs.ci_commit }} + shell: bash + + - name: Up + run: .github/composite-actions/ssh-docker-compose/up.sh ${{ inputs.server-ip }} ${{ inputs.server-ssh-port }} ${{ inputs.server-ssh-user }} ${{ inputs.docker-compose-file }} + shell: bash \ No newline at end of file diff --git a/.github/composite-actions/ssh-docker-compose/build.sh b/.github/composite-actions/ssh-docker-compose/build.sh new file mode 100755 index 000000000..0402c25be --- /dev/null +++ b/.github/composite-actions/ssh-docker-compose/build.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -eu + +# ${1} for Server IP +# ${2} for Server Port +# ${3} for Server User +# ${4} for Dockerfile location + +server_ip="${1}" +server_port="${2}" +server_user="${3}" +docker_compose_file="${4}" +ci_commit="${5}" + +ssh -i priv.key -o BatchMode=yes \ + -p "${server_port}" \ + -o UserKnownHostsFile=/dev/null \ + -o StrictHostKeyChecking=no \ + "${server_user}"@"${server_ip}" "cd src/deploy && CI_COMMIT=${ci_commit} docker compose -f ${docker_compose_file} build --no-cache" \ No newline at end of file diff --git a/.github/composite-actions/ssh-docker-compose/frontend-build.sh b/.github/composite-actions/ssh-docker-compose/frontend-build.sh new file mode 100755 index 000000000..2755da0c7 --- /dev/null +++ b/.github/composite-actions/ssh-docker-compose/frontend-build.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -eu + +# ${1} for Server IP +# ${2} for Server Port +# ${3} for Server User +# ${4} for Dockerfile location + +server_ip="${1}" +server_port="${2}" +server_user="${3}" +docker_compose_file="${4}" +ci_commit="${5}" + +ssh -i priv.key -o BatchMode=yes \ + -p "${server_port}" \ + -o UserKnownHostsFile=/dev/null \ + -o StrictHostKeyChecking=no \ + "${server_user}"@"${server_ip}" "cd src/deploy && CI_COMMIT=${ci_commit} docker compose -f ${docker_compose_file} up --build" \ No newline at end of file diff --git a/.github/composite-actions/ssh-docker-compose/git-pull.sh b/.github/composite-actions/ssh-docker-compose/git-pull.sh new file mode 100755 index 000000000..c66b51f2b --- /dev/null +++ b/.github/composite-actions/ssh-docker-compose/git-pull.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -eu + +# ${1} for Server IP +# ${2} for Server Port +# ${3} for Server User + +server_ip="${1}" +server_port="${2}" +server_user="${3}" + + +ssh -i priv.key -o BatchMode=yes \ + -p "${server_port}" \ + -o UserKnownHostsFile=/dev/null \ + -o StrictHostKeyChecking=no \ + "${server_user}"@"${server_ip}" "cd src/ && git pull" \ No newline at end of file diff --git a/.github/composite-actions/ssh-docker-compose/up.sh b/.github/composite-actions/ssh-docker-compose/up.sh new file mode 100755 index 000000000..fb42a2125 --- /dev/null +++ b/.github/composite-actions/ssh-docker-compose/up.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -eu + +# ${1} for Server IP +# ${2} for Server Port +# ${3} for Server User +# ${4} for Dockerfile location + +server_ip="${1}" +server_port="${2}" +server_user="${3}" +docker_compose_file="${4}" + +ssh -i priv.key -o BatchMode=yes \ + -p "${server_port}" \ + -o UserKnownHostsFile=/dev/null \ + -o StrictHostKeyChecking=no \ + "${server_user}"@"${server_ip}" "cd src/deploy && docker compose -f ${docker_compose_file} stop && docker compose -f ${docker_compose_file} up -d" \ No newline at end of file diff --git a/deploy/docker-compose.frontend-build.yml b/deploy/docker-compose.frontend-build.yml index b24b46da5..d730a8a4b 100644 --- a/deploy/docker-compose.frontend-build.yml +++ b/deploy/docker-compose.frontend-build.yml @@ -13,6 +13,6 @@ services: echo "PUBLIC_URL=/" > .env sed 's/"warn"/"error"/g' < .eslintrc.json > .eslintrc.prod.json sed "s/\"##CACHE_VERSION##\"/\"${CI_COMMIT}\"/g" < public/service-worker.template.js > public/service-worker.js - sh release.sh + sh release.prod.sh volumes: - ../frontend:/app:delegated \ No newline at end of file diff --git a/frontend/release.prod.sh b/frontend/release.prod.sh new file mode 100755 index 000000000..c4186942c --- /dev/null +++ b/frontend/release.prod.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +#shellcheck disable=SC2039 + +set -euo pipefail + +yarn install --no-progress --frozen-lock +yarn eslint --config .eslintrc.prod.json src --ext .js,.jsx +yarn prettier --check src/ +yarn build