From c1fefa3e249e97e51f155cfb7276730c99b1cf44 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sat, 16 Aug 2025 22:56:19 +0700 Subject: feat: split deploy into 2 environments fix: rm server exec in repo --- .github/workflows/build-and-deploy-prod.yml | 78 +++++++++++++++++++++++ .github/workflows/build-and-deploy-stage.yml | 89 +++++++++++++++++++++++++++ .github/workflows/build-and-deploy.yml | 77 ----------------------- Dockerfile | 9 +-- config/config.prod.yaml | 20 ++++++ config/config.stage.yaml | 20 ++++++ config/config.yaml | 20 ------ deploy/playbook.yml | 1 + saya-today-web | Bin 19752119 -> 0 bytes 9 files changed, 213 insertions(+), 101 deletions(-) create mode 100644 .github/workflows/build-and-deploy-prod.yml create mode 100644 .github/workflows/build-and-deploy-stage.yml delete mode 100644 .github/workflows/build-and-deploy.yml create mode 100644 config/config.prod.yaml create mode 100644 config/config.stage.yaml delete mode 100644 config/config.yaml delete mode 100755 saya-today-web diff --git a/.github/workflows/build-and-deploy-prod.yml b/.github/workflows/build-and-deploy-prod.yml new file mode 100644 index 0000000..53e307e --- /dev/null +++ b/.github/workflows/build-and-deploy-prod.yml @@ -0,0 +1,78 @@ +name: Docker image building and publishing onto Production +on: + push: + tags: + - "*" +jobs: + build-and-push: + if: github.repository == 'SayaAndy/saya-today-web' + runs-on: ubuntu-latest + environment: Production + permissions: + packages: write + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Generate output.css with tailwindcss + uses: ZoeyVid/tailwindcss-update@main + with: + input: static/input.css + output: static/output.css + params: "--minify" + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GHCR_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + push: true + tags: | + ghcr.io/sayaandy/saya-today-web:latest + ghcr.io/sayaandy/saya-today-web:stable + 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 connection for pl.saya.today + run: | + mkdir -p ~/.ssh + (cat < ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + sed -i 's/\r$//' ~/.ssh/id_ed25519 + ssh-keyscan -H pl.saya.today >> ~/.ssh/known_hosts + + - name: Test SSH connection + run: | + ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 svc_github@pl.saya.today "echo 'SSH connection successful'" + + - 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 prod playbook.yml --private-key ~/.ssh/id_ed25519 -u svc_github -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_environment=prod -e saya_today_web_tag=${{ github.ref_name }} diff --git a/.github/workflows/build-and-deploy-stage.yml b/.github/workflows/build-and-deploy-stage.yml new file mode 100644 index 0000000..104d9ac --- /dev/null +++ b/.github/workflows/build-and-deploy-stage.yml @@ -0,0 +1,89 @@ +name: Docker image building and publishing onto Stage +on: + push: + branches: [stage] +jobs: + build-and-push: + if: github.repository == 'SayaAndy/saya-today-web' + runs-on: ubuntu-latest + environment: Stage + permissions: + packages: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Generate output.css with tailwindcss + uses: ZoeyVid/tailwindcss-update@main + with: + input: static/input.css + output: static/output.css + params: "--minify" + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: Set github short sha + id: ghss_vars + run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + push: true + tags: | + ghcr.io/sayaandy/saya-today-web:latest + ghcr.io/sayaandy/saya-today-web:commit-${{ steps.ghss_vars.outputs.sha_short }} + + deploy: + if: github.repository == 'SayaAndy/saya-today-web' + runs-on: ubuntu-latest + environment: Stage + needs: [build-and-push] + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up SSH connection for pl.saya.today + run: | + mkdir -p ~/.ssh + (cat < ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + sed -i 's/\r$//' ~/.ssh/id_ed25519 + ssh-keyscan -H pl.saya.today >> ~/.ssh/known_hosts + + - name: Test SSH connection + run: | + ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 svc_github@pl.saya.today "echo 'SSH connection successful'" + + - name: Install Ansible + shell: bash + run: | + sudo apt update + sudo apt install -y ansible + + - name: Set github short sha + id: ghss_vars + run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + + - 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 ~/.ssh/id_ed25519 -u svc_github -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_environment=stage -e saya_today_web_tag=commit-${{ steps.ghss_vars.outputs.sha_short }} diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml deleted file mode 100644 index ac5fa1f..0000000 --- a/.github/workflows/build-and-deploy.yml +++ /dev/null @@ -1,77 +0,0 @@ -name: Publish Docker image -on: - push: - tags: - - "*" -jobs: - build-and-push: - if: github.repository == 'SayaAndy/saya-today-web' - runs-on: ubuntu-latest - environment: Production - permissions: - packages: write - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Generate output.css with tailwindcss - uses: ZoeyVid/tailwindcss-update@main - with: - input: static/input.css - output: static/output.css - params: "--minify" - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - name: Login to GitHub Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GHCR_TOKEN }} - - name: Build and push - uses: docker/build-push-action@v6 - 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 connection for pl.saya.today - run: | - mkdir -p ~/.ssh - (cat < ~/.ssh/id_ed25519 - chmod 600 ~/.ssh/id_ed25519 - sed -i 's/\r$//' ~/.ssh/id_ed25519 - ssh-keyscan -H pl.saya.today >> ~/.ssh/known_hosts - - - name: Test SSH connection - run: | - ssh -o ConnectTimeout=10 -i ~/.ssh/id_ed25519 svc_github@pl.saya.today "echo 'SSH connection successful'" - - - 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 ~/.ssh/id_ed25519 -u svc_github -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/Dockerfile b/Dockerfile index 45d3e8e..30170a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,20 +2,21 @@ FROM golang:1.24.5-alpine3.22 AS build-stage WORKDIR /builddir COPY . . -RUN go build -o sayana-demo . +RUN go build -o sayana-web . FROM alpine:3.22.1 AS runtime-stage +ENV ENVIRONMENT="" ENV B2_KEY_ID="" ENV B2_APPLICATION_KEY="" WORKDIR /app -COPY --from=build-stage /builddir/sayana-demo /app/sayana-demo +COPY --from=build-stage /builddir/sayana-web /app/sayana-web COPY --from=build-stage /builddir/static /app/static COPY --from=build-stage /builddir/views /app/views COPY --from=build-stage /builddir/locale/*.yaml /app/locale/ -COPY --from=build-stage /builddir/config/config.yaml /app/config.yaml +COPY --from=build-stage /builddir/config/config*.yaml /app/config/ RUN apk add --no-cache tzdata -ENTRYPOINT [ "/app/sayana-demo", "-c", "/app/config.yaml" ] +ENTRYPOINT [ "/app/sayana-web", "-c", "/app/config/config.${ENVIRONMENT}.yaml" ] diff --git a/config/config.prod.yaml b/config/config.prod.yaml new file mode 100644 index 0000000..2d050bf --- /dev/null +++ b/config/config.prod.yaml @@ -0,0 +1,20 @@ +logLevel: info +blogPages: + storage: + type: b2 + config: + bucketName: sayana-pages + region: eu-central-003 + prefix: 'prod-' + keyID: '${B2_KEY_ID}' + applicationKey: '${B2_APPLICATION_KEY}' +localePath: ./locale/ +availableLanguages: + - name: ru + alt: Русский + flag: https://f003.backblazeb2.com/file/sayana-static/flags/ru.svg + locFile: localization.ru.yaml + - name: en + alt: English + flag: https://f003.backblazeb2.com/file/sayana-static/flags/gb.svg + locFile: localization.en.yaml diff --git a/config/config.stage.yaml b/config/config.stage.yaml new file mode 100644 index 0000000..a34a33e --- /dev/null +++ b/config/config.stage.yaml @@ -0,0 +1,20 @@ +logLevel: debug +blogPages: + storage: + type: b2 + config: + bucketName: sayana-pages + region: eu-central-003 + prefix: 'stage-' + keyID: '${B2_KEY_ID}' + applicationKey: '${B2_APPLICATION_KEY}' +localePath: ./locale/ +availableLanguages: + - name: ru + alt: Русский + flag: https://f003.backblazeb2.com/file/sayana-static/flags/ru.svg + locFile: localization.ru.yaml + - name: en + alt: English + flag: https://f003.backblazeb2.com/file/sayana-static/flags/gb.svg + locFile: localization.en.yaml diff --git a/config/config.yaml b/config/config.yaml deleted file mode 100644 index dac7b9a..0000000 --- a/config/config.yaml +++ /dev/null @@ -1,20 +0,0 @@ -logLevel: debug -blogPages: - storage: - type: b2 - config: - bucketName: sayana-pages - region: eu-central-003 - prefix: '' - keyID: '${B2_KEY_ID}' - applicationKey: '${B2_APPLICATION_KEY}' -localePath: ./locale/ -availableLanguages: - - name: ru - alt: Русский - flag: https://f003.backblazeb2.com/file/sayana-static/flags/ru.svg - locFile: localization.ru.yaml - - name: en - alt: English - flag: https://f003.backblazeb2.com/file/sayana-static/flags/gb.svg - locFile: localization.en.yaml diff --git a/deploy/playbook.yml b/deploy/playbook.yml index 773bb61..ca22ac3 100644 --- a/deploy/playbook.yml +++ b/deploy/playbook.yml @@ -12,6 +12,7 @@ env: B2_KEY_ID: "{{ saya_today_web_b2_key_id }}" B2_APPLICATION_KEY: "{{ saya_today_web_b2_application_key }}" + ENVIRONMENT: "{{ saya_today_web_environment }}" network_mode: caddy networks: - name: caddy diff --git a/saya-today-web b/saya-today-web deleted file mode 100755 index b6522e8..0000000 Binary files a/saya-today-web and /dev/null differ -- cgit v1.3.1+13 From 2da6a5f4a666da470d8c2170bb9246f0f5804da5 Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Sat, 16 Aug 2025 23:07:23 +0700 Subject: fix: environment subst issue --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 30170a8..cd3e0eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,4 +19,4 @@ COPY --from=build-stage /builddir/config/config*.yaml /app/config/ RUN apk add --no-cache tzdata -ENTRYPOINT [ "/app/sayana-web", "-c", "/app/config/config.${ENVIRONMENT}.yaml" ] +ENTRYPOINT /app/sayana-web -c /app/config/config.${ENVIRONMENT}.yaml -- cgit v1.3.1+13