From f8a48541055bf7e5f929479a4b69ba89054ee908 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Mon, 8 Jul 2024 08:48:10 +0700 Subject: [PATCH 01/10] CI: Add github actions to build and push container image to dockerhub --- .github/workflows/main.yml | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..e1dbe86 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,89 @@ +name: CI + +on: + push: + branches: + - main + - dev + tags: + - '*' + +jobs: + build_lint_test: + runs-on: ubuntu-22.04 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: 1.22.3 + + - name: Print Go environment + run: go env + + - name: Cache Go modules + id: go-cache + uses: actions/cache@v4 + with: + path: | + ~/.go/pkg/mod + ~/.cache/go-build + key: go-mod-${{ hashFiles('**/go.sum') }} + restore-keys: | + go-mod- + + - name: Build Staking API Service + run: make build + + - name: Run tests + run: make test + + build_docker: + runs-on: ubuntu-22.04 + needs: build_lint_test + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build Docker image + uses: docker/build-push-action@v6 + with: + tags: staking-api-service:${{ github.sha }} + outputs: type=docker,dest=/tmp/staking-api-service.tar + context: ./ + file: ./contrib/images/staking-api-service + + - name: Upload Docker image to workspace + uses: actions/upload-artifact@v4 + with: + name: staking-api-service + path: /tmp/staking-api-service.tar + + push_docker: + runs-on: ubuntu-22.04 + needs: build_docker + steps: + - name: Download Docker image from workspace + uses: actions/download-artifact@v4 + with: + name: staking-api-service + path: /tmp/ + + - name: Load Docker image + run: docker load -i /tmp/staking-api-service.tar + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Push Docker image + run: | + docker tag staking-api-service:${{ github.sha }} ${{ secrets.DOCKERHUB_REGISTRY_ID }}/staking-api-service:${{ github.sha }} + docker push ${{ secrets.DOCKERHUB_REGISTRY_ID }}/staking-api-service:${{ github.sha }} From 41f88ca2985d96d8ff86b177e4dbbd3ab35e0b60 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Mon, 8 Jul 2024 08:53:04 +0700 Subject: [PATCH 02/10] make tests --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e1dbe86..486181a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,7 +38,7 @@ jobs: run: make build - name: Run tests - run: make test + run: make tests build_docker: runs-on: ubuntu-22.04 From 0252e90cedab9865537c7fdce1f2fe0828bfafe8 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Fri, 12 Jul 2024 15:00:34 +0700 Subject: [PATCH 03/10] feat: Add github actions --- .github/workflows/main.yml | 89 -------------------------------------- 1 file changed, 89 deletions(-) delete mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 486181a..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,89 +0,0 @@ -name: CI - -on: - push: - branches: - - main - - dev - tags: - - '*' - -jobs: - build_lint_test: - runs-on: ubuntu-22.04 - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version: 1.22.3 - - - name: Print Go environment - run: go env - - - name: Cache Go modules - id: go-cache - uses: actions/cache@v4 - with: - path: | - ~/.go/pkg/mod - ~/.cache/go-build - key: go-mod-${{ hashFiles('**/go.sum') }} - restore-keys: | - go-mod- - - - name: Build Staking API Service - run: make build - - - name: Run tests - run: make tests - - build_docker: - runs-on: ubuntu-22.04 - needs: build_lint_test - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Build Docker image - uses: docker/build-push-action@v6 - with: - tags: staking-api-service:${{ github.sha }} - outputs: type=docker,dest=/tmp/staking-api-service.tar - context: ./ - file: ./contrib/images/staking-api-service - - - name: Upload Docker image to workspace - uses: actions/upload-artifact@v4 - with: - name: staking-api-service - path: /tmp/staking-api-service.tar - - push_docker: - runs-on: ubuntu-22.04 - needs: build_docker - steps: - - name: Download Docker image from workspace - uses: actions/download-artifact@v4 - with: - name: staking-api-service - path: /tmp/ - - - name: Load Docker image - run: docker load -i /tmp/staking-api-service.tar - - - name: Login to Docker Hub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Push Docker image - run: | - docker tag staking-api-service:${{ github.sha }} ${{ secrets.DOCKERHUB_REGISTRY_ID }}/staking-api-service:${{ github.sha }} - docker push ${{ secrets.DOCKERHUB_REGISTRY_ID }}/staking-api-service:${{ github.sha }} From c4ef0a09c7affdb8e098ef1d695099c060982bd7 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Fri, 12 Jul 2024 15:02:13 +0700 Subject: [PATCH 04/10] add ci workflow --- .github/workflows/ci.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f5c47e4 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,20 @@ +name: ci + +on: + pull_request: + branches: + - '**' + +jobs: + lint_test: + uses: babylonchain/.github/.github/workflows/reusable_go_lint_test.yml@hiep/add-reusable-workflows + with: + run-unit-tests: true + run-integration-tests: false + run-lint: false + + docker_pipeline: + uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@hiep/add-reusable-workflows + secrets: inherit + with: + publish: false \ No newline at end of file From e5f48b9116ec348159174dd709d725c353be8a1e Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Fri, 12 Jul 2024 15:03:50 +0700 Subject: [PATCH 05/10] Make the lint_test workflow works --- Makefile | 2 +- bin/local-startup.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8108483..95d02aa 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,7 @@ run-unprocessed-events-replay-local: generate-mock-interface: cd internal/db && mockery --name=DBClient --output=../../tests/mocks --outpkg=dbmock --filename=mock_db_client.go -tests: +test: ./bin/local-startup.sh; go test -v -cover -p 1 ./... -count=1 diff --git a/bin/local-startup.sh b/bin/local-startup.sh index 5e49df8..aa14de2 100755 --- a/bin/local-startup.sh +++ b/bin/local-startup.sh @@ -7,7 +7,7 @@ if [ $(docker ps -q -f name=^/${MONGO_CONTAINER_NAME}$) ]; then else echo "Starting MongoDB" # Start MongoDB - docker-compose up mongodb -d + docker-compose up -d mongodb fi # Check if the RabbitMQ container is already running @@ -17,5 +17,5 @@ if [ $(docker ps -q -f name=^/${RABBITMQ_CONTAINER_NAME}$) ]; then else echo "Starting RabbitMQ" # Start RabbitMQ - docker-compose up rabbitmq -d + docker-compose up -d rabbitmq fi \ No newline at end of file From 65ff9befa3d0df80940c4cf7288cb96bb99f975f Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Fri, 12 Jul 2024 15:07:27 +0700 Subject: [PATCH 06/10] Update dockerfile dir --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5c47e4..7f88677 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,4 +17,5 @@ jobs: uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@hiep/add-reusable-workflows secrets: inherit with: - publish: false \ No newline at end of file + publish: false + dockerfile: ./contrib/images/staking-api-service/Dockerfile \ No newline at end of file From c2455086c7a958d921d9c4f42d3fc1f01379ccc9 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Mon, 15 Jul 2024 19:12:08 +0700 Subject: [PATCH 07/10] Wait for rabbitmq to start up --- bin/local-startup.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/local-startup.sh b/bin/local-startup.sh index aa14de2..6931560 100755 --- a/bin/local-startup.sh +++ b/bin/local-startup.sh @@ -18,4 +18,5 @@ else echo "Starting RabbitMQ" # Start RabbitMQ docker-compose up -d rabbitmq + sleep 10 fi \ No newline at end of file From dc2ce1749ec78bcf88f0b5782b9352eb42d81108 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Tue, 16 Jul 2024 09:22:53 +0700 Subject: [PATCH 08/10] Add publish workflow --- .github/workflows/publish.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..abbc3cf --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,24 @@ +name: docker_publish + +on: + push: + branches: + - 'hiep/add-github-actions' + tags: + - 'hiep/add-github-actions' + +jobs: + lint_test: + uses: babylonchain/.github/.github/workflows/reusable_go_lint_test.yml@hiep/add-reusable-workflows + with: + run-unit-tests: true + run-integration-tests: false + run-lint: false + + docker_pipeline: + needs: ["lint_test"] + uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@hiep/add-reusable-workflows + secrets: inherit + with: + publish: true + dockerfile: ./contrib/images/staking-api-service/Dockerfile \ No newline at end of file From ceac5d46360f8f4b321bf99ee9f25d3d844e39b2 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Thu, 18 Jul 2024 14:42:34 +0700 Subject: [PATCH 09/10] Remove circleCI and update the on push condition --- .circleci/config.yml | 105 ---------------------------------- .github/workflows/publish.yml | 5 +- 2 files changed, 3 insertions(+), 107 deletions(-) delete mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 29f2ff1..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,105 +0,0 @@ -version: 2.1 - -orbs: - aws-ecr: circleci/aws-ecr@8.2.1 - go: circleci/go@1.9.0 - kubernetes: circleci/kubernetes@1.3.1 - helm: circleci/helm@2.0.1 - -jobs: - build_lint_tests: - machine: - image: ubuntu-2204:2024.01.1 - resource_class: large - steps: - - go/install: - version: "1.21.4" - - checkout - - run: - name: Print Go environment - command: "go env" - - restore_cache: # restores saved cache if no changes are detected since last run - keys: - - go-mod-v6-{{ checksum "go.sum" }} - - add_ssh_keys - - run: - name: Build Staking API Service - command: make build - - save_cache: - key: go-mod-v6-{{ checksum "go.sum" }} - paths: - - "/home/circleci/.go_workspace/pkg/mod" - # TODO: https://github.com/babylonchain/staking-api-service/issues/29 - # - run: - # name: Lint - # command: | - # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.55.2 - # ./bin/golangci-lint run - - run: - name: Run tests - command: | - make tests - - build_docker: - machine: - image: ubuntu-2204:2024.01.1 - resource_class: large - steps: - - checkout - - add_ssh_keys - - aws-ecr/build-image: - push-image: false - dockerfile: Dockerfile - path: ./contrib/images/staking-api-service/ - build-path: ./ - tag: "$CIRCLE_SHA1,$CIRCLE_TAG" - repo: "staking-api-service" - - run: - name: Save Docker image to export it to workspace - command: | - docker save $(docker image ls --format '{{.Repository}}:{{.Tag}}') > /tmp/staking-api-service.tar - - persist_to_workspace: - root: /tmp - paths: - - staking-api-service.tar - - push_docker: - machine: - image: ubuntu-2204:2024.01.1 - resource_class: large - steps: - - add_ssh_keys - - attach_workspace: - at: /tmp - - run: - name: Load Docker image from workspace - command: | - docker load -i /tmp/staking-api-service.tar - - aws-ecr/ecr-login: - aws-access-key-id: AWS_ACCESS_KEY_ID - aws-secret-access-key: AWS_SECRET_ACCESS_KEY - region: "$AWS_REGION" - - aws-ecr/push-image: - registry-id: AWS_ECR_REGISTRY_ID - region: "$AWS_REGION" - repo: "staking-api-service" - tag: "$CIRCLE_SHA1,$CIRCLE_TAG" - -workflows: - CICD: - jobs: - - build_lint_tests - - build_docker: - filters: - tags: - only: /.*/ - - push_docker: - requires: - - build_docker - filters: - tags: - only: /.*/ - branches: - only: - - main - - dev diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index abbc3cf..b9592ba 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -3,9 +3,10 @@ name: docker_publish on: push: branches: - - 'hiep/add-github-actions' + - 'main' + - 'dev' tags: - - 'hiep/add-github-actions' + - '*' jobs: lint_test: From adefe869f3bfe5657ef222dd680cda448fe3c027 Mon Sep 17 00:00:00 2001 From: Hiep Mai Date: Wed, 24 Jul 2024 10:24:02 +0700 Subject: [PATCH 10/10] Point resuable workflows to v0.1.0 --- .github/workflows/ci.yml | 4 ++-- .github/workflows/publish.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7f88677..facc47c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,14 +7,14 @@ on: jobs: lint_test: - uses: babylonchain/.github/.github/workflows/reusable_go_lint_test.yml@hiep/add-reusable-workflows + uses: babylonchain/.github/.github/workflows/reusable_go_lint_test.yml@v0.1.0 with: run-unit-tests: true run-integration-tests: false run-lint: false docker_pipeline: - uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@hiep/add-reusable-workflows + uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@v0.1.0 secrets: inherit with: publish: false diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b9592ba..8c080e2 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -10,7 +10,7 @@ on: jobs: lint_test: - uses: babylonchain/.github/.github/workflows/reusable_go_lint_test.yml@hiep/add-reusable-workflows + uses: babylonchain/.github/.github/workflows/reusable_go_lint_test.yml@v0.1.0 with: run-unit-tests: true run-integration-tests: false @@ -18,7 +18,7 @@ jobs: docker_pipeline: needs: ["lint_test"] - uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@hiep/add-reusable-workflows + uses: babylonchain/.github/.github/workflows/reusable_docker_pipeline.yml@v0.1.0 secrets: inherit with: publish: true