From 049dfd6448829727580cf5eec612b41945984ceb Mon Sep 17 00:00:00 2001 From: AzamatKomaev Date: Tue, 15 Oct 2024 22:06:10 +0300 Subject: [PATCH 1/4] Fix build.yaml --- .github/workflows/build.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 84e01f9..ef683f8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -33,7 +33,7 @@ jobs: --bootstrap --use - name: Build & Publish backend to Github Container registry run: | - docker buildx build --platform linux/amd64,linux/arm64 ./backend + docker buildx build --platform linux/amd64,linux/arm64 ./backend \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:latest \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:$IMAGE_TAG docker push $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:latest @@ -41,7 +41,7 @@ jobs: - name: Build & Publish frontend to Github Container registry run: | - docker buildx build --platform linux/amd64,linux/arm64 ./frontend + docker buildx build --platform linux/amd64,linux/arm64 ./frontend \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:latest \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:$IMAGE_TAG docker push $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:latest From 1229c4b8a89bcbf186adfb555b8728dcb9f398d7 Mon Sep 17 00:00:00 2001 From: AzamatKomaev Date: Tue, 15 Oct 2024 22:38:39 +0300 Subject: [PATCH 2/4] Fix build.yaml --- .github/workflows/build.yaml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ef683f8..95940d6 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -33,16 +33,12 @@ jobs: --bootstrap --use - name: Build & Publish backend to Github Container registry run: | - docker buildx build --platform linux/amd64,linux/arm64 ./backend \ + docker buildx build --platform linux/amd64,linux/arm64 --push ./backend \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:latest \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:$IMAGE_TAG - docker push $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:latest - docker push $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:$IMAGE_TAG - name: Build & Publish frontend to Github Container registry run: | - docker buildx build --platform linux/amd64,linux/arm64 ./frontend \ + docker buildx build --platform linux/amd64,linux/arm64 --push ./frontend \ --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:latest \ - --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:$IMAGE_TAG - docker push $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:latest - docker push $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:$IMAGE_TAG \ No newline at end of file + --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:$IMAGE_TAG \ No newline at end of file From b97bfa640e7be69e5f3bafd3ec322d9341da2755 Mon Sep 17 00:00:00 2001 From: AzamatKomaev Date: Tue, 15 Oct 2024 22:50:17 +0300 Subject: [PATCH 3/4] Add .version to repo, fix update docker compose step --- .github/workflows/deploy.yaml | 12 ++++++++---- .gitignore | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 82f4822..39bbc3c 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -30,10 +30,14 @@ jobs: - run: ssh-keyscan -p $SSH_PORT $SSH_HOST >> ~/.ssh/known_hosts - run: ssh-agent -a $SSH_AUTH_SOCK > /dev/null - run: ssh-add - <<< "${{ secrets.SSH_KEY }}" - + - name: Add image tag to .version file + run: | + echo $IMAGE_TAG > .version + scp -P 666 .version $SSH_USER@$SSH_HOST:~/YetAnotherCalendar/.version + - name: Update docker compose run: | - ssh $SSH_USER@$SSH_HOST -p $SSH_PORT "cd YetAnotherCalendar/ && - YET_ANOTHER_CALENDAR_VERSION=$IMAGE_TAG docker compose -f docker-compose.prod.yaml pull - YET_ANOTHER_CALENDAR_VERSION=$IMAGE_TAG docker compose -f docker-compose.prod.yaml up -d" + ssh $SSH_USER@$SSH_HOST -p $SSH_PORT "cd YetAnotherCalendar/ && git pull origin main + YET_ANOTHER_CALENDAR_VERSION=$(cat .version) docker compose -f docker-compose.prod.yaml pull + YET_ANOTHER_CALENDAR_VERSION=$(cat .version) docker compose -f docker-compose.prod.yaml up -d" \ No newline at end of file diff --git a/.gitignore b/.gitignore index a7ef229..2c15530 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea +.version # Byte-compiled / optimized / DLL files __pycache__/ From 235b078b941a129e2268c923a2aade178238f304 Mon Sep 17 00:00:00 2001 From: AzamatKomaev Date: Wed, 16 Oct 2024 19:15:27 +0300 Subject: [PATCH 4/4] add new workflow build-and-deploy.yaml (for main branch only) --- .github/workflows/build-and-deploy.yaml | 71 +++++++++++++++++++++++++ .github/workflows/build.yaml | 3 +- .github/workflows/deploy.yaml | 43 --------------- 3 files changed, 72 insertions(+), 45 deletions(-) create mode 100644 .github/workflows/build-and-deploy.yaml delete mode 100644 .github/workflows/deploy.yaml diff --git a/.github/workflows/build-and-deploy.yaml b/.github/workflows/build-and-deploy.yaml new file mode 100644 index 0000000..ca3b079 --- /dev/null +++ b/.github/workflows/build-and-deploy.yaml @@ -0,0 +1,71 @@ +name: build and deploy +on: + push: + branches: + - main + workflow_dispatch: + +env: + REGISTRY_URL: ghcr.io + REGISTRY_USERNAME: azamatkomaev + REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }} + IMAGE_TAG: ${{ github.sha }} + + SSH_AUTH_SOCK: /tmp/ssh_agent.sock + SSH_HOST: ${{ secrets.SSH_HOST }} + SSH_USER: ${{ secrets.SSH_USER }} + SSH_PORT: ${{ secrets.SSH_PORT }} + + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Login with Github Container registry + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY_URL }} + username: ${{ env.REGISTRY_USERNAME }} + password: ${{ env.REGISTRY_PASSWORD }} + - name: Add custom builder + run: | + docker buildx create \ + --name container-builder \ + --driver docker-container \ + --bootstrap --use + - name: Build & Publish backend to Github Container registry + run: | + docker buildx build --platform linux/amd64,linux/arm64 --push ./backend \ + --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:latest \ + --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_backend:$IMAGE_TAG + + - name: Build & Publish frontend to Github Container registry + run: | + docker buildx build --platform linux/amd64,linux/arm64 --push ./frontend \ + --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:latest \ + --tag $REGISTRY_URL/$REGISTRY_USERNAME/yet_another_calendar_frontend:$IMAGE_TAG + + deploy: + runs-on: ubuntu-latest + needs: [build] + steps: + - uses: actions/checkout@v4 + - run: 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )' + - name: Set up SSH private key. + run: eval "$(ssh-agent -s)" + - run: mkdir -p ~/.ssh + - run: ssh-keyscan -p $SSH_PORT $SSH_HOST >> ~/.ssh/known_hosts + - run: ssh-agent -a $SSH_AUTH_SOCK > /dev/null + - run: ssh-add - <<< "${{ secrets.SSH_KEY }}" + - name: Add image tag to .version file + run: | + echo $IMAGE_TAG > .version + scp -P 666 .version $SSH_USER@$SSH_HOST:~/YetAnotherCalendar/.version + + - name: Update docker compose + run: | + ssh $SSH_USER@$SSH_HOST -p $SSH_PORT "cd YetAnotherCalendar/ && git pull origin main && + YET_ANOTHER_CALENDAR_VERSION=$(cat .version) docker compose -f docker-compose.prod.yaml pull && + YET_ANOTHER_CALENDAR_VERSION=$(cat .version) docker compose -f docker-compose.prod.yaml up -d" + \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 95940d6..68ca4dd 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,7 +3,6 @@ on: push: branches: - dev - - main paths-ignore: - "README.md" workflow_dispatch: @@ -18,7 +17,7 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Login with Github Container registry uses: docker/login-action@v2 with: diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml deleted file mode 100644 index 39bbc3c..0000000 --- a/.github/workflows/deploy.yaml +++ /dev/null @@ -1,43 +0,0 @@ -name: deploy -on: - push: - branches: - - main - workflow_run: - workflows: [build] - types: - - completed - workflow_dispatch: - -env: - IMAGE_TAG: ${{ github.sha }} - SSH_AUTH_SOCK: /tmp/ssh_agent.sock - SSH_HOST: ${{ secrets.SSH_HOST }} - SSH_USER: ${{ secrets.SSH_USER }} - SSH_PORT: ${{ secrets.SSH_PORT }} - - -jobs: - deploy: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - run: 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )' - - name: Set up SSH private key. - run: eval "$(ssh-agent -s)" - - run: mkdir -p ~/.ssh - - run: ssh-keyscan -p $SSH_PORT $SSH_HOST >> ~/.ssh/known_hosts - - run: ssh-agent -a $SSH_AUTH_SOCK > /dev/null - - run: ssh-add - <<< "${{ secrets.SSH_KEY }}" - - name: Add image tag to .version file - run: | - echo $IMAGE_TAG > .version - scp -P 666 .version $SSH_USER@$SSH_HOST:~/YetAnotherCalendar/.version - - - name: Update docker compose - run: | - ssh $SSH_USER@$SSH_HOST -p $SSH_PORT "cd YetAnotherCalendar/ && git pull origin main - YET_ANOTHER_CALENDAR_VERSION=$(cat .version) docker compose -f docker-compose.prod.yaml pull - YET_ANOTHER_CALENDAR_VERSION=$(cat .version) docker compose -f docker-compose.prod.yaml up -d" - \ No newline at end of file