|
| 1 | +name: CD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + # branches: |
| 6 | + # - main |
| 7 | + pull_request: |
| 8 | + # branches: |
| 9 | + # - main |
| 10 | + workflow_dispatch: # Позволяет запустить workflow вручную через интерфейс GitHub |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + name: Build and Push to GHCR |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + # needs: test |
| 18 | + steps: |
| 19 | + - name: Check out code |
| 20 | + uses: actions/checkout@v2 |
| 21 | + |
| 22 | + - name: Set up Go |
| 23 | + uses: actions/setup-go@v2 |
| 24 | + with: |
| 25 | + go-version: '1.23' |
| 26 | + |
| 27 | + - name: Build Docker Image |
| 28 | + run: | |
| 29 | + git fetch --unshallow --tags |
| 30 | + BUILDTIME=$(date -R) |
| 31 | + docker build --build-arg VERSION=$(git describe --tags --always) --build-arg BUILDTIME='$BUILDTIME' . -f build/Dockerfile -t ghcr.io/${{ github.repository }}:latest |
| 32 | +
|
| 33 | + - name: Log in to GitHub Container Registry |
| 34 | + uses: docker/login-action@v2 |
| 35 | + with: |
| 36 | + registry: ghcr.io |
| 37 | + username: ${{ github.actor }} |
| 38 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + |
| 40 | + - name: Push Docker Image |
| 41 | + run: docker push ghcr.io/${{ github.repository }}:latest |
| 42 | + |
| 43 | + deploy: |
| 44 | + name: Deploy to Staging |
| 45 | + runs-on: ubuntu-latest |
| 46 | + needs: build |
| 47 | + # if: github.event_name == 'workflow_dispatch' |
| 48 | + steps: |
| 49 | + - name: Install SSH and other dependencies |
| 50 | + run: | |
| 51 | + sudo apt-get update |
| 52 | + sudo apt-get install -y openssh-client |
| 53 | +
|
| 54 | + - name: Set up SSH |
| 55 | + run: | |
| 56 | + mkdir -p ~/.ssh |
| 57 | + echo "${{ secrets.STAGE_SSH_KEY }}" > ~/.ssh/id_rsa |
| 58 | + chmod 600 ~/.ssh/id_rsa |
| 59 | + ssh-keyscan -H ctf01d.ru >> ~/.ssh/known_hosts |
| 60 | +
|
| 61 | + - name: Deploy Docker Compose to Staging |
| 62 | + run: | |
| 63 | + scp build/docker-compose.staging.yml root@ctf01d.ru:/root/ctf01d-training-platform/build |
| 64 | + scp build/docker-compose.yml root@ctf01d.ru:/root/ctf01d-training-platform/build |
| 65 | + 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" |
| 66 | +
|
| 67 | + environment: |
| 68 | + name: staging |
| 69 | + url: http://staging.example.com |
0 commit comments