optimize github workflow 2 #11
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: prod | |
permissions: | |
id-token: write | |
contents: read | |
on: | |
pull_request: | |
types: [closed] | |
branches: | |
- master | |
workflow_dispatch: | |
env: | |
STACK_NAME: ${{ github.event.repository.name }} | |
AWS_REGION: us-east-1 # Changed to match configure-aws-credentials | |
ECR_REGISTRY: public.ecr.aws/seb-demo | |
jobs: | |
build-and-push: | |
name: Build and Push Images | |
if: github.event.pull_request.merged | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.AWS_ROLE_ARN }} | |
aws-region: ${{ env.AWS_REGION }} | |
mask-aws-account-id: true | |
- name: Verify AWS credentials | |
run: | | |
echo "Verifying AWS credentials..." | |
aws sts get-caller-identity | |
echo "Verifying ECR access..." | |
aws ecr-public get-login-password --region us-east-1 > /dev/null | |
echo "Checking ECR repository..." | |
aws ecr-public describe-repositories \ | |
--repository-names ${{ github.event.repository.name }} \ | |
--region us-east-1 || \ | |
# aws ecr-public create-repository \ | |
# --repository-name ${{ github.event.repository.name }} \ | |
# --region us-east-1 | |
- name: Login to Public ECR | |
uses: aws-actions/amazon-ecr-login@v2 | |
with: | |
registry-type: public | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Extract metadata for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: | | |
${{ env.ECR_REGISTRY }}/${{ github.event.repository.name }} | |
allamand/${{ github.event.repository.name }} | |
tags: | | |
type=sha,format=long | |
type=raw,value=latest | |
- name: Build and push | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
platforms: linux/amd64,linux/arm64 | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
provenance: false |