Skip to content

Commit

Permalink
feat: Add go_lint_test and docker_pipeline workflow (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiepmai-babylonchain authored Jul 24, 2024
1 parent 1a6db66 commit dcf7293
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 0 deletions.
149 changes: 149 additions & 0 deletions .github/workflows/reusable_docker_pipeline.yml
Original file line number Diff line number Diff line change
@@ -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 }}
144 changes: 144 additions & 0 deletions .github/workflows/reusable_go_lint_test.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dcf7293

Please sign in to comment.