Ray Chen commited on
Commit
31ffb4a
·
1 Parent(s): 70d8d48

new: push to space by GitHub Actions

Browse files
.github/tools/deploy_to_space.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import subprocess
3
+ import sys
4
+
5
+ DEFAULT_USER = "jy-raychen"
6
+ DEFAULT_SPACE_OWNER = "jy-raychen"
7
+ DEFAULT_SPACE_NAME = "example"
8
+
9
+
10
+ def force_push_to_remote(token, branch_name):
11
+ proc = subprocess.run(
12
+ [
13
+ "git",
14
+ "push",
15
+ "--force",
16
+ f"https://{DEFAULT_USER}:{token}@huggingface.co/spaces/{DEFAULT_SPACE_OWNER}/{DEFAULT_SPACE_NAME}",
17
+ f"{branch_name}:main",
18
+ ],
19
+ capture_output=True,
20
+ )
21
+
22
+ if proc.returncode == 0:
23
+ # NOTE: `git push` output always goes to stderr
24
+ results = proc.stderr.decode("utf-8")
25
+ return results
26
+
27
+ print(f"Failed to push remote: {proc.stderr.decode('utf-8')}")
28
+ return None
29
+
30
+
31
+ if __name__ == "__main__":
32
+ parser = argparse.ArgumentParser(description="Deploy to Hugging Face spaces.")
33
+ parser.add_argument("--branch_name", required=True)
34
+ parser.add_argument("--user_token", required=True)
35
+ args = parser.parse_args()
36
+
37
+ push_result = force_push_to_remote(args.user_token, args.branch_name)
38
+ if not push_result:
39
+ sys.exit(1)
40
+ print(push_result)
41
+ sys.exit(0)
.github/workflows/deploy-to-space.yaml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Deploy Space
2
+ run-name: Deploy to space
3
+
4
+ on:
5
+ push:
6
+ branches:
7
+ - '**'
8
+
9
+ concurrency:
10
+ group: ${{ github.workflow }}-${{ github.ref_name }}
11
+ cancel-in-progress: true
12
+
13
+ jobs:
14
+ run_deploy_tool:
15
+ runs-on: ubuntu-latest
16
+ permissions:
17
+ contents: read
18
+ steps:
19
+ - name: Check out repository code
20
+ uses: actions/checkout@v4
21
+ with:
22
+ fetch-depth: '0'
23
+ - name: Run deploy tool
24
+ run: python3 .github/tools/deploy_to_space.py --branch_name="${{ github.ref_name }}" --user_token="${{ secrets.HF_TOKEN }}"