File size: 4,093 Bytes
8d3475a
 
 
 
 
 
 
 
 
62f43a3
8d3475a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: Sync to Hugging Face Hub

on:
  push:
    branches: [main]
  workflow_dispatch:

env:
  HF_USERNAME: jake-molnia
  HF_SPACE_NAME: CS_553

jobs:
  sync-to-hub:
    runs-on: ubuntu-latest
    environment: deployment_to_huggingface

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
      with:
        fetch-depth: 0
        lfs: true

    - name: Setup Git configuration
      run: |
        git config --global user.name "GitHub Action"
        git config --global user.email "action@github.com"

    - name: Verify Git status
      run: |
        git status
        git log -n 1

    - name: Setup Hugging Face CLI
      run: pip install huggingface_hub

    - name: Login to Hugging Face
      env:
        HF_TOKEN: ${{ secrets.HF_TOKEN }}
      run: huggingface-cli login --token $HF_TOKEN

    - name: Sync to Hugging Face Hub
      env:
        HF_TOKEN: ${{ secrets.HF_TOKEN }}
      run: |
        echo "Syncing to Hugging Face Space: ${{ env.HF_USERNAME }}/${{ env.HF_SPACE_NAME }}"
        if ! git push https://${{ env.HF_USERNAME }}:$HF_TOKEN@huggingface.co/spaces/${{ env.HF_USERNAME }}/${{ env.HF_SPACE_NAME }} main --force; then
          echo "Push failed. Attempting to resolve conflicts..."
          git config --global pull.rebase false
          git pull https://${{ env.HF_USERNAME }}:$HF_TOKEN@huggingface.co/spaces/${{ env.HF_USERNAME }}/${{ env.HF_SPACE_NAME }} main --allow-unrelated-histories
          git push https://${{ env.HF_USERNAME }}:$HF_TOKEN@huggingface.co/spaces/${{ env.HF_USERNAME }}/${{ env.HF_SPACE_NAME }} main
        fi

    - name: Verify sync
      run: |
        echo "Verifying sync..."
        git ls-remote --exit-code https://${{ env.HF_USERNAME }}:$HF_TOKEN@huggingface.co/spaces/${{ env.HF_USERNAME }}/${{ env.HF_SPACE_NAME }} main

    - name: Cleanup
      if: always()
      run: |
        echo "Cleaning up..."
        huggingface-cli logout
        git config --global --unset user.name
        git config --global --unset user.email

    - name: Notify Discord
      if: always()
      env:
        DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
      run: |
        if [ "${{ job.status }}" == "success" ]; then
          COLOR="3066993"
          TITLE="๐Ÿš€ Sync Successful!"
          DESCRIPTION="The sync to Hugging Face Hub completed successfully."
          EMOJI="โœ…"
        else
          COLOR="15158332"
          TITLE="๐Ÿ’ฅ Sync Failed"
          DESCRIPTION="The sync to Hugging Face Hub encountered an error."
          EMOJI="โŒ"
        fi
        
        # Prepare the JSON payload
        PAYLOAD=$(cat <<EOF
        {
          "embeds": [{
            "title": "$TITLE",
            "description": "$DESCRIPTION",
            "color": $COLOR,
            "fields": [
              {
                "name": "Repository",
                "value": "${{ github.repository }}",
                "inline": true
              },
              {
                "name": "Branch",
                "value": "${{ github.ref_name }}",
                "inline": true
              },
              {
                "name": "Triggered by",
                "value": "${{ github.event_name }}",
                "inline": true
              }
            ],
            "footer": {
              "text": "$EMOJI Workflow run completed at $(date -u "+%Y-%m-%d %H:%M:%S UTC")"
            }
          }],
          "username": "GitHub Actions",
          "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
        }
        EOF
        )
        
        # Send the payload and capture the response
        RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Content-Type: application/json" -d "$PAYLOAD" $DISCORD_WEBHOOK)
        
        # Check if the request was successful
        if [ $RESPONSE -ne 204 ]; then
          echo "Failed to send Discord notification. HTTP response code: $RESPONSE"
          echo "Payload:"
          echo "$PAYLOAD"
          exit 1
        else
          echo "Discord notification sent successfully!"
        fi