Skip to content

Instantly share code, notes, and snippets.

@gifhuppp
Forked from JimmyBjorklund/aws-develop.yaml
Created December 3, 2021 09:27
Show Gist options
  • Save gifhuppp/7aa3bd7bdec3498650c558c558d2e15a to your computer and use it in GitHub Desktop.
Save gifhuppp/7aa3bd7bdec3498650c558c558d2e15a to your computer and use it in GitHub Desktop.
Use git actions to deploy to AWS EKS and ECR, will build your development branch and tag with develop-n and latest.
name: Deploy to Develop ECR
on:
push:
branches:
- develop
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: eu-west-1 # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY: my-repo # set this to your Amazon ECR repository name
ECS_SERVICE: my-service # set this to your Amazon EKS service name
ECS_CLUSTER: my-cluster # set this to your Amazon EKS cluster name
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)
- name: Get build number
id: build_number
run: echo ::set-output name=NUMBER::$( echo $GITHUB_RUN_NUMBER )
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
#IMAGE_TAG: ${{ github.sha }}
IMAGE_TAG: ${{ steps.get_version.outputs.VERSION }}-${{ steps.build_number.outputs.NUMBER }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
# echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:latest"
- name: deploy to cluster
uses: kodermax/kubectl-aws-eks@master
env:
AWS_DEFAULT_REGION: ${{ env.AWS_REGION}}
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
with:
args: set image deployment/$ECR_REPOSITORY $ECR_REPOSITORY=${{ steps.build-image.outputs.image }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment