Skip to content

Commit 1e5219b

Browse files
Merge pull request #672 from LaurentGoderre/automate-deploy
Added a deploy stage to automate the deployment to the official images repo
2 parents e90556f + 11f3cab commit 1e5219b

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ addons:
1414

1515
script: ./test-build.sh $NODE_VERSION $VARIANT
1616

17+
stages:
18+
- Test
19+
- Build
20+
- name: Deploy
21+
if: branch = build-test AND type IN (push)
22+
1723
jobs:
1824
include:
1925
- stage: Test
@@ -37,6 +43,11 @@ jobs:
3743
packages:
3844
- shellcheck
3945

46+
- stage: Deploy
47+
script: ./generate-stackbrew-pr.sh
48+
env:
49+
secure: "HkgYgawkr/hkg2vURHGOb/JmF6U1e71QQsd0HXJ1UJh6WBiFJOrjomCMHZGHnpyopdRna++up8dISBqM2X+EiLAr9yWdRXy72oMP9X42M0ccnja/3E5KYYXMkETS50JnUhMCOt2xRZW5/ojqrHukqRo0YIjIu+P0U0VsZ4QIsng="
50+
4051
# Docker Build #
4152
- stage: Build
4253
env:

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ addons:
1212

1313
script: ./test-build.sh $NODE_VERSION $VARIANT
1414

15+
stages:
16+
- Test
17+
- Build
18+
- name: Deploy
19+
if: branch = build-test AND type IN (push)
20+
1521
jobs:
1622
include:
1723
- stage: Test
@@ -35,4 +41,9 @@ jobs:
3541
packages:
3642
- shellcheck
3743

44+
- stage: Deploy
45+
script: ./generate-stackbrew-pr.sh
46+
env:
47+
secure: "HkgYgawkr/hkg2vURHGOb/JmF6U1e71QQsd0HXJ1UJh6WBiFJOrjomCMHZGHnpyopdRna++up8dISBqM2X+EiLAr9yWdRXy72oMP9X42M0ccnja/3E5KYYXMkETS50JnUhMCOt2xRZW5/ojqrHukqRo0YIjIu+P0U0VsZ4QIsng="
48+
3849
# Docker Build #

0 commit comments

Comments
 (0)