add deploy to ECR pipeline #1
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: CI Pipeline | |
on: | |
pull_request: | |
branches: | |
- main | |
push: | |
branches: | |
- main | |
env: | |
DOCKER_REGISTRY: 057560478517.dkr.ecr.us-east-1.amazonaws.com | |
IMAGE_NAME: devops-estudos/nestjs-app | |
ARTIFACT_NAME: nestjs-app-${{ github.sha }} | |
AWS_REGION: us-east-1 | |
NODE_VERSION: 22 | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
outputs: | |
image_name_with_tag: ${{ steps.extract-first.outputs.first_tag }} | |
artifact_name: ${{ env.ARTIFACT_NAME }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: docker/setup-buildx-action@v3 | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }} | |
- id: extract-first | |
run: | | |
tags="${{ steps.meta.outputs.tags }}" | |
first_tag=$(echo "$tags" | head -n 1) | |
echo "first_tag=$first_tag" >> $GITHUB_OUTPUT | |
- name: Build and export | |
id: build | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
outputs: type=docker,dest=/tmp/${{ env.ARTIFACT_NAME }}.tar | |
- name: Upload artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: ${{ env.ARTIFACT_NAME }} | |
path: /tmp/${{ env.ARTIFACT_NAME }}.tar | |
retention-days: 3 | |
push-and-sign-image: | |
runs-on: ubuntu-latest | |
needs: [build] | |
permissions: | |
id-token: write | |
packages: write | |
contents: read | |
attestations: write | |
env: | |
ARTIFACT_NAME: ${{ needs.build.outputs.artifact_name }} | |
IMAGE_NAME_WITH_TAG: ${{ needs.build.outputs.image_name_with_tag }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Configure AWS credentials | |
uses: aws-actions/[email protected] | |
with: | |
role-to-assume: ${{ secrets.OIDC_ARN_ROLE}} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Download artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: ${{ env.ARTIFACT_NAME }} | |
path: /tmp | |
- name: Load image | |
run: | | |
docker load --input /tmp/${{ env.ARTIFACT_NAME }}.tar | |
- name: Push image to Container Registry | |
id: push | |
run: | | |
docker push ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }} --all-tags | |
digest=$(docker image inspect ${{ env.IMAGE_NAME_WITH_TAG }} --format '{{index .RepoDigests 0}}' | cut -d'@' -f2) | |
echo "digest=$digest" >> "$GITHUB_OUTPUT" |