From f34df55e4558af4ce707864d82cc719fd32993d3 Mon Sep 17 00:00:00 2001 From: IgorPolyakov Date: Wed, 18 Sep 2024 17:39:19 +0300 Subject: [PATCH] ci(deps): :rocket: add delivery by button --- .github/workflows/build.yml | 31 +---------------- .github/workflows/cd.yml | 69 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 91f7925..deb2763 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: CI/CD +name: CI on: push: @@ -9,35 +9,6 @@ on: - main jobs: - build: - name: Build and Push to GHCR - runs-on: ubuntu-latest - - needs: test - steps: - - name: Check out code - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: '1.23' - - - name: Build Docker Image - run: | - git fetch --unshallow --tags - BUILDTIME=$(date -R) - docker build --build-arg VERSION=$(git describe --tags --always) --build-arg BUILDTIME='$BUILDTIME' . -f build/Dockerfile -t ghcr.io/${{ github.repository }}:latest - - - name: Log in to GitHub Container Registry - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Push Docker Image - run: docker push ghcr.io/${{ github.repository }}:latest check-dependencies: name: Check dependencies with nancy runs-on: ubuntu-latest diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..d7939cc --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,69 @@ +name: CD + +on: + push: + # branches: + # - main + pull_request: + # branches: + # - main + workflow_dispatch: # Позволяет запустить workflow вручную через интерфейс GitHub + +jobs: + build: + name: Build and Push to GHCR + runs-on: ubuntu-latest + + # needs: test + steps: + - name: Check out code + uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: '1.23' + + - name: Build Docker Image + run: | + git fetch --unshallow --tags + BUILDTIME=$(date -R) + docker build --build-arg VERSION=$(git describe --tags --always) --build-arg BUILDTIME='$BUILDTIME' . -f build/Dockerfile -t ghcr.io/${{ github.repository }}:latest + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Push Docker Image + run: docker push ghcr.io/${{ github.repository }}:latest + + deploy: + name: Deploy to Staging + runs-on: ubuntu-latest + needs: build + # if: github.event_name == 'workflow_dispatch' + steps: + - name: Install SSH and other dependencies + run: | + sudo apt-get update + sudo apt-get install -y openssh-client + + - name: Set up SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.STAGE_SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H ctf01d.ru >> ~/.ssh/known_hosts + + - name: Deploy Docker Compose to Staging + run: | + scp build/docker-compose.staging.yml root@ctf01d.ru:/root/ctf01d-training-platform/build + scp build/docker-compose.yml root@ctf01d.ru:/root/ctf01d-training-platform/build + ssh root@ctf01d.ru "docker login ghcr.io -u '${{ secrets.GHCR_USERNAME }}' -p '${{ secrets.GHCR_TOKEN }}' && docker compose -f docker-compose.yml -f docker-compose.stage.yml pull && docker compose -f docker-compose.yml -f docker-compose.stage.yml stop && docker compose -f docker-compose.yml -f docker-compose.stage.yml up -d" + + environment: + name: staging + url: http://staging.example.com