Update docker-deploy.yml #16
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: Build and Deploy Docker Image | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
types: [closed] | |
branches: | |
- main | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v3 | |
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: Log in to AWS ECR | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Build Docker image | |
id: build-image | |
run: | | |
docker build -t my-app:${{ github.sha }} . | |
echo "DOCKER_IMAGE=my-app:${{ github.sha }}" >> $GITHUB_ENV | |
- name: Determine ECR Repository | |
id: ecr-repo | |
run: | | |
if [[ "${{ github.event.pull_request.merged }}" == "true" && "${{ github.event.pull_request.labels }}" == *"production"* ]]; then | |
echo "ECR_REPOSITORY=${{ secrets.PROD_ECR_URI }}" >> $GITHUB_ENV | |
elif [[ "${{ github.event.pull_request.merged }}" != "true" && "${{ github.event.pull_request.labels }}" == *"development"* ]]; then | |
echo "ECR_REPOSITORY=${{ secrets.DEV_ECR_URI }}" >> $GITHUB_ENV | |
elif [[ "${{ github.event.pull_request.merged }}" != "true" && -z "${{ github.event.pull_request.labels }}" ]]; then | |
echo "ECR_REPOSITORY=${{ secrets.DEV_ECR_URI }}" >> $GITHUB_ENV | |
elif [[ "${{ github.event.pull_request.merged }}" == "true" && -z "${{ github.event.pull_request.labels }}" ]]; then | |
echo "ECR_REPOSITORY=${{ secrets.PROD_ECR_URI }}" >> $GITHUB_ENV | |
fi | |
- name: Tag Docker image | |
run: docker tag ${{ env.DOCKER_IMAGE }} ${{ env.ECR_REPOSITORY }}:${{ github.sha }} | |
- name: Push Docker image to Dev (with development label or no label) | |
if: | | |
(github.event_name == 'push' && (github.event.pull_request.labels != 'production')) || | |
(github.event_name == 'pull_request' && github.event.pull_request.merged == false && env.ECR_REPOSITORY == env.DEV_ECR_URI) | |
run: docker push ${{ env.ECR_REPOSITORY }}:${{ github.sha }} | |
- name: Push Docker image to Prod (after merge with production label or no label) | |
if: | | |
github.event_name == 'pull_request' && github.event.pull_request.merged == true && | |
(env.ECR_REPOSITORY == env.PROD_ECR_URI) | |
run: docker push ${{ env.ECR_REPOSITORY }}:${{ github.sha }} |