96 lines
2.2 KiB
YAML
96 lines
2.2 KiB
YAML
|
name: Build, Test & Publish
|
||
|
|
||
|
on:
|
||
|
push:
|
||
|
branches:
|
||
|
- main
|
||
|
pull_request:
|
||
|
branches:
|
||
|
- main
|
||
|
workflow_dispatch:
|
||
|
|
||
|
jobs:
|
||
|
build:
|
||
|
name: Build
|
||
|
runs-on: ubuntu-latest
|
||
|
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- name: Enable corepack
|
||
|
run: corepack enable
|
||
|
|
||
|
- name: Setup Node.js
|
||
|
uses: actions/setup-node@v4
|
||
|
with:
|
||
|
node-version: '20'
|
||
|
|
||
|
- name: Install dependencies
|
||
|
run: yarn install --frozen-lockfile
|
||
|
|
||
|
- name: Build
|
||
|
run: yarn build
|
||
|
|
||
|
publish:
|
||
|
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||
|
name: Build and Publish Container Image
|
||
|
runs-on: ubuntu-latest
|
||
|
|
||
|
needs:
|
||
|
- build
|
||
|
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- name: Setup Docker Metadata
|
||
|
id: meta
|
||
|
uses: docker/metadata-action@v4
|
||
|
with:
|
||
|
images: liamp1/code
|
||
|
tags: |
|
||
|
type=raw,value=latest
|
||
|
|
||
|
- name: Login to DockerHub
|
||
|
uses: docker/login-action@v3
|
||
|
with:
|
||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||
|
password: ${{ secrets.DOCKER_TOKEN }}
|
||
|
|
||
|
- name: Build and Push Docker Image to DockerHub
|
||
|
uses: docker/build-push-action@v4
|
||
|
with:
|
||
|
push: true
|
||
|
tags: ${{ steps.meta.outputs.tags }}
|
||
|
labels: ${{ steps.meta.outputs.labels }}
|
||
|
|
||
|
deploy:
|
||
|
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||
|
name: Deploy to Infrastructure
|
||
|
runs-on: ubuntu-latest
|
||
|
|
||
|
needs:
|
||
|
- publish
|
||
|
|
||
|
steps:
|
||
|
- uses: actions/checkout@v3
|
||
|
|
||
|
- name: Write GitHub SSH Key to File
|
||
|
env:
|
||
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
||
|
run: |
|
||
|
echo "$SSH_PRIVATE_KEY" > private.key
|
||
|
sudo chmod 400 private.key
|
||
|
|
||
|
- name: Write Ansible Inventory To File
|
||
|
env:
|
||
|
APP_HOST: ${{ secrets.APP_HOST }}
|
||
|
run: |
|
||
|
echo "[app]" > hosts.ini
|
||
|
echo "$APP_HOST" >> hosts.ini
|
||
|
|
||
|
- name: Run Ansible Playbook to Configure Servers
|
||
|
run: |
|
||
|
export ANSIBLE_HOST_KEY_CHECKING=False # Disable host key checking
|
||
|
ansible-playbook infra/ansible/deploy-playbook.yml --private-key private.key --inventory hosts.ini
|
||
|
env:
|
||
|
APP_HOST: ${{ secrets.APP_HOST }}
|