Skip to content

Commit 7840711

Browse files
Added script to create the PR to the official-images repo
Co-authored-by: Hank Brekke <[email protected]> Co-authored-by: Laurent Goderre <[email protected]>
1 parent a13ad42 commit 7840711

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ stages:
1818
- Test
1919
- Build
2020
- name: Deploy
21-
if: branch = master
21+
if: branch = master AND type IN (push)
2222

2323
jobs:
2424
include:
@@ -44,7 +44,7 @@ jobs:
4444
- shellcheck
4545

4646
- stage: Deploy
47-
script: ./generate-stackbrew-library.sh
47+
script: ./generate-stackbrew-pr.sh
4848

4949
# Docker Build #
5050
- stage: Build

generate-stackbrew-pr.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/bin/bash
2+
set -e
3+
. functions.sh
4+
5+
GITHUB_USERNAME="nodejs-github-bot"
6+
gitpath="../docker-images"
7+
IMAGES_FILE="library/node"
8+
REPO_NAME="official-images"
9+
BRANCH_NAME="travis-$TRAVIS_BUILD_ID"
10+
ORIGIN_SLUG="$GITHUB_USERNAME/$REPO_NAME"
11+
UPSTREAM_SLUG="docker-library/$REPO_NAME"
12+
13+
function updated() {
14+
local versions
15+
local images_changed
16+
17+
IFS=' ' read -ra versions <<< "$(IFS=','; get_versions)"
18+
images_changed=$(git show --name-only "$TRAVIS_COMMIT" "${versions[@]}")
19+
20+
if [ -z "$images_changed" ]; then
21+
return 1
22+
else
23+
return 0
24+
fi
25+
}
26+
27+
function permission_check() {
28+
auth="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \
29+
-s \
30+
"https://api.github.com")"
31+
if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then
32+
fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN"
33+
fi
34+
35+
auth="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \
36+
-s \
37+
"https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")"
38+
if [ "$(echo "$auth" | jq .message)" != "null" ]; then
39+
fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git"
40+
fi
41+
}
42+
43+
function setup_git_author() {
44+
GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$TRAVIS_COMMIT")"
45+
GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$TRAVIS_COMMIT")"
46+
GIT_COMMITTER_NAME="$(git show -s --format="%cN" "$TRAVIS_COMMIT")"
47+
GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "$TRAVIS_COMMIT")"
48+
49+
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
50+
}
51+
52+
function message() {
53+
echo "Node: $TRAVIS_COMMIT_MESSAGE"
54+
}
55+
56+
function pr_payload() {
57+
local escaped_message
58+
IFS=' ' read -ra escaped_message <<< "$TRAVIS_COMMIT_MESSAGE"
59+
escaped_message="$(printf '%q ' "${escaped_message[@]}")"
60+
echo '{
61+
"title": "Node: '"$escaped_message"'",
62+
"body": "Commit: nodejs/docker-node@'"$TRAVIS_COMMIT"'",
63+
"head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'",
64+
"base": "master"
65+
}'
66+
}
67+
68+
if updated; then
69+
70+
permission_check
71+
72+
# Set Git User Info
73+
setup_git_author
74+
75+
info "Cloning..."
76+
git clone --depth 50 https://github.com/docker-library/official-images.git $gitpath 2> /dev/null
77+
78+
./generate-stackbrew-library.sh > "$gitpath/$IMAGES_FILE"
79+
80+
cd $gitpath
81+
82+
git checkout -b "$BRANCH_NAME"
83+
git add "$IMAGES_FILE"
84+
git commit -m "$(message)"
85+
86+
info "Pushing..."
87+
git push "https://$GITHUB_API_TOKEN:[email protected]/$ORIGIN_SLUG.git" -f "$BRANCH_NAME" 2> /dev/null || fatal "Error pushing the updated stackbrew"
88+
89+
info "Creating Pull request"
90+
response_payload="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \
91+
-s \
92+
-X POST \
93+
-d "$(pr_payload)" \
94+
"https://api.github.com/repos/$UPSTREAM_SLUG/pulls")"
95+
96+
url="$(echo "$response_payload" | jq .html_url)"
97+
if [ "$url" != "null" ]; then
98+
info "Pull request created at $url"
99+
else
100+
error_message=$(echo "$response_payload" | jq .message)
101+
fatal "Error creating pull request ($error_message)"
102+
fi
103+
else
104+
info "No change!"
105+
fi

travis.yml.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ stages:
1616
- Test
1717
- Build
1818
- name: Deploy
19-
if: branch = master
19+
if: branch = master AND type IN (push)
2020

2121
jobs:
2222
include:
@@ -42,6 +42,6 @@ jobs:
4242
- shellcheck
4343

4444
- stage: Deploy
45-
script: ./generate-stackbrew-library.sh
45+
script: ./generate-stackbrew-pr.sh
4646

4747
# Docker Build #

0 commit comments

Comments
 (0)