From dcf72934aa802a10f40180020af3642e5a1c89df Mon Sep 17 00:00:00 2001 From: hiepmai-babylonchain <167088365+hiepmai-babylonchain@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:18:48 +0700 Subject: [PATCH] feat: Add go_lint_test and docker_pipeline workflow (#1) --- .../workflows/reusable_docker_pipeline.yml | 149 ++++++++++++++++++ .github/workflows/reusable_go_lint_test.yml | 144 +++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 .github/workflows/reusable_docker_pipeline.yml create mode 100644 .github/workflows/reusable_go_lint_test.yml diff --git a/.github/workflows/reusable_docker_pipeline.yml b/.github/workflows/reusable_docker_pipeline.yml new file mode 100644 index 0000000..086037f --- /dev/null +++ b/.github/workflows/reusable_docker_pipeline.yml @@ -0,0 +1,149 @@ +name: Docker Build & Publish + +on: + workflow_call: + inputs: + dockerfile: + required: false + type: string + description: "Path to Dockerfile" + default: "Dockerfile" + dockerContext: + required: false + type: string + description: "The Docker context" + default: "." + publish: + required: true + type: boolean + repoName: + required: false + type: string + description: "Custom repository name" + default: "" + +jobs: + docker_build: + runs-on: ubuntu-22.04 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + if: inputs.publish == true + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Determine image name + id: set_image_name + run: | + if [ -n "${{ inputs.repoName }}" ]; then + echo "IMAGE_NAME=${{ inputs.repoName }}" >> $GITHUB_ENV + else + echo "IMAGE_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV + fi + + - name: Build Docker image + uses: docker/build-push-action@v6 + with: + tags: ${{ env.IMAGE_NAME }}:${{ github.sha }} + outputs: type=docker,dest=/tmp/${{ env.IMAGE_NAME }}.tar + context: ${{ inputs.dockerContext }} + file: ${{ inputs.dockerfile }} + + - name: Upload Docker image to workspace + if: inputs.publish == true + uses: actions/upload-artifact@v4 + with: + name: ${{ env.IMAGE_NAME }} + path: /tmp/${{ env.IMAGE_NAME }}.tar + + dockerhub_publish: + runs-on: ubuntu-22.04 + if: inputs.publish == true + needs: ["docker_build"] + steps: + - name: Determine image name + id: set_image_name + run: | + if [ -n "${{ inputs.repoName }}" ]; then + echo "IMAGE_NAME=${{ inputs.repoName }}" >> $GITHUB_ENV + else + echo "IMAGE_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV + fi + + - name: Download Docker image from workspace + uses: actions/download-artifact@v4 + with: + name: ${{ env.IMAGE_NAME }} + path: /tmp/ + + - name: Load Docker image + run: docker load -i /tmp/${{ env.IMAGE_NAME }}.tar + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Push Docker image with SHA + run: | + docker tag ${{ env.IMAGE_NAME }}:${{ github.sha }} ${{ vars.DOCKERHUB_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + docker push ${{ vars.DOCKERHUB_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + + - name: Push Docker image with Tag + if: startsWith(github.ref, 'refs/tags/') + run: | + docker tag ${{ env.IMAGE_NAME }}:${{ github.sha }} ${{ vars.DOCKERHUB_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} + docker push ${{ vars.DOCKERHUB_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} + + ecr_publish: + runs-on: ubuntu-22.04 + if: inputs.publish == true + needs: ["docker_build"] + steps: + - name: Determine image name + id: set_image_name + run: | + if [ -n "${{ inputs.repoName }}" ]; then + echo "IMAGE_NAME=${{ inputs.repoName }}" >> $GITHUB_ENV + else + echo "IMAGE_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV + fi + + - name: Download Docker image from workspace + uses: actions/download-artifact@v4 + with: + name: ${{ env.IMAGE_NAME }} + path: /tmp/ + + - name: Load Docker image + run: docker load -i /tmp/${{ env.IMAGE_NAME }}.tar + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ vars.AWS_ECR_REGION }} + + - name: Login to Amazon ECR Private + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Push Docker image with SHA + run: | + docker tag ${{ env.IMAGE_NAME }}:${{ github.sha }} ${{ vars.AWS_ECR_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + docker push ${{ vars.AWS_ECR_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.sha }} + + - name: Push Docker image with Tag + if: startsWith(github.ref, 'refs/tags/') + run: | + docker tag ${{ env.IMAGE_NAME }}:${{ github.sha }} ${{ vars.AWS_ECR_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} + docker push ${{ vars.AWS_ECR_REGISTRY_ID }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} \ No newline at end of file diff --git a/.github/workflows/reusable_go_lint_test.yml b/.github/workflows/reusable_go_lint_test.yml new file mode 100644 index 0000000..cc9563f --- /dev/null +++ b/.github/workflows/reusable_go_lint_test.yml @@ -0,0 +1,144 @@ +name: Reusable Go Build, Lint, and Test Workflow + +on: + workflow_call: + inputs: + go-version: + description: 'Go version' + type: string + default: '1.22.3' + go-lint-version: + description: 'Go Lint version' + type: string + default: 'v1.59' + install-dependencies-command: + description: 'Command to install dependencies' + required: false + type: string + run-lint: + description: 'Run lint' + type: boolean + default: false + run-unit-tests: + description: 'Run unit tests' + type: boolean + default: false + run-integration-tests: + description: 'Run integration tests' + type: boolean + default: false + # allow customizing the test command (e.g sudo make test-e2e) + integration-tests-command: + description: 'Command to run integration tests' + required: false + type: string + default: 'make test-e2e' + run-check-mock-gen: + description: 'Run check-mock-gen' + type: boolean + default: false + +jobs: + build: + 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: ${{ inputs.go-version }} + + - 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: Install Dependencies + if: ${{ inputs.install-dependencies-command != '' }} + run: ${{ inputs.install-dependencies-command }} + + - name: Build Application + run: make build + + lint: + runs-on: ubuntu-22.04 + if: ${{ inputs.run-lint }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ inputs.go-version }} + + - name: Run Lint + uses: golangci/golangci-lint-action@v6 + with: + version: ${{ inputs.go-lint-version }} + args: --timeout=10m + + unit-tests: + runs-on: ubuntu-22.04 + if: ${{ inputs.run-unit-tests }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ inputs.go-version }} + + - name: Run Unit Tests + run: | + make test + + integration-tests: + runs-on: ubuntu-22.04 + if: ${{ inputs.run-integration-tests }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ inputs.go-version }} + + - name: Run Integration Tests + run: ${{ inputs.integration-tests-command }} + + check-mock-gen: + runs-on: ubuntu-22.04 + if: ${{ inputs.run-check-mock-gen }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ inputs.go-version }} + + - name: Run make mock-gen + run: make mock-gen + + - name: Check for uncommitted changes + run: | + if ! git diff --exit-code; then + echo "Uncommitted changes detected. Please run 'make mock-gen' before committing." + exit 1 + fi \ No newline at end of file