|
| 1 | +name: Build and Deploy CompreFace on push |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - '1.1.x' |
| 8 | + |
| 9 | +env: |
| 10 | + REGISTRY: ghcr.io |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + permissions: |
| 16 | + contents: read |
| 17 | + packages: write |
| 18 | + # Map steps outputs to a job outputs. |
| 19 | + # We need to share it between build and deploy jobs. |
| 20 | + outputs: |
| 21 | + registry_path: ${{ steps.registry_path.outputs.registry_path }} |
| 22 | + tag: ${{ steps.tag_var.outputs.tag }} |
| 23 | + env_name: ${{ steps.env_var.outputs.env_name }} |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout Repo |
| 27 | + uses: actions/checkout@v3 |
| 28 | + |
| 29 | + - name: Set registry path output |
| 30 | + id: registry_path |
| 31 | + run: echo "registry_path=${{ env.REGISTRY }}/exadel-inc/compreface/" >> $GITHUB_OUTPUT |
| 32 | + |
| 33 | + - name: Set commit sha output from git |
| 34 | + id: tag_var |
| 35 | + run: echo "tag=${{ github.ref_name }}-$(git rev-parse HEAD | cut -c 1-7 | tr -d '\n')" >> $GITHUB_OUTPUT |
| 36 | + |
| 37 | + - name: Set environment output from git |
| 38 | + id: env_var |
| 39 | + run: | |
| 40 | + if [ "${{ github.ref_name }}" = "master" ]; then |
| 41 | + echo "env_name=dev" >> $GITHUB_OUTPUT |
| 42 | + elif [ "${{ github.ref_name }}" = "1.1.x" ]; then |
| 43 | + echo "env_name=stage" >> $GITHUB_OUTPUT |
| 44 | + else |
| 45 | + echo "env_name=Features" >> $GITHUB_OUTPUT |
| 46 | + fi |
| 47 | +
|
| 48 | + - name: Check outputs |
| 49 | + run: | |
| 50 | + echo "Branch : ${{ github.ref_name }}" |
| 51 | + echo "TAG : ${{ steps.tag_var.outputs.tag }}" |
| 52 | + echo "Environment: ${{ steps.env_var.outputs.env_name }}" |
| 53 | +
|
| 54 | + - name: Build images |
| 55 | + env: |
| 56 | + TAG: ${{ steps.tag_var.outputs.tag }} |
| 57 | + REGISTRY_PATH: ${{ steps.registry_path.outputs.registry_path }} |
| 58 | + APPERY_API_KEY: ${{ secrets.APPERY_API_KEY }} |
| 59 | + working-directory: ./dev |
| 60 | + run: | |
| 61 | + sed -i "s|registry=|registry=${REGISTRY_PATH}|g" .env |
| 62 | + sed -i "s/latest/${TAG}/g" .env |
| 63 | + docker-compose build |
| 64 | + docker images |
| 65 | +
|
| 66 | + - name: Log in to the Container registry |
| 67 | + uses: docker/login-action@v2 |
| 68 | + with: |
| 69 | + registry: ${{ env.REGISTRY }} |
| 70 | + username: ${{ github.actor }} |
| 71 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + |
| 73 | + - name: Push images to the Container registry |
| 74 | + working-directory: ./dev |
| 75 | + run: | |
| 76 | + docker-compose push |
| 77 | +
|
| 78 | + deploy: |
| 79 | + needs: build |
| 80 | + # It's not possible to use natively env (e.g. env.ENV_NAME) variable on the runs-on job field (yet?) |
| 81 | + # for deploy to different environments depending on branch https://github.com/actions/runner/issues/480 |
| 82 | + # That's why we use output from the previous build job |
| 83 | + # Note: we are using self-hosted runner here |
| 84 | + runs-on: ["${{needs.build.outputs.env_name}}"] |
| 85 | + |
| 86 | + steps: |
| 87 | + - name: Checkout Repo |
| 88 | + uses: actions/checkout@v3 |
| 89 | + |
| 90 | + - name: Deploy |
| 91 | + working-directory: ./dev |
| 92 | + env: |
| 93 | + TAG: ${{ needs.build.outputs.tag }} |
| 94 | + REGISTRY_PATH: ${{ needs.build.outputs.registry_path }} |
| 95 | + run: | |
| 96 | + sed -i "s|registry=|registry=${REGISTRY_PATH}|g" .env |
| 97 | + sed -i "s/latest/${TAG}/g" .env |
| 98 | + sudo docker-compose stop |
| 99 | + sudo docker system prune -a -f |
| 100 | + sudo docker-compose pull |
| 101 | + HOSTNAME=$HOSTNAME sudo docker-compose -f docker-compose.yml -f docker-compose.env.yml up -d |
0 commit comments