diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 10babe3..94e767d 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -9,7 +9,7 @@ jobs: build: strategy: matrix: - configuration: [Debug, Release] + configuration: [Release] os: [ubuntu-latest, windows-latest] name: Build and Test @@ -19,6 +19,15 @@ jobs: - name: Check out the repo uses: actions/checkout@v3 + # Fetch NuGet packages from cache + - name: Restore package cache + uses: actions/cache@v3 + with: + path: ~/.nuget/packages + key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj*') }} + restore-keys: | + ${{ runner.os }}-nuget + # Install the .NET Core workload - name: Install .NET Core uses: actions/setup-dotnet@v3 diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..d8aaac2 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,99 @@ +name: Deploy to Production + +on: + workflow_dispatch: + inputs: + publish_docker: + description: 'Publish to Docker' + required: false + type: boolean + workflow_call: + inputs: + tag: + required: false + type: string + +concurrency: production + +jobs: + compile: + name: Compile and Test + uses: ./.github/workflows/compile.yml + if: ${{ inputs.publish_docker }} + + publish_docker: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + needs: [compile] + if: ${{ inputs.publish_docker }} + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: novosadkry/novaxis + tags: | + type=raw,value=${{ inputs.tag || github.ref_name }} + flavor: | + latest=false + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + update_server: + name: Update production environment + runs-on: ubuntu-latest + needs: [publish_docker] + if: ${{ !failure() && !cancelled() }} + environment: production + steps: + - name: Replace Docker container + uses: appleboy/ssh-action@master + env: + TAG: ${{ inputs.tag || github.ref_name }} + with: + host: ${{ secrets.PROD_HOST }} + username: ${{ secrets.PROD_USER }} + key: ${{ secrets.PROD_SSH_KEY }} + envs: TAG + script: | + cd ./files/novaxis/ + sudo -E docker-compose down + sudo -E docker-compose pull + sudo -E docker-compose up -d + + restore_server: + name: Restore production environment + runs-on: ubuntu-latest + needs: [update_server] + if: ${{ failure() && needs.update_server.result == 'failure' }} + environment: production + steps: + - name: Replace Docker container + uses: appleboy/ssh-action@master + env: + TAG: 'latest' + with: + host: ${{ secrets.PROD_HOST }} + username: ${{ secrets.PROD_USER }} + key: ${{ secrets.PROD_SSH_KEY }} + envs: TAG + script: | + cd ./files/novaxis/ + sudo -E docker-compose down + sudo -E docker-compose pull + sudo -E docker-compose up -d