T-15 Fix incorrect scheduled job time (#49) #41
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Ubuntu Server, build and run docker | |
on: | |
push: | |
branches: | |
- main | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
# 1. Checkout repository | |
checkout: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
# 2. Get Docker container ID | |
get_container_id: | |
runs-on: ubuntu-latest | |
needs: checkout | |
outputs: | |
container_id: ${{ steps.get_container_id.outputs.container_id }} | |
steps: | |
- name: Setup SSH | |
uses: webfactory/ssh-agent@v0.9.0 | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Get Docker container ID | |
id: get_container_id | |
run: | | |
CONTAINER_ID=$(ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'docker ps -qf name=${{ secrets.CONTAINER_NAME }}') | |
echo "container_id=$CONTAINER_ID" >> "$GITHUB_OUTPUT" | |
# 3. Update code | |
update_code: | |
runs-on: ubuntu-latest | |
needs: get_container_id | |
outputs: | |
container_id: ${{ needs.get_container_id.outputs.container_id }} | |
steps: | |
- name: Setup SSH | |
uses: webfactory/ssh-agent@v0.9.0 | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Extract branch name | |
id: extract_branch | |
run: | | |
BRANCH_NAME=$(echo "${GITHUB_REF#refs/heads/}") | |
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV | |
echo "container_id=${{ needs.get_container_id.outputs.container_id }}" >> "$GITHUB_OUTPUT" | |
- name: Update code | |
run: | | |
BRANCH_NAME="${{ steps.extract_branch.outputs.branch }}" | |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'cd /root/MEOWLSServer/MEOWLSServer/ && git pull origin $BRANCH_NAME' | |
# 4. Build Docker image | |
build_docker_image: | |
runs-on: ubuntu-latest | |
needs: update_code | |
outputs: | |
container_id: ${{ needs.update_code.outputs.container_id }} | |
steps: | |
- name: Setup SSH | |
uses: webfactory/ssh-agent@v0.9.0 | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Build Docker image | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'cd /root/MEOWLSServer/MEOWLSServer/ && docker build -t ${{ secrets.CONTAINER_NAME }} .' | |
echo "container_id=${{ needs.update_code.outputs.container_id }}" >> "$GITHUB_OUTPUT" | |
# 5. Stop and remove old container | |
stop_remove_old_container: | |
runs-on: ubuntu-latest | |
needs: build_docker_image | |
if: ${{ needs.build_docker_image.outputs.container_id != '' }} | |
steps: | |
- name: Setup SSH | |
uses: webfactory/ssh-agent@v0.9.0 | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Stop and remove old container | |
run: | | |
echo "Remove container with ID ${{ needs.build_docker_image.outputs.container_id }}" | |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'docker container stop ${{ needs.build_docker_image.outputs.container_id }} && docker container rm ${{ needs.build_docker_image.outputs.container_id }}' | |
# 6. Run new container | |
run_new_container: | |
runs-on: ubuntu-latest | |
needs: | |
- stop_remove_old_container | |
- build_docker_image | |
if: ${{ always() }} | |
steps: | |
- name: Setup SSH | |
uses: webfactory/ssh-agent@v0.9.0 | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Run new container | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} ' | |
docker run --restart=always \ | |
--name "${{ secrets.CONTAINER_NAME }}" \ | |
--network meowls_network \ | |
-e DATABASE_HOST="${{ secrets.DATABASE_HOST }}" \ | |
-e DATABASE_NAME="${{ secrets.DATABASE_NAME }}" \ | |
-e DATABASE_PASSWORD="${{ secrets.DATABASE_PASSWORD }}" \ | |
-e DATABASE_USERNAME="${{ secrets.DATABASE_USERNAME }}" \ | |
-e DADATA_TOKEN="${{ secrets.DADATA_TOKEN }}" \ | |
-p "${{ secrets.DATABASE_PORT }}" \ | |
-d "${{ secrets.CONTAINER_NAME }}:latest"' | |
# 6. Clean up unused images and containers | |
clean_up: | |
runs-on: ubuntu-latest | |
needs: run_new_container | |
if: ${{ always() }} | |
steps: | |
- name: Setup SSH | |
uses: webfactory/ssh-agent@v0.9.0 | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Clean up unused images and containers | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} 'docker system prune -a -f' |