From a13ad42828553776ed1a4de2fae45187cd2a1b29 Mon Sep 17 00:00:00 2001 From: Laurent Goderre Date: Tue, 27 Mar 2018 10:27:13 -0400 Subject: [PATCH 1/3] Added a deploy stage to automate the deployment to the official images repo --- .travis.yml | 9 +++++++++ travis.yml.template | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/.travis.yml b/.travis.yml index 5dc73074..3a733cf4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,12 @@ addons: script: ./test-build.sh $NODE_VERSION $VARIANT +stages: + - Test + - Build + - name: Deploy + if: branch = master + jobs: include: - stage: Test @@ -37,6 +43,9 @@ jobs: packages: - shellcheck + - stage: Deploy + script: ./generate-stackbrew-library.sh + # Docker Build # - stage: Build env: diff --git a/travis.yml.template b/travis.yml.template index 4aecb7a5..394fce79 100644 --- a/travis.yml.template +++ b/travis.yml.template @@ -12,6 +12,12 @@ addons: script: ./test-build.sh $NODE_VERSION $VARIANT +stages: + - Test + - Build + - name: Deploy + if: branch = master + jobs: include: - stage: Test @@ -35,4 +41,7 @@ jobs: packages: - shellcheck + - stage: Deploy + script: ./generate-stackbrew-library.sh + # Docker Build # From 78407116065b2be42d88a8d2d853305f4c6807e9 Mon Sep 17 00:00:00 2001 From: Laurent Goderre Date: Tue, 27 Mar 2018 12:27:15 -0400 Subject: [PATCH 2/3] Added script to create the PR to the official-images repo Co-authored-by: Hank Brekke Co-authored-by: Laurent Goderre --- .travis.yml | 4 +- generate-stackbrew-pr.sh | 105 +++++++++++++++++++++++++++++++++++++++ travis.yml.template | 4 +- 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100755 generate-stackbrew-pr.sh diff --git a/.travis.yml b/.travis.yml index 3a733cf4..5779ea35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ stages: - Test - Build - name: Deploy - if: branch = master + if: branch = master AND type IN (push) jobs: include: @@ -44,7 +44,7 @@ jobs: - shellcheck - stage: Deploy - script: ./generate-stackbrew-library.sh + script: ./generate-stackbrew-pr.sh # Docker Build # - stage: Build diff --git a/generate-stackbrew-pr.sh b/generate-stackbrew-pr.sh new file mode 100755 index 00000000..d7c55abf --- /dev/null +++ b/generate-stackbrew-pr.sh @@ -0,0 +1,105 @@ +#!/bin/bash +set -e +. functions.sh + +GITHUB_USERNAME="nodejs-github-bot" +gitpath="../docker-images" +IMAGES_FILE="library/node" +REPO_NAME="official-images" +BRANCH_NAME="travis-$TRAVIS_BUILD_ID" +ORIGIN_SLUG="$GITHUB_USERNAME/$REPO_NAME" +UPSTREAM_SLUG="docker-library/$REPO_NAME" + +function updated() { + local versions + local images_changed + + IFS=' ' read -ra versions <<< "$(IFS=','; get_versions)" + images_changed=$(git show --name-only "$TRAVIS_COMMIT" "${versions[@]}") + + if [ -z "$images_changed" ]; then + return 1 + else + return 0 + fi +} + +function permission_check() { + auth="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \ + -s \ + "https://api.github.com")" + if [ "$(echo "$auth" | jq .message)" = "\"Bad credentials\"" ]; then + fatal "Authentication Failed! Invalid \$GITHUB_API_TOKEN" + fi + + auth="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \ + -s \ + "https://api.github.com/repos/$ORIGIN_SLUG/collaborators/$GITHUB_USERNAME/permission")" + if [ "$(echo "$auth" | jq .message)" != "null" ]; then + fatal "\$GITHUB_API_TOKEN can't push to https://github.com/$ORIGIN_SLUG.git" + fi +} + +function setup_git_author() { + GIT_AUTHOR_NAME="$(git show -s --format="%aN" "$TRAVIS_COMMIT")" + GIT_AUTHOR_EMAIL="$(git show -s --format="%aE" "$TRAVIS_COMMIT")" + GIT_COMMITTER_NAME="$(git show -s --format="%cN" "$TRAVIS_COMMIT")" + GIT_COMMITTER_EMAIL="$(git show -s --format="%cN" "$TRAVIS_COMMIT")" + + export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL +} + +function message() { + echo "Node: $TRAVIS_COMMIT_MESSAGE" +} + +function pr_payload() { + local escaped_message + IFS=' ' read -ra escaped_message <<< "$TRAVIS_COMMIT_MESSAGE" + escaped_message="$(printf '%q ' "${escaped_message[@]}")" + echo '{ + "title": "Node: '"$escaped_message"'", + "body": "Commit: nodejs/docker-node@'"$TRAVIS_COMMIT"'", + "head": "'"$GITHUB_USERNAME"':'"$BRANCH_NAME"'", + "base": "master" + }' +} + +if updated; then + + permission_check + + # Set Git User Info + setup_git_author + + info "Cloning..." + git clone --depth 50 https://github.com/docker-library/official-images.git $gitpath 2> /dev/null + + ./generate-stackbrew-library.sh > "$gitpath/$IMAGES_FILE" + + cd $gitpath + + git checkout -b "$BRANCH_NAME" + git add "$IMAGES_FILE" + git commit -m "$(message)" + + info "Pushing..." + git push "https://$GITHUB_API_TOKEN:x-oauth-basic@github.com/$ORIGIN_SLUG.git" -f "$BRANCH_NAME" 2> /dev/null || fatal "Error pushing the updated stackbrew" + + info "Creating Pull request" + response_payload="$(curl -H "Authorization: token $GITHUB_API_TOKEN" \ + -s \ + -X POST \ + -d "$(pr_payload)" \ + "https://api.github.com/repos/$UPSTREAM_SLUG/pulls")" + + url="$(echo "$response_payload" | jq .html_url)" + if [ "$url" != "null" ]; then + info "Pull request created at $url" + else + error_message=$(echo "$response_payload" | jq .message) + fatal "Error creating pull request ($error_message)" + fi +else + info "No change!" +fi diff --git a/travis.yml.template b/travis.yml.template index 394fce79..80686296 100644 --- a/travis.yml.template +++ b/travis.yml.template @@ -16,7 +16,7 @@ stages: - Test - Build - name: Deploy - if: branch = master + if: branch = master AND type IN (push) jobs: include: @@ -42,6 +42,6 @@ jobs: - shellcheck - stage: Deploy - script: ./generate-stackbrew-library.sh + script: ./generate-stackbrew-pr.sh # Docker Build # From 11f3cab3a3804720b56394d6bd7434a3255b32de Mon Sep 17 00:00:00 2001 From: Laurent Goderre Date: Wed, 4 Apr 2018 08:17:31 -0400 Subject: [PATCH 3/3] Added the secured variable --- .travis.yml | 4 +++- travis.yml.template | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5779ea35..6cff24bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ stages: - Test - Build - name: Deploy - if: branch = master AND type IN (push) + if: branch = build-test AND type IN (push) jobs: include: @@ -45,6 +45,8 @@ jobs: - stage: Deploy script: ./generate-stackbrew-pr.sh + env: + secure: "HkgYgawkr/hkg2vURHGOb/JmF6U1e71QQsd0HXJ1UJh6WBiFJOrjomCMHZGHnpyopdRna++up8dISBqM2X+EiLAr9yWdRXy72oMP9X42M0ccnja/3E5KYYXMkETS50JnUhMCOt2xRZW5/ojqrHukqRo0YIjIu+P0U0VsZ4QIsng=" # Docker Build # - stage: Build diff --git a/travis.yml.template b/travis.yml.template index 80686296..4b0447af 100644 --- a/travis.yml.template +++ b/travis.yml.template @@ -16,7 +16,7 @@ stages: - Test - Build - name: Deploy - if: branch = master AND type IN (push) + if: branch = build-test AND type IN (push) jobs: include: @@ -43,5 +43,7 @@ jobs: - stage: Deploy script: ./generate-stackbrew-pr.sh + env: + secure: "HkgYgawkr/hkg2vURHGOb/JmF6U1e71QQsd0HXJ1UJh6WBiFJOrjomCMHZGHnpyopdRna++up8dISBqM2X+EiLAr9yWdRXy72oMP9X42M0ccnja/3E5KYYXMkETS50JnUhMCOt2xRZW5/ojqrHukqRo0YIjIu+P0U0VsZ4QIsng=" # Docker Build #