[CICD] k8s.yml shorten file #9
Workflow file for this run
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
name: NestJS CI/CD on Kubernetes | ||
on: | ||
push: | ||
branches: [ "k8s" ] | ||
jobs: | ||
build: | ||
name: CI/CD on EKS | ||
runs-on: ubuntu-latest | ||
environment: develop | ||
env: | ||
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} | ||
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: ${{ vars.AWS_REGION }} | ||
- name: Login to Amazon ECR | ||
id: login-ecr | ||
uses: aws-actions/amazon-ecr-login@v2 | ||
- 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.outputs.registry }} | ||
IMAGE_TAG: ${{ steps.commit.outputs.short }} | ||
run: | | ||
Check failure on line 56 in .github/workflows/k8s.yml
|
||
touch .env | ||
for var in NODE_ENV PORT CORS_VALID_ORIGINS JWT_ACCESS_SECRET JWT_ACCESS_EXPIRATION JWT_REFRESH_SECRET JWT_REFRESH_EXPIRATION SMTP_HOST SMTP_PORT SMTP_FROM_EMAIL SMTP_FROM_NAME | ||
do | ||
echo $var=${{ vars[$var] }} >> .env | ||
done | ||
for var in MONGODB_CONNECTION_STRING SMTP_USERNAME SMTP_PASSWORD | ||
do | ||
echo $var=${{ secrets[$var] }} >> .env | ||
done | ||
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 ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }} \ | ||
--push \ | ||
. | ||
rm -rf /tmp/.buildx-cache | ||
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | ||
- name: Update kube config | ||
run: aws eks update-kubeconfig --name ${{ vars.EKS_CLUSTER_NAME }} --region ${{ vars.AWS_REGION }} | ||
- name: Deploy to EKS | ||
env: | ||
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
IMAGE_TAG: ${{ steps.commit.outputs.short }} | ||
run: | | ||
sed -i.bak "s|DOCKER_IMAGE|${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}|g" k8s/deployment.yaml && \ | ||
kubectl apply -f k8s/deployment.yaml | ||
kubectl apply -f k8s/ingress.yaml |