Skip to content

feat: add ci

feat: add ci #1

Workflow file for this run

name: Build
env:
APP_NAME: CMS-BACKEND-API
PROJECT_NAME: CMS-BACKEND-API
DOCKER_COMPOSE_PATH: /root/app/docker-compose.yml
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
DOCKER_REGISTRY: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

Check failure on line 9 in .github/workflows/ci.yml

View workflow run for this annotation

GitHub Actions / Build

Invalid workflow file

The workflow is not valid. .github/workflows/ci.yml (Line: 9, Col: 20): Unrecognized named-value: 'env'. Located at position 1 within expression: env.REGISTRY
DOT_ENV_FILE_NAME: env.data
on:
workflow_dispatch:
push:
branches:
- devops
- dev
- main
permissions:
contents: write
jobs:
set_vars:
name: Set Environment Variables
runs-on: ubuntu-latest
outputs:
TAG_LATEST: ${{ steps.tag_values.outputs.TAG_LATEST }}
TAG_ENV_COMMIT: ${{ steps.tag_values.outputs.TAG_ENV_COMMIT }}
APP_ENV: ${{ steps.tag_values.outputs.APP_ENV }}
steps:
- name: Set Docker Image Tags
id: tag_values
run: |
case "${{ github.ref }}" in
'refs/heads/main')
echo "TAG_LATEST=prod-latest" >> $GITHUB_OUTPUT
echo "TAG_ENV_COMMIT=prod-${GITHUB_SHA:0:5}" >> $GITHUB_OUTPUT
echo "APP_ENV=PROD" >> $GITHUB_OUTPUT
;;
'refs/heads/devops')
echo "TAG_LATEST=dev-latest" >> $GITHUB_OUTPUT
echo "TAG_ENV_COMMIT=dev-${GITHUB_SHA:0:5}" >> $GITHUB_OUTPUT
echo "APP_ENV=DEV" >> $GITHUB_OUTPUT
;;
'refs/heads/dev')
echo "TAG_LATEST=dev-latest" >> $GITHUB_OUTPUT
echo "TAG_ENV_COMMIT=dev-${GITHUB_SHA:0:5}" >> $GITHUB_OUTPUT
echo "APP_ENV=DEV" >> $GITHUB_OUTPUT
;;
esac
build:
name: Build
runs-on: ubuntu-latest
needs: [set_vars]
permissions:
contents: read
packages: write
env:
TAG_LATEST: ${{ needs.set_vars.outputs.TAG_LATEST }}
TAG_ENV_COMMIT: ${{ needs.set_vars.outputs.TAG_ENV_COMMIT }}
SUPABASE_URL: ${{ vars[format('APP_{0}_SUPABASE_URL', needs.set_vars.outputs.APP_ENV)] }}
SUPABASE_KEY: ${{ secrets[format('APP_{0}_SUPABASE_KEY', needs.set_vars.outputs.APP_ENV)] }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Login to GitHub Packages
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Set Docker Tags
uses: actions/setup-node@v2
- name: Read Secrets
run: |
echo "SUPABASE_URL=${SUPABASE_URL}" >> .env
echo "SUPABASE_KEY=${SUPABASE_KEY}" >> .env
mv .env ${{ env.DOT_ENV_FILE_NAME }}
- name: Copy env file to DEV Server
uses: appleboy/[email protected]
if: needs.set_vars.outputs.APP_ENV == 'DEV'
with:
host: ${{ vars.DEV_SERVER_HOST }}
username: ${{ vars.DEV_SERVER_USERNAME }}
key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
port: ${{ vars.DEV_SERVER_PORT }}
source: "${{ env.DOT_ENV_FILE_NAME }}"
target: /root/app/
- name: Build ${{ env.APP_NAME }} Docker image
run: |
docker build -t ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_LATEST }} .
- name: Add tag to Docker image
run: |
echo ${{ github.sha }}
docker tag ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_LATEST }} ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_ENV_COMMIT }}
- name: Push Docker image to GitHub Packages
run: |
docker push ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_LATEST }}
docker push ${{ env.DOCKER_REGISTRY }}:${{ env.TAG_ENV_COMMIT }}
deploy:
name: Deployment
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'push' && github.ref_type == 'branch'
steps:
- name: Deploy to DevOps/Dev Environment
if: github.ref == 'refs/heads/devops' || github.ref == 'refs/heads/dev'
uses: appleboy/[email protected]
env:
DOCKER_COMPOSE_PATH: ${{ env.DOCKER_COMPOSE_PATH }}
APP_NAME: ${{ env.APP_NAME }}
DOCKER_REGISTRY: ${{ env.DOCKER_REGISTRY }}
with:
host: ${{ vars.DEV_SERVER_HOST }}
username: ${{ vars.DEV_SERVER_USERNAME }}
key: ${{ secrets.DEV_SSH_PRIVATE_KEY }}
port: ${{ vars.DEV_SERVER_PORT }}
allenvs: true
script_stop: true
envs: DOCKER_COMPOSE_PATH,APP_NAME,DOCKER_REGISTRY
script: |
echo "==============================="
echo "Deploying to Dev environment running on Docker Compose"
echo "==============================="
echo "Docker Compose Path $DOCKER_COMPOSE_PATH"
echo "App Name $APP_NAME"
echo "Docker Registry $DOCKER_REGISTRY"
COMMIT=$(echo $GITHUB_SHA | cut -c1-5)
TAG_ENV_COMMIT="dev-${COMMIT}"
CURRENT_TAG=$(grep "${DOCKER_REGISTRY}:" $DOCKER_COMPOSE_PATH | sed -n 's/.*'"${APP_NAME}"':\([^"]*\).*/\1/p')
echo "Current Tag: $CURRENT_TAG"
echo "Latest Tag: $TAG_ENV_COMMIT"
sudo sed -i 's|'"${DOCKER_REGISTRY}:${CURRENT_TAG}"'|'"${DOCKER_REGISTRY}:${TAG_ENV_COMMIT}"'|g' $DOCKER_COMPOSE_PATH
docker pull $DOCKER_REGISTRY:$TAG_ENV_COMMIT
docker compose -f $DOCKER_COMPOSE_PATH up -d
- name: Deploy to Prod environment
if: github.ref == 'refs/heads/main'
run: echo "Deploying to Kubernetes"