From cf1bf0adaa006d169683c10dc463390cc31337b3 Mon Sep 17 00:00:00 2001 From: DrunkBatya Date: Thu, 11 Jul 2024 07:17:18 +0000 Subject: [PATCH] Add CI/CD --- .github/workflows/build.yml | 90 +++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..309861b --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,90 @@ +name: 'Build' + +on: + push: + branches: + - dev + tags: + - '*' + pull_request: + +env: + IMAGE_NAME: "flipperdevices/irdb-backend" + +jobs: + build: + runs-on: ubuntu-22.04 + if: ${{ !github.event.pull_request.head.repo.fork }} + steps: + - name: 'Checkout code' + uses: actions/checkout@v4 + with: + submodules: true + + - name: 'Set image tag and name' + id: tag + run: | + IMAGE_TAG="0.0.0" + if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then + REF=${{ github.ref }}; + TAG_FULL=${REF#refs/*/}; + IMAGE_TAG=${TAG_FULL//\//_}; + fi + echo "image_tag=${IMAGE_TAG}" >> $GITHUB_OUTPUT + echo "image_name=${IMAGE_NAME}" >> $GITHUB_OUTPUT + + - name: 'Login to registry' + uses: docker/login-action@v3 + with: + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: 'Set up Docker Buildx' + uses: docker/setup-buildx-action@v3 + + - name: 'Build' + uses: docker/build-push-action@v5 + with: + push: false + tags: ${{ steps.tag.outputs.image_name }}:${{ steps.tag.outputs.image_tag }} + cache-from: type=registry,ref=${{ steps.tag.outputs.image_name }}:buildcache + + - name: 'Push' + if: ${{ github.event_name != 'pull_request' }} + uses: docker/build-push-action@v5 + with: + push: true + tags: ${{ steps.tag.outputs.image_name }}:${{ steps.tag.outputs.image_tag }} + cache-from: type=registry,ref=${{ steps.tag.outputs.image_name }}:buildcache + cache-to: type=registry,ref=${{ steps.tag.outputs.image_name }}:buildcache,mode=max + + - name: 'Trigger k8s to use new image: dev' + if: ${{ (github.event_name != 'pull_request') + && (steps.tag.outputs.image_tag == '0.0.0') }} + uses: peter-evans/repository-dispatch@v3 + with: + repository: ${{ secrets.INFRASTRUCTURE_REPO }} + token: ${{ secrets.K8S_GITHUB_PAT }} + event-type: irdb-backend-dev-deploy + client-payload: '{"image_tag": "${{steps.tag.outputs.image_tag}}"}' + + - name: 'Trigger k8s to use new image: stage' + if: ${{ (github.event_name != 'pull_request') + && (endsWith(github.event.client_payload.image_tag, '-rc')) }} + uses: peter-evans/repository-dispatch@v3 + with: + repository: ${{ secrets.INFRASTRUCTURE_REPO }} + token: ${{ secrets.K8S_GITHUB_PAT }} + event-type: irdb-backend-stage-deploy + client-payload: '{"image_tag": "${{steps.tag.outputs.image_tag}}"}' + + - name: 'Trigger k8s to use new image: prod' + if: ${{ (github.event_name != 'pull_request') + && (steps.tag.outputs.image_tag != '0.0.0') + && (!endsWith(github.event.client_payload.image_tag, '-rc')) }} + uses: peter-evans/repository-dispatch@v3 + with: + repository: ${{ secrets.INFRASTRUCTURE_REPO }} + token: ${{ secrets.K8S_GITHUB_PAT }} + event-type: irdb-backend-prod-deploy + client-payload: '{"image_tag": "${{steps.tag.outputs.image_tag}}"}'