GitHub Actions For Publishing ASP.NET Docker Image To The Registry

I am a big fan of Docker Images. I usually host my websites inside a container and use Docker Hub to host my images.

Whenever I want to do a release, I manually push my image to the Docker Hub. This takes a lot of time because my applications are pretty big. So, I am trying to find a solution to automate this step. The GitHub Actions were my best choice because I already hosted my source code there.

GitHub actions allow you to automate workflows that imply building, running automated tests, or deploying.

You write a configuration file that specifies the workflow steps; then, a GitHub job will run them. Every account has 2000 minutes of execution. This is enough for almost everyone.

Configure GitHub Workflow for ASP.NET application

  1. First, you must create a GitHub repository and push your code there. Your project must be already configured to work with Docker, so a Dockerfile is required. If you don’t have a Dockerfile, right-click on the project and choose Add > Docker support.
  2. Then go to the settings tab of the GitHub repository, Secrets, and select Actions.
  3. Create the following records with the corresponding username and password of your Docker account:
    • DOCKER_USER
    • DOCKER_PASSWORD
      GitHub Secrets
  4. Go to the Repository, and choose the Actions tab. Select to create a new workflow. Then you will see a couple of recipes that can be your starting point. We will not choose any of them, but there are good ones if you don’t know how to start. GitHub Actions Recipes
  5.  Skip and select set up a workflow by yourself.
  6. An editor will appear. Here you must write the configuration file. The language used for configuration is YAML.
  7. Paste and change this configuration where it is highlighted:
    name: Docker Image CI
    
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    
    jobs:
    
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v2
        - name: docker login
          env:
            DOCKER_USER: ${{secrets.DOCKER_USER}}
            DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
          run: |
            docker login -u $DOCKER_USER -p $DOCKER_PASSWORD 
        - name: Set up Docker Buildx
          uses: docker/setup-buildx-action@v1
        - name: Build and push
          uses: docker/build-push-action@v2
          with:
              context: .
              file: ./nameoftheproject/Dockerfile
              push: true
              tags: ${{ secrets.DOCKER_USER}}/nameoftheimage:latest
  8. Commit the file. Now the action will start. Go to the actions tab again and check the status of your workflow.

The configuration file specifies that the action should run on push on the main branch. First, it logins on the Docker Hub using your secrets, then builds the image and pushes it. So, whenever you make a change on the main branch, the GitHub action will run.

Leave a Comment