Kubernetes Deployment for Simple Golang API
version: 2.1
jobs:
build-and-deploy:
docker:
- image: circleci/golang:1.19.5
environment:
AWS_REGION: ap-southeast-1
AWS_ACCOUNT_ID: 0987612345
IMAGE_NAME: devopscorner/bookstore
steps:
- checkout
- run:
name: Build and push Docker image
command: |
go build -o app
if [[ "$CIRCLE_BRANCH" == "main" ]]; then
semver=1.0.0-${CIRCLE_SHA1:0:8}
elif [[ "$CIRCLE_BRANCH" == "features/"* ]]; then
semver=1.0.0-${CIRCLE_BRANCH#features/}.${CIRCLE_SHA1:0:8}
elif [[ "$CIRCLE_BRANCH" == "bugfix/"* ]]; then
semver=1.1.0-${CIRCLE_BRANCH#bugfix/}.${CIRCLE_SHA1:0:8}
elif [[ "$CIRCLE_BRANCH" == "hotfix/"* ]]; then
semver=1.1.1-${CIRCLE_BRANCH#hotfix/}.${CIRCLE_SHA1:0:8}
fi
echo "Semantic version: $semver"
echo "imageTag=$semver" >> $HOME/variables.env
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_NAME:$semver .
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_NAME:$semver
docker tag $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_NAME:$semver $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_NAME:latest
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$IMAGE_NAME:latest
- run:
name: Deploy to Kubernetes using Helmfile
command: |
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
helmfile sync
- store_artifacts:
path: app
workflows:
build-and-deploy:
jobs:
- build-and-deploy:
requires:
- checkout
filters:
branches:
only:
- main
- /^features\/.*$/
- /^bugfix\/.*$/
- /^hotfix\/.*$/