skip to Main Content

I would like to achieve the following CI pipeline with GitHub Actions workflow.

Pull Request is merged -> GitHub action is being triggered -> docker image is being built with semantic version incremented by one or the version is based on GitHub tag – if it is possible to somehow tag the pull request merge.

How to achieve that or is the better approach?

I have tried with secrets but no to avail. How to implement semantic versioning in GitHub Actions workflow?

3

Answers


  1. name: Docker Image CI
    
    on:
      push:
        branches: [ master ]
    
    jobs:
    
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
        - name: Build the Docker image
          run: docker build . --file Dockerfile --tag my-image-name:${{github.ref_name}}
    

    ${{github.ref_name}} pulls the tag for you or run git command like git describe --abbrev=0 in the previous step to get the latest tag and append it to image name and use it like:

    - name: Get Tag
      id: vars
      run: echo ::set-output name=tag::${git describe --abbrev=0}
    - name: Build the Docker image
      run: docker build . --file Dockerfile --tag my-image-name:${{ steps.vars.outputs.tag }}
    
    Login or Signup to reply.
  2. You can use a lot of semver actions on the market place.
    For example, I have tried using this – Semver action

    This will bump your repo version, and you can use a git bash command to get that bumped version on the next job.

    So combining with docker build, you can do something like:

    jobs:
      update-semver:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: haya14busa/action-update-semver@v1
            id: version
            with:
              major_version_tag_only: true  # (optional, default is "false")
      Build:
        name: Build Image
        needs: [update-semver]
        runs-on: ubuntu-latest
        steps:
        - name: Check out code
          uses: actions/checkout@v2
        - name: Build image
          run: |
            tag_v=$(git describe --tags $(git rev-list --tags --max-count=1))
            tag=$(echo $tag_v | sed 's/v//')
            docker build -t my_image_${tag} .
            
    
    Login or Signup to reply.
  3. Here’s my public solution: a more or less trivial docker image, built and run during pull requests, bumping version based on commits when pushed to main. All based on the awesome work of others out there, i.e. using existing GitHub actions.

    details: https://github.com/DrPsychick/docker-githubtraffic/tree/main/.github/workflows

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search