Skip to content

Commit b5c4bad

Browse files
committed
use Python code to update Docker hub README
1 parent 5790e5d commit b5c4bad

File tree

2 files changed

+122
-12
lines changed

2 files changed

+122
-12
lines changed

.github/workflows/docker.yml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- name: Checkout master branch
10-
uses: actions/checkout@v2
11-
- name: Build Docker image
10+
uses: actions/checkout@v3
11+
- name: Build and optionally push Docker image
1212
env:
1313
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
1414
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
1515
OPENGROK_REPO_SLUG: ${{ github.repository }}
1616
OPENGROK_PULL_REQUEST: ${{ github.head_ref }}
1717
OPENGROK_REF: ${{ github.ref }}
1818
run: ./dev/docker.sh
19-
- name: push README to Dockerhub on release
20-
if: startsWith(github.ref, 'refs/tags/v')
21-
uses: christian-korneck/update-container-description-action@v1
22-
env:
23-
DOCKER_USER: ${{ secrets.DOCKER_USERNAME }}
24-
DOCKER_PASS: ${{ secrets.DOCKER_PASSWORD }}
19+
- uses: actions/setup-python@v4
2520
with:
26-
destination_container_repo: opengrok/docker
27-
provider: dockerhub
28-
short_description: 'Official OpenGrok Docker image'
29-
readme_file: 'docker/README.md'
21+
python-version: '3.10'
22+
- name: Optionally update README on Docker hub
23+
env:
24+
OPENGROK_PULL_REQUEST: ${{ github.head_ref }}
25+
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
26+
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
27+
run: ./dev/dockerhub_readme.py

dev/dockerhub_readme.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Update the README on Docker hub.
5+
6+
This script uses the following secure variables:
7+
- DOCKER_USERNAME
8+
- DOCKER_PASSWORD
9+
10+
These are set via https://github.com/oracle/opengrok/settings/secrets
11+
"""
12+
13+
import logging
14+
import os
15+
import sys
16+
17+
import requests
18+
19+
API_URL = "https://hub.docker.com/v2"
20+
IMAGE = "opengrok/docker"
21+
MAIN_REPO_SLUG = "oracle/opengrok"
22+
23+
24+
def get_token(username, password):
25+
"""
26+
:param username: Docker hub username
27+
:param password: Docker hub password
28+
:return JWT token string from Docker hub API response
29+
"""
30+
31+
logger = logging.getLogger(__name__)
32+
33+
logger.debug("Getting Docker hub token using username/password")
34+
headers = {"Content-Type": "application/json"}
35+
data = {"username": f"{username}", "password": f"{password}"}
36+
response = requests.post(f"{API_URL}/users/login/", headers=headers, json=data)
37+
response.raise_for_status()
38+
39+
return response.json()["token"]
40+
41+
42+
def update_readme(image, readme_file_path, username, password):
43+
"""
44+
Update README file for given image on Docker hub.
45+
:param image: image path (in the form of "namespace_name/repository_name")
46+
:param readme_file_path path to the README file
47+
:param username: Docker hub username
48+
:param password: Docker hub password
49+
"""
50+
51+
logger = logging.getLogger(__name__)
52+
53+
token = get_token(username, password)
54+
headers = {"Content-Type": "application/json", "Authorization": f"JWT {token}"}
55+
with open(readme_file_path, "r") as readme_fp:
56+
readme_data = readme_fp.read()
57+
logger.info("Updating README file on Docker hub")
58+
body_data = {"full_description": readme_data}
59+
response = requests.patch(
60+
f"{API_URL}/repositories/{image}/",
61+
headers=headers,
62+
json=body_data,
63+
)
64+
response.raise_for_status()
65+
66+
67+
def check_push_env():
68+
"""
69+
Check environment variables.
70+
Will exit the program if the environment is not setup for pushing images to Docker hub.
71+
Specifically, number of environment variables is required:
72+
- DOCKER_USERNAME
73+
- DOCKER_PASSWORD
74+
:return Docker hub username and password
75+
"""
76+
77+
logger = logging.getLogger(__name__)
78+
79+
if os.environ.get("OPENGROK_PULL_REQUEST"):
80+
logger.info("Not updating Docker hub README for pull requests")
81+
sys.exit(0)
82+
83+
docker_username = os.environ.get("DOCKER_USERNAME")
84+
if docker_username is None:
85+
logger.info("DOCKER_USERNAME is empty, exiting")
86+
sys.exit(1)
87+
88+
docker_password = os.environ.get("DOCKER_PASSWORD")
89+
if docker_password is None:
90+
logger.info("DOCKER_PASSWORD is empty, exiting")
91+
sys.exit(1)
92+
93+
return docker_username, docker_password
94+
95+
96+
def main():
97+
"""
98+
main program - update Docker hub README and exit.
99+
"""
100+
logging.basicConfig(level=logging.INFO)
101+
logger = logging.getLogger(__name__)
102+
103+
docker_username, docker_password = check_push_env()
104+
try:
105+
update_readme(IMAGE, "docker/README.md", docker_username, docker_password)
106+
except requests.exceptions.HTTPError as exc:
107+
logger.error(exc)
108+
sys.exit(1)
109+
110+
111+
if __name__ == "__main__":
112+
main()

0 commit comments

Comments
 (0)