|
| 1 | +name: Build and Deploy Classifier API to Production |
| 2 | + |
| 3 | +# 1 |
| 4 | +# Controls when the workflow will run |
| 5 | +on: |
| 6 | + # Triggers the workflow on push events but only for the master branch |
| 7 | + push: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | + # Allows you to run this workflow manually from the Actions tab |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + version: |
| 14 | + description: "Production" |
| 15 | + required: true |
| 16 | +#2 |
| 17 | +env: |
| 18 | + IMAGE_NAME: "classifier_api" |
| 19 | + CONTAINER_NAME: "classifier_api_prod_ctr" |
| 20 | + |
| 21 | +#3 |
| 22 | +jobs: |
| 23 | + build_and_push: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout the repo |
| 27 | + uses: actions/checkout@v2 |
| 28 | + |
| 29 | + - name: Log in to Docker Hub |
| 30 | + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 |
| 31 | + with: |
| 32 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 33 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 34 | + |
| 35 | + - name: Extract metadata (tags, labels) for Docker |
| 36 | + id: meta |
| 37 | + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 |
| 38 | + with: |
| 39 | + images: pawitrawarda/classifier_api |
| 40 | + |
| 41 | + - name: Build and push Docker image |
| 42 | + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc |
| 43 | + with: |
| 44 | + context: . |
| 45 | + push: true |
| 46 | + tags: pawitrawarda/classifier_api:latest |
| 47 | + labels: latest |
| 48 | + |
| 49 | + deploy: |
| 50 | + runs-on: ubuntu-latest |
| 51 | + needs: build_and_push |
| 52 | + steps: |
| 53 | + - name: Deploy to VPS via SSH action |
| 54 | + uses: appleboy/ssh-action@master |
| 55 | + with: |
| 56 | + host: ${{ secrets.HOST }} |
| 57 | + username: ${{ secrets.USERNAME }} |
| 58 | + key: ${{ secrets.SSHKEY }} |
| 59 | + port: ${{ secrets.PORT }} |
| 60 | + passphrase: ${{ secrets.PASSPHRASE }} |
| 61 | + envs: IMAGE_NAME, CONTAINER_NAME |
| 62 | + script: | |
| 63 | + # Pull registry |
| 64 | + docker pull pawitrawarda/$(echo $IMAGE_NAME):latest |
| 65 | + # Stop running container |
| 66 | + docker stop $(echo $CONTAINER_NAME) |
| 67 | + # Remove old container |
| 68 | + docker rm $(echo $CONTAINER_NAME) |
| 69 | + # Run a new container from a new image |
| 70 | + docker run -d -p 8111:8123 --name $(echo $CONTAINER_NAME) pawitrawarda/$(echo $IMAGE_NAME):latest |
0 commit comments