dockergithub-actionscicdamazon-ecr

Implement semantic versioning of Docker images in GitHub Actions workflow and push to ECR


I'm utilizing GitHub Actions to automate the building and pushing of a Docker image to AWS ECR. I've successfully set up a workflow for this process. However, I'm now aiming to implement Semantic Versioning and include two labels on my Docker image: [v.0.0.1] and latest. The desired outcome is a tag like v.0.0.1-latest.

Ideally, with each subsequent push, I intend to increment the version, e.g., v.0.0.2-latest, while the previous image transitions to v.0.0.1 without the latest tag. Despite my attempts using various runners and exploring forums, I've encountered challenges in finding clear explanations or examples. (this should probably solve my situation, but is very poor explained)

Could you assist me with a specific and detailed example to achieve this versioning scheme in my GitHub Actions workflow?

name: AWS ECR Push

# These permissions are needed to interact with GitHub's OIDC Token endpoint.
permissions:
  id-token: write
  contents: read

on:
  push:
    branches: [ "my_branch" ]

env:
  AWS_REGION: "ca-central-1"
  AWS_ACCOUNT_ID: "123456789"
  REPO_NAME: "my_repo"
  IMAGE_TAG: latest

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Clone repository
      uses: actions/checkout@v3

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v2
      with:
        role-to-assume: arn:aws:iam::${{env.AWS_ACCOUNT_ID }}:role/github-actions-${{ env.REPO_NAME }}
        aws-region: ${{ env.AWS_REGION }}    
        
    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1
      
    - name: Build and push the Docker image to ECR
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
      run: |
        docker build . --file Dockerfile --tag $ECR_REGISTRY/$REPO_NAME:$IMAGE_TAG
        docker push $ECR_REGISTRY/$REPO_NAME:$IMAGE_TAG

Solution

  • This resolve the issue:

    name: AWS ECR Push
    
    # These permissions are needed to interact with GitHub's OIDC Token endpoint.
    permissions:
      id-token: write
      contents: read
    
    on:
      push:
        branches: [ "my_branch" ]
    
    env:
      AWS_REGION: "ca-central-1"
      AWS_ACCOUNT_ID: "123456789"
      REPO_NAME: "my_repo"
      IMAGE_TAG: latest
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
        - name: Clone repository
          uses: actions/checkout@v3
    
        - name: Configure AWS credentials
          uses: aws-actions/configure-aws-credentials@v2
          with:
            role-to-assume: arn:aws:iam::${{env.AWS_ACCOUNT_ID }}:role/github-actions-${{ env.REPO_NAME }}
            aws-region: ${{ env.AWS_REGION }}    
            
        - name: Login to Amazon ECR
          id: login-ecr
          uses: aws-actions/amazon-ecr-login@v1
    
        - name: Extract metadata (tags, labels) for Docker
          id: meta
          uses: docker/metadata-action@v5.5.0
          with:
            images: ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}
    
        - name: Build and push Docker images
          uses: docker/build-push-action@v5.1.0
          with:
            context: .
            file: ./Dockerfile
            push: true
            tags: ${{ steps.meta.outputs.tags }}
            labels: ${{ steps.meta.outputs.labels }}