aboutsummaryrefslogtreecommitdiff
path: root/.gitea/workflows/workflow.yml
blob: c1ae90cec34473343d77a6c7ec50fe203aca5cc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
name: Build and Push Docker Image

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2

    - name: Log in to Gitea Docker registry
      uses: docker/login-action@v2
      with:
        registry: gitea.s1.ctf.rs
        username: ${{ secrets.REGISTRY_USERNAME }}
        password: ${{ secrets.REGISTRY_PASSWORD }}

    - name: Convert repository name to lowercase
      id: convert_repo_name
      run: |
        REPO_NAME=${{ github.repository }}
        LOWER_REPO_NAME=$(echo "$REPO_NAME" | awk '{print tolower($0)}')
        echo "LOWER_REPO_NAME=$LOWER_REPO_NAME" >> $GITHUB_ENV

    - name: Get commit details
      id: commit
      run: |
        echo "COMMIT_HASH=$(git rev-parse HEAD)" >> $GITHUB_ENV
        echo "COMMIT_NAME=$(git log -1 --pretty=%B | base64)" >> $GITHUB_ENV
        echo "COMMIT_DATE=$(date -d "$(git log -1 --pretty=format:%ci)" -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_ENV

    - name: Build and push Docker image
      id: build
      uses: docker/build-push-action@v3
      with:
        context: .
        push: true
        tags: |
          gitea.s1.ctf.rs/${{ env.LOWER_REPO_NAME }}:latest
          gitea.s1.ctf.rs/${{ env.LOWER_REPO_NAME }}:${{ github.sha }}

    - name: Send POST request
      env:
        REPO_NAME: ${{ github.repository }}
        COMMIT_HASH: ${{ env.COMMIT_HASH }}
        COMMIT_NAME: ${{ env.COMMIT_NAME }}
        COMMIT_DATE: ${{ env.COMMIT_DATE }}
        DOCKER_URL: gitea.s1.ctf.rs/${{ env.LOWER_REPO_NAME }}:${{ github.sha }}
      run: |
        RETRY_DELAY=20

        while true; do
          echo "Sending POST request..."
        
          # Create JSON payload
          JSON_PAYLOAD=$(jq -n \
            --arg repo_name "$REPO_NAME" \
            --arg commit_hash "$COMMIT_HASH" \
            --arg commit_message "$COMMIT_NAME" \
            --arg commit_date "$COMMIT_DATE" \
            --arg docker_url "$DOCKER_URL" \
            '{
              repo_name: $repo_name,
              commit_hash: $commit_hash,
              commit_message: $commit_message,
              commit_date: $commit_date,
              docker_url: $docker_url
            }'
          )
        
          # Capture HTTP response code
          RESPONSE=$(curl --max-time 30 -k -s -o /dev/null -w "%{http_code}" -X POST http://advantage.s1.ctf.rs/api/docker_deploy \
            -H "Content-Type: application/json" \
            -d "$JSON_PAYLOAD"
          )
        
          # Check HTTP status code
          if [ "$RESPONSE" -eq 200 ]; then
            echo "Request succeeded."
            break
          else
            echo "Request failed with HTTP status $RESPONSE. Retrying in $RETRY_DELAY seconds..."
            sleep $RETRY_DELAY
          fi
        done