From 1cb0a24d807ce893565982216aecb0f622e6ad43 Mon Sep 17 00:00:00 2001 From: Bayu Aditya Date: Fri, 9 May 2025 22:03:58 +0700 Subject: [PATCH 1/2] feat: enhance Docker image build process and add infrastructure update step --- .github/workflows/test.yaml | 61 ++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f96d6ff..d296b85 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -7,6 +7,7 @@ on: - '!main' env: + DOCKER_USERNAME: bayu3490 IMAGE_NAME: ideagate-server-controller jobs: @@ -43,6 +44,10 @@ jobs: name: Build Docker Image runs-on: ubuntu-latest needs: test + outputs: + image-name-tag: ${{ steps.generate-image-name.outputs.image-name-tag }} + image-name: ${{ steps.generate-image-name.outputs.image-name }} + image-tag: ${{ steps.generate-image-name.outputs.image-tag }} steps: - name: Checkout code @@ -54,13 +59,53 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Build and push - uses: docker/build-push-action@v6 + - name: Generate Image Name + id: generate-image-name + run: | + echo "image-name-tag=${{ env.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.commit.outputs.short }}" >> $GITHUB_OUTPUT + echo "image-name=${{ env.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT + echo "image-tag=${{ steps.commit.outputs.short }}" >> $GITHUB_OUTPUT + +# - name: Build +# uses: docker/build-push-action@v6 +# with: +# context: . +# push: false +# tags: ${{ steps.generate-image-name.outputs.image-name-tag }} +# +# - name: Get size of the image +# run: | +# docker images ${{ steps.generate-image-name.outputs.image-name-tag }} --format "Image Size: {{.Size}}" + + update-infrastructure: + name: Update Infrastructure + runs-on: ubuntu-latest + needs: docker-image-build + + steps: + - name: Checkout Infrastructure Repo + uses: actions/checkout@v3 with: - context: . - push: false - tags: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.commit.outputs.short }} + token: '${{ secrets.GH_TOKEN }}' + fetch-depth: 0 + repository: ideagate/infrastructure + ref: main + path: deployment - - name: Get size of the image - run: | - docker images ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.commit.outputs.short }} --format "Image Size: {{.Size}}" \ No newline at end of file + - name: Push Promoted Image to Infrastructure Repo + uses: kustomize-everything/action-promote@v3.7.2 + id: promote + with: + target-repo: ideagate/infrastructure + target-branch: main + working-directory: deployment + images: |- + [ + { + "name": "server-controller", + "newName": "${{ needs.docker-image-build.outputs.image-name }}", + "newTag": "${{ needs.docker-image-build.outputs.image-tag }}", + "overlays": ["apps/server-controller/overlays/staging"] + } + ] + github-token: ${{ secrets.GH_TOKEN }} \ No newline at end of file From e80ff0e714f3f9093392712d664b3af2d9c52151 Mon Sep 17 00:00:00 2001 From: Bayu Aditya Date: Fri, 9 May 2025 22:49:00 +0700 Subject: [PATCH 2/2] ci: enhance CI pipeline with Go testing and Docker image build steps --- .github/workflows/deploy-staging.yaml | 88 ++++++++++++++++++++++++--- .github/workflows/test.yaml | 49 +++------------ 2 files changed, 89 insertions(+), 48 deletions(-) diff --git a/.github/workflows/deploy-staging.yaml b/.github/workflows/deploy-staging.yaml index 8cd36aa..66a06f2 100644 --- a/.github/workflows/deploy-staging.yaml +++ b/.github/workflows/deploy-staging.yaml @@ -6,21 +6,51 @@ on: - 'main' env: + DOCKER_USERNAME: bayu3490 IMAGE_NAME: ideagate-server-controller jobs: - build-and-push: + test: + name: Go Test runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - - name: Log in to Docker Hub - uses: docker/login-action@v2 + - name: Set up Go + uses: actions/setup-go@v4 with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} + go-version: 1.23 + + - name: Cache Go modules + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Install dependencies + run: go mod tidy + + - name: Run unit tests + run: go test ./... -v + + docker-image-build: + name: Build Docker Image + runs-on: ubuntu-latest + needs: test + outputs: + image-name-tag: ${{ steps.generate-image-name.outputs.image-name-tag }} + image-name: ${{ steps.generate-image-name.outputs.image-name }} + image-tag: ${{ steps.generate-image-name.outputs.image-tag }} + + steps: + - name: Checkout code + uses: actions/checkout@v3 - id: commit uses: prompt/actions-commit-hash@v3 @@ -28,9 +58,53 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Build and push + - name: Generate Image Name + id: generate-image-name + run: | + echo "image-name-tag=${{ env.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.commit.outputs.short }}" >> $GITHUB_OUTPUT + echo "image-name=${{ env.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT + echo "image-tag=${{ steps.commit.outputs.short }}" >> $GITHUB_OUTPUT + + - name: Build uses: docker/build-push-action@v6 with: context: . push: true - tags: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.commit.outputs.short }} \ No newline at end of file + tags: ${{ steps.generate-image-name.outputs.image-name-tag }} + + - name: Get size of the image + run: | + docker images ${{ steps.generate-image-name.outputs.image-name-tag }} --format "Image Size: {{.Size}}" + + update-infrastructure: + name: Update Infrastructure + runs-on: ubuntu-latest + needs: docker-image-build + + steps: + - name: Checkout Infrastructure Repo + uses: actions/checkout@v3 + with: + token: '${{ secrets.GH_TOKEN }}' + fetch-depth: 0 + repository: ideagate/infrastructure + ref: main + path: deployment + + - name: Push Promoted Image to Infrastructure Repo + uses: kustomize-everything/action-promote@v3.7.2 + id: promote + with: + target-repo: ideagate/infrastructure + target-branch: main + working-directory: deployment + images: |- + [ + { + "name": "server-controller", + "newName": "${{ needs.docker-image-build.outputs.image-name }}", + "newTag": "${{ needs.docker-image-build.outputs.image-tag }}", + "overlays": ["apps/server-controller/overlays/staging"] + } + ] + github-token: ${{ secrets.GH_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index d296b85..6ad9581 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -66,46 +66,13 @@ jobs: echo "image-name=${{ env.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT echo "image-tag=${{ steps.commit.outputs.short }}" >> $GITHUB_OUTPUT -# - name: Build -# uses: docker/build-push-action@v6 -# with: -# context: . -# push: false -# tags: ${{ steps.generate-image-name.outputs.image-name-tag }} -# -# - name: Get size of the image -# run: | -# docker images ${{ steps.generate-image-name.outputs.image-name-tag }} --format "Image Size: {{.Size}}" - - update-infrastructure: - name: Update Infrastructure - runs-on: ubuntu-latest - needs: docker-image-build - - steps: - - name: Checkout Infrastructure Repo - uses: actions/checkout@v3 + - name: Build + uses: docker/build-push-action@v6 with: - token: '${{ secrets.GH_TOKEN }}' - fetch-depth: 0 - repository: ideagate/infrastructure - ref: main - path: deployment + context: . + push: false + tags: ${{ steps.generate-image-name.outputs.image-name-tag }} - - name: Push Promoted Image to Infrastructure Repo - uses: kustomize-everything/action-promote@v3.7.2 - id: promote - with: - target-repo: ideagate/infrastructure - target-branch: main - working-directory: deployment - images: |- - [ - { - "name": "server-controller", - "newName": "${{ needs.docker-image-build.outputs.image-name }}", - "newTag": "${{ needs.docker-image-build.outputs.image-tag }}", - "overlays": ["apps/server-controller/overlays/staging"] - } - ] - github-token: ${{ secrets.GH_TOKEN }} \ No newline at end of file + - name: Get size of the image + run: | + docker images ${{ steps.generate-image-name.outputs.image-name-tag }} --format "Image Size: {{.Size}}"