-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
name: Release NestJS CI/CD on Kubernetes | ||
|
||
on: | ||
push: | ||
branches: [ "k8s" ] | ||
|
||
env: | ||
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} | ||
EKS_CLUSTER_NAME: ${{ vars.EKS_CLUSTER_NAME }} | ||
AWS_REGION: ${{ vars.AWS_REGION }} | ||
IMAGE_TAG: "pr-${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}" | ||
|
||
jobs: | ||
build: | ||
name: Deployment | ||
runs-on: ubuntu-latest | ||
environment: develop | ||
steps: | ||
- name: Cancel Previous Runs | ||
uses: styfle/[email protected] | ||
with: | ||
access_token: ${{ github.token }} | ||
|
||
- name: Set short git commit SHA | ||
id: commit | ||
uses: prompt/actions-commit-hash@v2 | ||
|
||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: $AWS_REGION | ||
|
||
- name: Login to Amazon ECR Public | ||
id: login-ecr-public | ||
uses: aws-actions/amazon-ecr-login@v2 | ||
with: | ||
registry-type: public | ||
|
||
- name: Set up Docker Buildx | ||
id: buildx | ||
uses: docker/setup-buildx-action@master | ||
|
||
- name: Docker cache layers | ||
uses: actions/cache@v2 | ||
with: | ||
path: /tmp/.buildx-cache | ||
key: ${{ runner.os }}-single-buildx-${{ github.sha }} | ||
restore-keys: | | ||
${{ runner.os }}-single-buildx | ||
- name: Build & Push Image | ||
env: | ||
ECR_REGISTRY: ${{ steps.login-ecr-public.outputs.registry }} | ||
|
||
run: | | ||
echo $ECR_REGISTRY | ||
touch .env | ||
echo NODE_ENV=${{ vars.NODE_ENV }} >> .env | ||
echo PORT=${{ vars.PORT }} >> .env | ||
echo MONGODB_CONNECTION_STRING=${{ secrets.MONGODB_CONNECTION_STRING }} >> .env | ||
echo CORS_VALID_ORIGINS=${{ vars.CORS_VALID_ORIGINS }} >> .env | ||
echo JWT_ACCESS_SECRET=${{ vars.JWT_ACCESS_SECRET }} >> .env | ||
echo JWT_ACCESS_EXPIRATION=${{ vars.JWT_ACCESS_EXPIRATION }} >> .env | ||
echo JWT_REFRESH_SECRET=${{ vars.JWT_REFRESH_SECRET }} >> .env | ||
echo JWT_REFRESH_EXPIRATION=${{ vars.JWT_REFRESH_EXPIRATION }} >> .env | ||
echo SMTP_USERNAME=${{ secrets.SMTP_USERNAME }} >> .env | ||
echo SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }} >> .env | ||
echo SMTP_HOST=${{ vars.SMTP_HOST }} >> .env | ||
echo SMTP_PORT=${{ vars.SMTP_PORT }} >> .env | ||
echo SMTP_FROM_EMAIL=${{ vars.SMTP_FROM_EMAIL }} >> .env | ||
echo SMTP_FROM_NAME=${{ vars.SMTP_FROM_NAME }} >> .env | ||
docker buildx create --use | ||
docker buildx build \ | ||
--cache-from=type=local,src=/tmp/.buildx-cache \ | ||
--cache-to=type=local,dest=/tmp/.buildx-cache-new \ | ||
--tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \ | ||
--target release \ | ||
--push \ | ||
. | ||
rm -rf /tmp/.buildx-cache | ||
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | ||
- name: Update kube config | ||
run: aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $AWS_REGION | ||
|
||
- name: Deploy to EKS | ||
env: | ||
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
run: | | ||
sed -i.bak "s|DOCKER_IMAGE|$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG|g" k8s/deployment.yaml && \ | ||
kubectl apply -f k8s/deployment.yaml | ||
kubectl apply -f k8s/ingress.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters