Skip to content

Instantly share code, notes, and snippets.

@davi020
Last active March 29, 2024 02:39
Show Gist options
  • Save davi020/89b1f9787239c3141da93e40806f0893 to your computer and use it in GitHub Desktop.
Save davi020/89b1f9787239c3141da93e40806f0893 to your computer and use it in GitHub Desktop.
Resolving "no basic auth credentials Error: Process completed with exit code 1" in AWS ECR GitHub Action Build Workflow
name: Build docker image and deploy it to ECR
# Controls when the workflow will run
on:
# Triggers the workflow on push
push:
branches: [ your-branch ]
jobs:
build:
name: Build Image Phase
runs-on: self-hosted
permissions:
id-token: write # required to use OIDC authentication
contents: read # required to checkout the code from the repo
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Step 1: Check out code from the repository
- name: Check out code
uses: actions/checkout@v2
# Step 2: Configure AWS credentials to authenticate with AWS services
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::xxxxxxxx:role/your-role-name
role-duration-seconds: 900
aws-region: your-region
# Step 3: Login to Amazon ECR to authenticate Docker client
- name: Login to Amazon ECR
id: login-ecr
run: |
# Get the ECR login password and use it to log in to the ECR registry
result=$(aws ecr get-login-password --region your-region | docker --config ${GITHUB_WORKSPACE}/${GITHUB_RUN_ID} login --username AWS --password-stdin xxxxxxxxx.dkr.ecr.your-region.amazonaws.com)
# Print the login result for debugging
echo "Login Result: $result"
# Step 4: Build, tag, and push the Docker image to Amazon ECR
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: your-registery-details
ECR_REPOSITORY: your-repo-name
TAG_COMMIT: your-tag-commit
IMAGE_TAG: your-image-tag
run: |
# Build the Docker image using the specified tags
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
# Tag the Docker image with the commit tag and the image tag
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:$TAG_COMMIT
# Push the Docker image to the ECR repository using Docker config from the workspace
docker --config ${GITHUB_WORKSPACE}/${GITHUB_RUN_ID} push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker --config ${GITHUB_WORKSPACE}/${GITHUB_RUN_ID} push $ECR_REGISTRY/$ECR_REPOSITORY:$TAG_COMMIT
# Remove the Docker config file after pushing the image
rm -fr ${GITHUB_WORKSPACE}/${GITHUB_RUN_ID}/config.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment