Merge pull request #23 from ngyngcphu/release-please--branches--main-… #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: Release and deploy | |
on: | |
push: | |
branches: | |
- main | |
paths-ignore: | |
- '*.md' | |
permissions: | |
contents: write | |
pull-requests: write | |
packages: write | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY: ghcr.io | |
# Image name MUST have a pattern like <org_name_lowercase>/<repo_name_lowercase> | |
IMAGE_NAME: ngyngcphu/tick3d-be | |
# username login to ghcr MUST be organization or personal name in lower case | |
CD_USERNAME: ngyngcphu | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
outputs: | |
build: ${{ steps.release.outputs.release_created }} | |
tag_name: ${{ steps.release.outputs.tag_name }} | |
steps: | |
- uses: google-github-actions/release-please-action@v3 | |
id: release | |
with: | |
release-type: node | |
pull-request-header: "Bot (:robot:) requested to create a new release on ${{ github.ref_name }}" | |
# Use if need to build package | |
build: | |
name: Build | |
needs: [release] | |
runs-on: ubuntu-latest | |
if: ${{ needs.release.outputs.build == 'true' }} | |
steps: | |
- uses: actions/checkout@v3 | |
name: Checkout repository | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Setup Docker buildx | |
uses: docker/setup-buildx-action@v2 | |
with: | |
version: v0.9.1 | |
- name: Log into registry ${{ env.REGISTRY }} | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ env.CD_USERNAME }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.release.outputs.tag_name }}, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
# Use if need to deploy on a server | |
deploy: | |
name: Deploy | |
runs-on: ubuntu-latest | |
needs: [build] | |
env: | |
POSTGRES_URL: postgresql://${{ secrets.POSTGRES_USER }}:${{ secrets.POSTGRES_PASSWORD }}@${{ secrets.VPS_HOST }}:5433/${{ secrets.POSTGRES_DB }}?schema=public | |
steps: | |
- name: Deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.VPS_HOST }} | |
port: ${{ secrets.VPS_PORT }} | |
username: ${{ secrets.VPS_USERNAME }} | |
password: ${{ secrets.VPS_PASSWORD }} | |
# TODO: Remove docker volumes in Dev stage only | |
script: | | |
export CR_PAT=${{ secrets.GITHUB_TOKEN }} | |
echo $CR_PAT | docker login ${{ env.REGISTRY }} -u ${{ env.CD_USERNAME }} --password-stdin | |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
cd tick3d-be | |
rm -f .env | |
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env | |
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env | |
echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env | |
echo POSTGRES_PORT=${{ secrets.POSTGRES_PORT }} >> .env | |
echo POSTGRES_URL=${{ env.POSTGRES_URL }} >> .env | |
echo COOKIE_SECRET=${{ secrets.COOKIE_SECRET }} >> .env | |
echo JWT_SECRET=${{ secrets.JWT_SECRET }} >> .env | |
echo CORS_WHITE_LIST=${{ vars.CORS_WHITE_LIST }} >> .env | |
echo BACKEND_URL=${{ secrets.BACKEND_URL }} >> .env | |
echo SMTP_SERVER=${{ secrets.SMTP_SERVER }} >> .env | |
echo SMTP_PORT=${{ secrets.SMTP_PORT }} >> .env | |
echo SMTP_USER=${{ secrets.SMTP_USER }} >> .env | |
echo SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }} >> .env | |
curl https://raw.githubusercontent.com/ngyngcphu/tick3d-be/main/docker-compose.yml -o docker-compose.yml | |
docker compose down --volumes --remove-orphans tick3d-be | |
docker compose rm -f tick3d-be | |
docker compose up -d tick3d-be | |
docker logout ${{ env.REGISTRY }} | |