Sarjit Delivala
Sarjit Delivala

Reputation: 567

Build Docker Image and tag it with github tag name

I have created a GitHub action on repo tag creation. I am successfully able to build and push the Docker image to AWS but, I don't know how to tag the image with the same name of the GitHub tag. Below is my git workflow file

name: Build Docker Image and Push to AWS ECR
on:
  push:
    tags:
    - '*'


jobs:

  build:

     runs-on: ubuntu-latest

     steps:
         - name: Checkout
           uses: actions/checkout@v1

         - name: Configure AWS credentials
           uses: aws-actions/configure-aws-credentials@v1
           with:
               aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
               aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
               aws-region: us-west-2

         - name: Login to Amazon ECR
           id: login-ecr
           uses: aws-actions/amazon-ecr-login@v1

         - name: Build, tag, and push image to Amazon ECR
           id: build-image
           env:
               ECR_REGISTRY: ${{ secrets.AWS_REGISTRY }}
               ECR_REPOSITORY: repo_name
               IMAGE_TAG: latest
           run: |
               docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
               docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

Please help me in replacing the correct value at IMAGE_TAG in the above code

Upvotes: 5

Views: 10520

Answers (3)

robyn
robyn

Reputation: 11

Example of how I accomplished this: https://github.com/nsidc/noaadata-web-server-metrics/blob/main/.github/workflows/docker-image.yml

 

name: Docker Image CI

on:
  push:
    branches: [ "main" ]
    tags: ["v[0-9]+.[0-9]+.[0-9]*"]

    
    
jobs:

  build:

    runs-on: "ubuntu-latest"

    steps:
    - name: "Check out the repo"
      uses: "actions/checkout@v3"
    
    - name: "docker login"
      uses: "docker/login-action@v2"
      with:
        username: ${{secrets.DOCKER_USER}}
        password: ${{secrets.DOCKER_PASS}}
      
    - name: "Build, Tag, and push the Docker image - latest"
      if: ${{ github.ref_name == 'main' }}
      env:
        IMAGE_NAME: nsidc/noaadata-web-server-metrics
        IMAGE_TAG: latest
      run: |
        docker build -t $IMAGE_NAME:$IMAGE_TAG .
        docker push $IMAGE_NAME:$IMAGE_TAG
        
    - name: "Build, Tag, and push the Docker image"
      if: ${{ github.ref_name != 'main' }}
      env:
        IMAGE_NAME: nsidc/noaadata-web-server-metrics
        IMAGE_TAG: ${{ github.ref_name }}
      run: |
        docker build -t $IMAGE_NAME:$IMAGE_TAG .
        docker push $IMAGE_NAME:$IMAGE_TAG

Upvotes: 1

Jhonny Ramirez Zeballos
Jhonny Ramirez Zeballos

Reputation: 3156

Use GITHUB_REF_NAME variable to get latest tag:

name: Bolivia Version - Develop

on:
  push:
    tags: # <---- only tags, important!!!
    - '*'

jobs:
  build-version:

    runs-on: ubuntu-latest

    steps:
    - name: Git checkout
      uses: actions/checkout@v2

    - name: Extract latest tag
      run: |
        GIT_TAG=$GITHUB_REF_NAME

Upvotes: 1

DannyB
DannyB

Reputation: 14776

We decided the use the git commit sha as the image tag, as it always represents the unique state of the code.

- name: Build, tag, and push image to Amazon ECR
  env:
    ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
    ECR_REPOSITORY: reponame
    IMAGE_TAG: ${{ github.sha }}
  run: |
    docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
    docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

If you need or prefer to use the commit tag, you just need to extract it from the ref using something like this:

- name: Extract Git Tag
  run: echo "GIT_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
- name: Build, tag, and push image to Amazon ECR
  env:
    ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
    ECR_REPOSITORY: reponame
    IMAGE_TAG: ${{ env.GIT_TAG }}
  run: |
    docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
    docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

Upvotes: 10

Related Questions