From 416728d60a2b123d779958bcf9f2951a05e828b2 Mon Sep 17 00:00:00 2001 From: giuseppeM99 Date: Tue, 10 Nov 2020 13:19:07 +0100 Subject: [PATCH 1/4] remove cross compilation * remove cross compilation --- .github/workflows/docker.yml | 179 ++--------------------------------- 1 file changed, 10 insertions(+), 169 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 125e792..3b46acd 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,20 +1,15 @@ -name: Docker multi-arch build and push +name: Docker build and push on: push: - pull_request: - jobs: build: - name: Build Docker image (${{ matrix.arch }}) + name: Build Docker image runs-on: ubuntu-latest env: IMAGE_TAG: ghcr.io/${{ github.repository_owner }}/tdlightbotapi - IMAGE_TAG_DH: ${{ secrets.DOCKERHUB_OWNER }}/tdlightbotapi - strategy: - matrix: - arch: [linux/386, linux/amd64, linux/arm/v6, linux/arm/v7, linux/arm64, linux/ppc64le] + steps: - name: Checkout current repo @@ -29,16 +24,15 @@ jobs: # Strip git ref prefix from version VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') - + # Strip "v" prefix from tag name [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') - + # Use Docker `latest` tag convention [ "$VERSION" == "master" ] && VERSION=latest - + # Convert IMAGE_TAG, HASH_VERSION and VERSION to lowercase (repository name must be lowercase) IMAGE_TAG=$(echo "$IMAGE_TAG" | awk '{print tolower($0)}') - IMAGE_TAG_DH=$(echo "$IMAGE_TAG_DH" | awk '{print tolower($0)}') HASH_VERSION=$(echo "$HASH_VERSION" | awk '{print tolower($0)}') VERSION=$(echo "$VERSION" | awk '{print tolower($0)}') ARCH=${{ matrix.arch }} @@ -46,7 +40,6 @@ jobs: # Store variable for future use echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV - echo "IMAGE_TAG_DH=$IMAGE_TAG_DH" >> $GITHUB_ENV echo "HASH_VERSION=$HASH_VERSION" >> $GITHUB_ENV echo "VERSION=$VERSION" >> $GITHUB_ENV echo "SAFE_ARCH=$SAFE_ARCH" >> $GITHUB_ENV @@ -59,167 +52,15 @@ jobs: # Save env to file cat $GITHUB_ENV > github.env - - name: Upload environment info as artifact - uses: actions/upload-artifact@v2 - with: - name: github_env - path: github.env - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Cache Docker layers - uses: actions/cache@v2 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ env.SAFE_ARCH }}-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-buildx-${{ env.SAFE_ARCH }}- - - name: Login to ghcr registry uses: docker/login-action@v1 with: registry: ghcr.io - username: ${{ secrets.GH_USERNAME }} + username: ${{ github.actor }} password: ${{ secrets.GH_ACCESS_TOKEN }} - - name: Login to Docker Hub registry - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} - - name: Build image - uses: docker/build-push-action@v2 - with: - context: . - file: ./Dockerfile - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,mode=max,dest=/tmp/.buildx-cache - platforms: ${{ matrix.arch }} - push: false - load: true - tags: | - tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} - - - name: Tag and push image + - name: Build and push image run: | - docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} - docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} - docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} - docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} - docker push ${{ env.IMAGE_TAG}}:${{ env.HASH_VERSION}}-${{ env.SAFE_ARCH }} - docker push ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} - docker push ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} - docker push ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} - - - name: Save image as tar archive - run: | - docker image ls # debug - docker save ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} -o ${{ env.SAFE_ARCH }}.tar - - - name: Upload image as artifact - uses: actions/upload-artifact@v2 - with: - name: image_${{ env.SAFE_ARCH }} - path: ${{ env.SAFE_ARCH }}.tar - - push-manifest: - name: Create and push multi-arch Docker manifest - runs-on: ubuntu-latest - env: - DOCKER_CLI_EXPERIMENTAL: enabled - needs: build - - steps: - - name: Download artifacts - uses: actions/download-artifact@v2 - - - name: Load environment info and built images - run: | - cat github_env/github.env > $GITHUB_ENV - docker load --input image_linux386/linux386.tar - docker load --input image_linuxamd64/linuxamd64.tar - docker load --input image_linuxarmv6/linuxarmv6.tar - docker load --input image_linuxarmv7/linuxarmv7.tar - docker load --input image_linuxarm64/linuxarm64.tar - docker load --input image_linuxppc64le/linuxppc64le.tar - - - name: Login to ghcr registry - uses: docker/login-action@v1 - with: - registry: ghcr.io - username: ${{ secrets.GH_USERNAME }} - password: ${{ secrets.GH_ACCESS_TOKEN }} - - - name: Login to Docker Hub registry - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} - - - name: Create and push manifest - run: | - # -- Push to ghcr.io - docker manifest create ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} \ - --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le - docker manifest push ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} - - # Tag images as VERSION (like 'latest') - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linux386 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxamd64 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv6 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv7 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarm64 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxppc64le - - docker manifest create ${{ env.IMAGE_TAG }}:${{ env.VERSION }} \ - --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linux386 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxamd64 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv6 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv7 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarm64 \ - --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxppc64le - docker manifest push ${{ env.IMAGE_TAG }}:${{ env.VERSION }} - - # -- Push to Docker Hub - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linux386 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxamd64 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv6 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv7 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarm64 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxppc64le - - docker manifest create ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }} \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linux386 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxamd64 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxarmv6 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxarmv7 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxarm64 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxppc64le - docker manifest push ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }} - - # Tag images as VERSION (like 'latest') - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linux386 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxamd64 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv6 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv7 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarm64 - docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxppc64le - - docker manifest create ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }} \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linux386 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxamd64 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv6 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv7 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarm64 \ - --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxppc64le - docker manifest push ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }} + docker build . -t ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} + docker push ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} From a5a0c49467913b03d4bd435f0fc98460c7c02438 Mon Sep 17 00:00:00 2001 From: Giuseppe Marino Date: Tue, 10 Nov 2020 16:14:44 +0100 Subject: [PATCH 2/4] Custom methods * Added custom methods - getMessageInfo - getParticipants - deleteMessages - toggleGroupInvite * Implemented method getMessageInfo * added `views` field and `forwards` field to message * getChat now resolves the username also for users * Added custom executable flags - relative : use relative path for files in local mode - insecure (not implemented) : allow http connection in non-local mode --- telegram-bot-api/Client.cpp | 55 ++++++++++++++++++++++++++- telegram-bot-api/Client.h | 10 +++++ telegram-bot-api/ClientParameters.h | 2 + telegram-bot-api/telegram-bot-api.cpp | 3 ++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index b6e3bca..3bb4011 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -250,6 +250,14 @@ bool Client::init_methods() { methods_.emplace("deletewebhook", &Client::process_set_webhook_query); methods_.emplace("getwebhookinfo", &Client::process_get_webhook_info_query); methods_.emplace("getfile", &Client::process_get_file_query); + + //custom methods + methods_.emplace("getmessageinfo", &Client::process_get_message_info_query); + methods_.emplace("getparticipants", &Client::process_get_participants_query); + methods_.emplace("deletemessages", &Client::process_delete_messages_query); + methods_.emplace("togglegroupinvites", &Client::process_toggle_group_invites_query); + + return true; } @@ -1479,6 +1487,12 @@ void Client::JsonMessage::store(JsonValueScope *scope) const { if (message_->edit_date > 0) { object("edit_date", message_->edit_date); } + if (message_->views != 0) { + object("views", message_->views); + } + if (message_->forwards != 0) { + object("forwards", message_->forwards); + } if (message_->initial_send_date > 0) { if (message_->initial_sender_user_id != 0) { object("forward_from", JsonUser(message_->initial_sender_user_id, client_)); @@ -3538,7 +3552,7 @@ void Client::check_chat(Slice chat_id_str, AccessRights access_rights, PromisedQ if (chat_id_str[0] == '@') { return send_request(make_object(chat_id_str.str()), - std::make_unique>(this, true, access_rights, std::move(query), + std::make_unique>(this, false, access_rights, std::move(query), std::move(on_success))); } @@ -7360,6 +7374,38 @@ td::Status Client::process_get_file_query(PromisedQueryPtr &query) { return Status::OK(); } + +//start custom methods impl + +td::Status Client::process_get_message_info_query(PromisedQueryPtr &query) { + auto chat_id = query->arg("chat_id"); + auto message_id = get_message_id(query.get(), "message_id"); + check_message(chat_id, message_id, false, AccessRights::Read, "message", std::move(query),[this] (int64 chat_id, int64 message_id, PromisedQueryPtr query) { + auto message = get_message(chat_id, message_id); + answer_query(JsonMessage(message, false, "get message info", this), std::move(query)); + }); + + return Status::OK(); +} + +td::Status Client::process_get_participants_query(PromisedQueryPtr &query) { + answer_query(td::JsonFalse(), std::move(query), "Not implemented"); + return Status::OK(); +} + +td::Status Client::process_delete_messages_query(PromisedQueryPtr &query) { + answer_query(td::JsonFalse(), std::move(query), "Not implemented"); + return Status::OK(); + +} + +td::Status Client::process_toggle_group_invites_query(PromisedQueryPtr &query) { + answer_query(td::JsonFalse(), std::move(query), "Not implemented"); + return Status::OK(); +} + +//end custom methods impl + void Client::do_get_file(object_ptr file, PromisedQueryPtr query) { if (!parameters_->local_mode_ && td::max(file->expected_size_, file->local_->downloaded_size_) > MAX_DOWNLOAD_FILE_SIZE) { // speculative check @@ -7994,7 +8040,7 @@ void Client::json_store_file(td::JsonObjectScope &object, const td_api::file *fi object("file_size", file->size_); } if (with_path && file->local_->is_downloading_completed_) { - if (parameters_->local_mode_) { + if (parameters_->local_mode_ && !parameters_->use_relative_path_) { if (td::check_utf8(file->local_->path_)) { object("file_path", file->local_->path_); } else { @@ -8922,6 +8968,11 @@ Client::FullMessageId Client::add_message(object_ptr &&message, } } + if (message->interaction_info_ != nullptr) { + message_info->views = message->interaction_info_->view_count_; + message_info->forwards = message->interaction_info_->forward_count_; + } + message_info->author_signature = std::move(message->author_signature_); if (message->reply_in_chat_id_ != chat_id && message->reply_to_message_id_ != 0) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 22716bc..cc6a7a9 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -487,6 +487,13 @@ class Client : public WebhookActor::Callback { Status process_get_webhook_info_query(PromisedQueryPtr &query); Status process_get_file_query(PromisedQueryPtr &query); + //custom methods + Status process_get_message_info_query(PromisedQueryPtr &query); + Status process_get_participants_query(PromisedQueryPtr &query); + Status process_delete_messages_query(PromisedQueryPtr &query); + Status process_toggle_group_invites_query(PromisedQueryPtr &query); + + void webhook_verified(td::string cached_ip_address) override; void webhook_success() override; void webhook_error(Status status) override; @@ -643,6 +650,9 @@ class Client : public WebhookActor::Callback { object_ptr content; object_ptr reply_markup; + int32 views = 0; + int32 forwards = 0; + mutable bool is_reply_to_message_deleted = false; mutable bool is_content_changed = false; }; diff --git a/telegram-bot-api/ClientParameters.h b/telegram-bot-api/ClientParameters.h index 018fca6..e08fbff 100644 --- a/telegram-bot-api/ClientParameters.h +++ b/telegram-bot-api/ClientParameters.h @@ -55,6 +55,8 @@ struct SharedData { struct ClientParameters { bool local_mode_ = false; + bool allow_http_ = false; + bool use_relative_path_ = false; td::int32 api_id_ = 0; td::string api_hash_; diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 3502e21..0fc0d7d 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -168,6 +168,9 @@ int main(int argc, char *argv[]) { options.add_option('h', "help", "display this help text and exit", [&] { need_print_usage = true; }); options.add_option('\0', "local", "allow the Bot API server to serve local requests", [&] { parameters->local_mode_ = true; }); + options.add_option('\0', "insecure", "allow the Bot API to send request via insecure HTTP", [&] { parameters->allow_http_ = true; }); + options.add_option('\0', "relative", "use relative file path in local mode", [&] { parameters->use_relative_path_ = true; }); + options.add_checked_option( '\0', "api-id", "application identifier for Telegram API access, which can be obtained at https://my.telegram.org (defaults to " From e6828cdf7a335c2e20ca3f864ddc0d1e98beac40 Mon Sep 17 00:00:00 2001 From: giuseppeM99 Date: Tue, 10 Nov 2020 16:23:40 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1764cdf..26eb550 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,17 @@ TDLight Telegram Bot API is 100% compatible with the official version. ### Command `optimize_memory` Calling `optimize_memory` will remove old data from the in-memory cache and give the freed memory back to the os +### Additional features +- getChat now resolves username for users +- the `message` object now has the `views` field and `forwards` field +- new method `getMessageInfo` to get a message (params: chat_id, message_id) + ----- The Telegram Bot API provides an HTTP API for creating [Telegram Bots](https://core.telegram.org/bots). -If you've got any questions about bots or would like to report an issue with your bot, kindly contact us at [@BotSupport](https://t.me/BotSupport) in Telegram. +If you've got any questions about bots or would like to report an issue with your bot, kindly contact [@BotSupport](https://t.me/BotSupport) in Telegram. Please note that only global Bot API issues that affect all bots are suitable for this repository. From 8f33a9f1c8298887522c33efa648f89644c1c236 Mon Sep 17 00:00:00 2001 From: giuseppeM99 Date: Tue, 10 Nov 2020 16:26:06 +0100 Subject: [PATCH 4/4] Revert "remove cross compilation" This reverts commit 416728d60a2b123d779958bcf9f2951a05e828b2. --- .github/workflows/docker.yml | 179 +++++++++++++++++++++++++++++++++-- 1 file changed, 169 insertions(+), 10 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 3b46acd..125e792 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,15 +1,20 @@ -name: Docker build and push +name: Docker multi-arch build and push on: push: + pull_request: + jobs: build: - name: Build Docker image + name: Build Docker image (${{ matrix.arch }}) runs-on: ubuntu-latest env: IMAGE_TAG: ghcr.io/${{ github.repository_owner }}/tdlightbotapi - + IMAGE_TAG_DH: ${{ secrets.DOCKERHUB_OWNER }}/tdlightbotapi + strategy: + matrix: + arch: [linux/386, linux/amd64, linux/arm/v6, linux/arm/v7, linux/arm64, linux/ppc64le] steps: - name: Checkout current repo @@ -24,15 +29,16 @@ jobs: # Strip git ref prefix from version VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') - + # Strip "v" prefix from tag name [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') - + # Use Docker `latest` tag convention [ "$VERSION" == "master" ] && VERSION=latest - + # Convert IMAGE_TAG, HASH_VERSION and VERSION to lowercase (repository name must be lowercase) IMAGE_TAG=$(echo "$IMAGE_TAG" | awk '{print tolower($0)}') + IMAGE_TAG_DH=$(echo "$IMAGE_TAG_DH" | awk '{print tolower($0)}') HASH_VERSION=$(echo "$HASH_VERSION" | awk '{print tolower($0)}') VERSION=$(echo "$VERSION" | awk '{print tolower($0)}') ARCH=${{ matrix.arch }} @@ -40,6 +46,7 @@ jobs: # Store variable for future use echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV + echo "IMAGE_TAG_DH=$IMAGE_TAG_DH" >> $GITHUB_ENV echo "HASH_VERSION=$HASH_VERSION" >> $GITHUB_ENV echo "VERSION=$VERSION" >> $GITHUB_ENV echo "SAFE_ARCH=$SAFE_ARCH" >> $GITHUB_ENV @@ -52,15 +59,167 @@ jobs: # Save env to file cat $GITHUB_ENV > github.env + - name: Upload environment info as artifact + uses: actions/upload-artifact@v2 + with: + name: github_env + path: github.env + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ env.SAFE_ARCH }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-${{ env.SAFE_ARCH }}- + - name: Login to ghcr registry uses: docker/login-action@v1 with: registry: ghcr.io - username: ${{ github.actor }} + username: ${{ secrets.GH_USERNAME }} password: ${{ secrets.GH_ACCESS_TOKEN }} + - name: Login to Docker Hub registry + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} - - name: Build and push image + - name: Build image + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache + platforms: ${{ matrix.arch }} + push: false + load: true + tags: | + tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} + + - name: Tag and push image run: | - docker build . -t ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} - docker push ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} + docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} + docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} + docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} + docker tag tdlightbotapi:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} + docker push ${{ env.IMAGE_TAG}}:${{ env.HASH_VERSION}}-${{ env.SAFE_ARCH }} + docker push ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} + docker push ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} + docker push ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-${{ env.SAFE_ARCH }} + + - name: Save image as tar archive + run: | + docker image ls # debug + docker save ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-${{ env.SAFE_ARCH }} -o ${{ env.SAFE_ARCH }}.tar + + - name: Upload image as artifact + uses: actions/upload-artifact@v2 + with: + name: image_${{ env.SAFE_ARCH }} + path: ${{ env.SAFE_ARCH }}.tar + + push-manifest: + name: Create and push multi-arch Docker manifest + runs-on: ubuntu-latest + env: + DOCKER_CLI_EXPERIMENTAL: enabled + needs: build + + steps: + - name: Download artifacts + uses: actions/download-artifact@v2 + + - name: Load environment info and built images + run: | + cat github_env/github.env > $GITHUB_ENV + docker load --input image_linux386/linux386.tar + docker load --input image_linuxamd64/linuxamd64.tar + docker load --input image_linuxarmv6/linuxarmv6.tar + docker load --input image_linuxarmv7/linuxarmv7.tar + docker load --input image_linuxarm64/linuxarm64.tar + docker load --input image_linuxppc64le/linuxppc64le.tar + + - name: Login to ghcr registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ secrets.GH_USERNAME }} + password: ${{ secrets.GH_ACCESS_TOKEN }} + + - name: Login to Docker Hub registry + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} + + - name: Create and push manifest + run: | + # -- Push to ghcr.io + docker manifest create ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} \ + --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le + docker manifest push ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} + + # Tag images as VERSION (like 'latest') + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linux386 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxamd64 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv6 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv7 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarm64 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxppc64le + + docker manifest create ${{ env.IMAGE_TAG }}:${{ env.VERSION }} \ + --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linux386 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxamd64 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv6 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarmv7 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxarm64 \ + --amend ${{ env.IMAGE_TAG }}:${{ env.VERSION }}-linuxppc64le + docker manifest push ${{ env.IMAGE_TAG }}:${{ env.VERSION }} + + # -- Push to Docker Hub + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linux386 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxamd64 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv6 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv7 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarm64 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxppc64le + + docker manifest create ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }} \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linux386 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxamd64 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxarmv6 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxarmv7 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxarm64 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }}-linuxppc64le + docker manifest push ${{ env.IMAGE_TAG_DH }}:${{ env.HASH_VERSION }} + + # Tag images as VERSION (like 'latest') + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linux386 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linux386 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxamd64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxamd64 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv6 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv6 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarmv7 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv7 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxarm64 ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarm64 + docker tag ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}-linuxppc64le ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxppc64le + + docker manifest create ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }} \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linux386 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxamd64 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv6 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarmv7 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxarm64 \ + --amend ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}-linuxppc64le + docker manifest push ${{ env.IMAGE_TAG_DH }}:${{ env.VERSION }}