Skip to content

Commit

Permalink
add continuous deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kashu-02 committed Jul 15, 2024
1 parent c7451bf commit 68414dd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: CD
on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Set up buildx for docker
uses: docker/setup-buildx-action@v2

- name: Checkout
uses: actions/checkout@v4

- name: Build Docker image and save
id: build-docker
run: |
TAG_NAME=zoo-backend:$(git rev-parse --short HEAD)
docker build -f infra/Dockerfile-deploy -t ${TAG_NAME} .
docker save --output ${TAG_NAME}.tar ${TAG_NAME}
echo "::set-output name=TAG_NAME::${TAG_NAME}"
- name: Install cloudflared
run: |
lastest_version=$(curl -s $GITHUB_API_URL/repos/cloudflare/cloudflared/releases/latest | jq -r '.tag_name')
mkdir -p /tmp/cloudflared
curl -sL -o /tmp/cloudflared/cloudflared $GITHUB_SERVER_URL/cloudflare/cloudflared/releases/download/$lastest_version/cloudflared-linux-amd64
chmod +x /tmp/cloudflared/cloudflared
/tmp/cloudflared/cloudflared --version
- name: Setup SSH
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: ${{ secrets.KNOWN_HOSTS }}
name: id_ed25519
config: |
Host server
HostName ${{ secrets.HOST_NAME }}
User ${{ secrets.USER_NAME }}
IdentityFile ~/.ssh/id_ed25519
ProxyCommand /tmp/cloudflared/cloudflared access ssh --id ${{ secrets.SSH_CLOUDFLARED_ID }} --secret ${{ secrets.SSH_CLOUDFLARED_SECRET }} --hostname %h
- name: Upload binary
run: |
rsync -vhz ${{ steps.build-docker.outputs.TAG_NAME }} server:~/
ssh server 'docker load --input ${{ steps.build-docker.outputs.TAG_NAME }}.tar && rm ${{ steps.build-docker.outputs.TAG_NAME }}.tar && docker stop $(docker ps -q) && docker run -d --restart always -p 8000:8000 ${{ steps.build-docker.outputs.TAG_NAME }}'
16 changes: 16 additions & 0 deletions infra/Dockerfile-deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM rust:1 as build-env

WORKDIR /app

COPY . /app

RUN cargo build --release


FROM gcr.io/distroless/cc-debian12:nonroot

USER nonroot

COPY --chown=nonroot:nonroot --from=build-env /app/target/release/zoo_backend /home/nonroot/

CMD ["/home/nonroot/zoo_backend"]

0 comments on commit 68414dd

Please sign in to comment.