From 7503d0f2fb605f014ba9f86dbae448a5e046b109 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sun, 3 Aug 2025 19:07:13 +0700 Subject: feat: add ci/cd for build, push & deploy --- .github/workflows/build-and-deploy.yml | 60 ++++++++++++++++++++++++++++++++++ deploy/inventory.yml | 15 +++++++++ deploy/playbook.yml | 20 ++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 .github/workflows/build-and-deploy.yml create mode 100644 deploy/inventory.yml create mode 100644 deploy/playbook.yml diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml new file mode 100644 index 0000000..c563ee8 --- /dev/null +++ b/.github/workflows/build-and-deploy.yml @@ -0,0 +1,60 @@ +name: Publish Docker image +on: + push: + tags: + - "*" +jobs: + build-and-push: + if: github.repository == 'SayaAndy/saya-today-web' + runs-on: ubuntu-latest + environment: Production + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Login to GitHub Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GHCR_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + push: true + tags: | + ghcr.io/SayaAndy/saya-today-web:latest + ghcr.io/SayaAndy/saya-today-web:${{ github.ref_name }} + + deploy: + if: github.repository == 'SayaAndy/saya-today-web' + runs-on: ubuntu-latest + environment: Production + needs: [build-and-push] + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up SSH private key + run: | + echo "${{ secrets.SVC_GITHUB_PK }}" > deploy/private_key.pem + chmod 600 deploy/private_key.pem + - name: Install Ansible + shell: bash + run: | + sudo apt update + sudo apt install -y ansible + - name: Run Ansible playbook + env: + ANSIBLE_HOST_KEY_CHECKING: False + working-directory: ./deploy + run: > + ansible-playbook -i inventory.yml -l stage playbook.yml + --private-key private_key.pem + -e saya_today_web.b2.key_id=${{ secrets.B2_KEY_ID }} + -e saya_today_web.b2.application_key=${{ secrets.B2_APPLICATION_KEY }} + -e saya_today_web.tag=${{ github.ref_name }} diff --git a/deploy/inventory.yml b/deploy/inventory.yml new file mode 100644 index 0000000..d3c325f --- /dev/null +++ b/deploy/inventory.yml @@ -0,0 +1,15 @@ +prod: + hosts: + pl.saya.today: + saya_today_web: + name: sayana-web + hostname: sayana-web + ipv4_address: 172.16.0.18 + +stage: + hosts: + pl.saya.today: + saya_today_web: + name: sayana-demo + hostname: sayana-demo + ipv4_address: 172.16.0.17 diff --git a/deploy/playbook.yml b/deploy/playbook.yml new file mode 100644 index 0000000..84432d8 --- /dev/null +++ b/deploy/playbook.yml @@ -0,0 +1,20 @@ +--- +- name: Deploy saya-today-web Docker Container + remote_user: svc_github + + tasks: + - name: Deploy saya-today-web Docker Container + community.docker.docker_container: + name: "{{ saya_today_web.name }}" + hostname: "{{ saya_today_web.hostname }}" + image: "ghcr.io/SayaAndy/saya-today-web:{{ saya_today_web.tag }}" + env: + B2_KEY_ID: "{{ saya_today_web.b2.key_id }}" + B2_APPLICATION_KEY: "{{ saya_today_web.b2.application_key }}" + network_mode: caddy + networks: + - name: caddy + ipv4_address: "{{ saya_today_web.ipv4_address }}" + restart_policy: unless-stopped + no_log: true +... -- cgit v1.3.1+13