This repository was archived by the owner on Nov 25, 2024. It is now read-only.
Build, push and deploy service #168
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: Build, push and deploy service | |
on: | |
workflow_dispatch: | |
inputs: | |
client: | |
description: client | |
type: boolean | |
server: | |
description: server | |
type: boolean | |
load-balancer: | |
description: load-balancer | |
type: boolean | |
deploy-env: | |
description: Environment | |
type: choice | |
options: | |
- stage | |
- prod | |
build: | |
description: Build services | |
type: boolean | |
default: true | |
deploy: | |
description: Deploy services | |
type: boolean | |
default: true | |
jobs: | |
generate-matrix: | |
runs-on: ubuntu-latest | |
name: Generate service matrix | |
outputs: | |
matrix: ${{ steps.generate-service-matrix.outputs.matrix }} | |
steps: | |
- name: Setup node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 18.16.0 | |
- name: Generate service matrix | |
id: generate-service-matrix | |
run: | | |
JSON='${{ toJSON(inputs) }}' | |
SERVICES=client,server,load-balancer | |
MATRIX_ARRAY=$(node -e "const services = '$SERVICES'.split(',');console.log(Object.entries($JSON).filter((e) => services.includes(e[0]) && Boolean(e[1])).map((e) => e[0]))") | |
echo ::set-output name=matrix::${MATRIX_ARRAY} | |
build-and-push: | |
runs-on: ubuntu-latest | |
name: Build and push ${{ matrix.service }} (${{ inputs.deploy-env }}) | |
needs: | |
- generate-matrix | |
if: inputs.build | |
strategy: | |
matrix: | |
service: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
- name: Log in to the Container registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ secrets.PACKAGES_USERNAME }} | |
password: ${{ secrets.PACKAGES_TOKEN }} | |
- name: Get package.json version | |
id: pkgversion | |
run: | | |
PKG_VERSION=$(jq -r ".version" "apps/${{ matrix.service }}/package.json") | |
echo "$PKG_VERSION" | |
echo "::set-output name=PKG_VERSION::$PKG_VERSION" | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ghcr.io/${{ github.repository }}/${{ matrix.service }}-${{ inputs.deploy-env }} | |
tags: | | |
type=semver,pattern={{version}},value=${{ steps.pkgversion.outputs.PKG_VERSION }} | |
- name: Build and push ${{ matrix.service }} | |
uses: docker/build-push-action@v3 | |
with: | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
file: deploy/dockerfiles/Dockerfile.${{ matrix.service }} | |
platforms: linux/amd64 | |
target: runner-prod | |
build-args: | | |
ENV=${{ inputs.deploy-env }} | |
deploy: | |
runs-on: ubuntu-latest | |
name: Deploy ${{ matrix.service }} (${{ inputs.deploy-env }}) | |
needs: | |
- generate-matrix | |
- build-and-push | |
if: | | |
always() && | |
inputs.deploy && | |
(needs.build-and-push.result == 'success' || needs.build-and-push.result == 'skipped') | |
strategy: | |
matrix: | |
service: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
- name: Get package.json version | |
id: pkgversion | |
run: | | |
PKG_VERSION=$(jq -r ".version" "apps/${{ matrix.service }}/package.json") | |
echo "$PKG_VERSION" | |
echo "::set-output name=PKG_VERSION::$PKG_VERSION" | |
- name: Deploy ${{ matrix.service }} | |
uses: appleboy/[email protected] | |
env: | |
REPOSITORY: ${{ github.repository }} | |
APP_PATH: /app | |
SOURCE_DIR: source-${{ matrix.service }}-${{ inputs.deploy-env }} | |
SERVERS_TO_START: 2 | |
DEPLOY_ENV: ${{ inputs.deploy-env }} | |
VERSION: ${{ steps.pkgversion.outputs.PKG_VERSION }} | |
UPDATE_TOKEN: ${{ secrets.UPDATE_TOKEN }} | |
with: | |
host: ${{ secrets.PROD_HOST }} | |
username: ${{ secrets.PROD_USERNAME }} | |
password: ${{ secrets.PROD_PASSWORD }} | |
envs: REPOSITORY,APP_PATH,SOURCE_DIR,SERVERS_TO_START,DEPLOY_ENV,VERSION,UPDATE_TOKEN | |
script: | | |
git clone --single-branch --branch ${{ github.ref_name }} https://github.com/$REPOSITORY.git $APP_PATH/$SOURCE_DIR | |
$APP_PATH/$SOURCE_DIR/deploy/scripts/update-${{ matrix.service }}.sh | |
purge-cache: | |
needs: | |
- deploy | |
if: (inputs.client && inputs.deploy) | |
name: Purge cloudflare cache | |
runs-on: ubuntu-latest | |
steps: | |
- name: Purge cloudflare cache | |
uses: nathanvaughn/[email protected] | |
with: | |
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }} | |
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }} |