From 8bd64f03c8ee0bced43f86e902cac63a0c7367b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyril=20=C5=A0ebek?= Date: Sun, 7 Jul 2024 21:11:32 +0200 Subject: [PATCH] Autodeployment --- .gitea/workflows/build.yaml | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index c00dc02..df1f59b 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -8,11 +8,11 @@ on: # Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. env: - REGISTRY: 10.69.13.154:5000 IMAGE_NAME: ${{ github.repository }} jobs: build-and-push-image: + name: Build Image and push to Registry runs-on: ubuntu-latest # Sets the permissions granted to the GITHUB_TOKEN for the actions in this job. permissions: @@ -48,4 +48,28 @@ jobs: context: . push: true tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file + labels: ${{ steps.meta.outputs.labels }} + + deploy: + name: Deploy on webserver + runs-on: ubuntu-latest + needs: build-and-push-image + steps: + + - name: Install SSH key for target server + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.SSH_PRIV_KEY }} + + - name: Login to registry + run: ssh ${{ env.TARGET_USER }}@${{ env.TARGET }} "docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} ${{ env.REGISTRY }}" + - name: Pull new image + run: ssh ${{ env.TARGET_USER }}@{{ env.TARGET }} "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" + - name: Logout from registry + run: ssh ${{ env.TARGET_USER }}@{{ env.TARGET }} "docker logout ${{ env.REGISTRY }}" + - name: Stop and Remove the container + run: ssh ${{ env.TARGET_USER }}@{{ env.TARGET }} "docker stop ${{ env.IMAGE_NAME }} && docker remove ${{ env.IMAGE_NAME }}" + - name: Recreate the container + run: ssh ${{ env.TARGET_USER }}@{{ env.TARGET }} "docker run --name=${{ env.IMAGE_NAME }} ${{ env.IMAGE_NAME }}:latest" + - name: Clean old images + run: ssh ${{ env.TARGET_USER }}@{{ env.TARGET }} "docker image prune -a"