Skip to content

Instantly share code, notes, and snippets.

@bfrancom
Last active March 7, 2023 15:35
Show Gist options
  • Save bfrancom/6ebebbf899912cd3c76c7f2978be0961 to your computer and use it in GitHub Desktop.
Save bfrancom/6ebebbf899912cd3c76c7f2978be0961 to your computer and use it in GitHub Desktop.
GitHub actions build Java jar and deploy to AWS ECS Fargate
#See https://gist.github.com/bfrancom/d2ca0f7c767092a924c99545298b557d for Dockerfile used
#Author: Ben Francom
name: Build
#on: workflow_dispatch
on:
push:
branches:
- "main"
jobs:
build:
name: Package & Deploy
runs-on: ubuntu-latest
timeout-minutes: 25
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
- name: Set up JDK 8
uses: actions/setup-java@v1
with:
java-version: 8
- name: Cache local Maven repository
uses: actions/cache@v2
with:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Maven Package
run: mvn -B package -DskipTests -f ./pom.xml
- 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: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: set env
id: vars
run: |
echo ::set-output name=github_short_sha::$(echo ${GITHUB_SHA:0:7})
- name: Build & Push Image
id: build-image
env:
DOCKER_BUILDKIT: 1
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: app-repo
IMAGE_TAG: ${{ steps.vars.outputs.github_short_sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:latest -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Fill in the new image ID for App Fargate task definition
id: app-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: .aws/prod-definition.json
container-name: prod-app-container
image: ${{ steps.build-image.outputs.image }}
- name: Deploy App Fargate Task
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.app-task-def.outputs.task-definition }}
service: prod-service
cluster: prod-cluster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment