Problem in Github Action and building latest changes pushed

Hi Everyone,

I’m running into an issue where GitHub action/runner (or docker/git) is not using my latest pushed changes or it just reads and builds the Master branch rather than the modified branch. For example, I added an extra print out line but when I execute docker container logs I don’t see the new print out.

  1. List item

I tried different combinations and so many changes across all files and still didn’t get to work. So, I really would love if someone can point me out to the right direction or the solution.

Setup
The flow is: GitHub Action -> Docker-Compose -> {project}/Dockerfile.
Hosting OS: Linux CentOS
Programming language (if it helps): .Net Core 3.1/C#
Git version: 2.27.0

My GitHub Action/workflow contents

name: Docker Image CI

on:
  push:
    branches: [ develop ]

jobs:
  build:
    runs-on: self-hosted
    steps:
    - uses: actions/checkout@v2
      with:
        ref: develop
        token: ${{ github.token }}

    - name: Stop & Remove Docker Compose Containers
      run: docker-compose down
    - name: Run Docker Compose
      run: docker-compose up -d

My Docker-compose contents:

version: '3.8'

services:    
 
    cashier:
        build: 
            context: .
            dockerfile: Cashier/Dockerfile
        container_name: cashier
        ports: 
            - "10000:80"
        networks: 
            - salad-bridge
        restart: always
 
networks: 
    salad-bridge:
        external: true

My Dockerfile contents (Generated by Visual Studio 2019)

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Cashier/Cashier.csproj", "Cashier/"]
COPY ["Infrastructure/Infrastructure.csproj", "Infrastructure/"]
RUN dotnet restore "Cashier/Cashier.csproj"
COPY . .
WORKDIR "/src/Cashier"
RUN dotnet build "Cashier.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Cashier.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Cashier.dll"]

If any extra details needed please let me know,

Thanks

@aalwaqfi,

When you push a new commit to a branch in your repository, if you want this new pushed commit can trigger a workflow to build, test or release the latest source code, you need to do the things below:

  • Make sure the workflow file is existing on the branch which you push the new commit to (develop branch in your case).

  • In the workflow file, when you use the checkout action, by default the action will fetch only a single commit for the event on the develop branch.
    For example, you push a new commit “commit1” to the develop branch, and this new commit trigger the workflow on the develop branch. If you set the checkout step like as below:

- name: Checkout code
  uses: actions/checkout@v2

the checkout action will only fetch “commit1” from the develop branch by default.
So, if you do not need to checkout other commits from other branches, tags, SHA or repositories, you can keep all the inputs as their default values (just like above example).

it just reads and builds the Master branch rather than the modified branch.

If the problem still exists, please make sure you have enabled debug logging in your repository, and share the repository with us, so that we can check more detailed logs of the workflow runs to analyze the root cause.

@brightran,

Thank you for the help.

Nothing worked as I’m the only developer that is working on the project (a PoC) and tried to have another runner on my local PC and same issue.

I created a mini repo they reproduce my problem.

Repo Url: https://github.com/alwaqfi/CashierRepo

I tried 2 runes on the repo

(Private Repo)
//await Console.Out.WriteLineAsync($"Start Time {DateTime.Now.ToShortDateString()}");
await Console.Out.WriteLineAsync($"Start Time {DateTime.Now.ToLongDateString()}");
await Console.Out.WriteLineAsync($"Start Time {DateTime.Now.ToLongTimeString()}");
await Console.Out.WriteLineAsync($"New Order Number Request {DateTime.Now.Ticks}");

"--> docker container logs cashier

Start Time 07/15/2020
Start Time Wednesday, 15 July 2020
Start Time 14:28:51
New Order Number Request 637304201311515733
(Public Repo)
//await Console.Out.WriteLineAsync($"Start Time {DateTime.Now.ToShortDateString()}");
await Console.Out.WriteLineAsync($"Start Time {DateTime.Now.ToLongDateString()}");
await Console.Out.WriteLineAsync($"Start Time {DateTime.Now.ToLongTimeString()}");
//await Console.Out.WriteLineAsync($"New Order Number Request {DateTime.Now.Ticks}");

"--> docker container logs cashier"

Start Time 07/15/2020
Start Time Wednesday, 15 July 2020
Start Time 14:32:26
New Order Number Request 637304203460523966

Thanks

@aalwaqfi,

From the repository you shared, I do not see the error you reported.
I can see the checkout action fetched the right commit from the develop branch, not from the master branch. Everything looks like normal.

If I have missed something, please feel free to tell me.

@brightran

Thank you,

The issue is when the action is executed and containers are created but what is in the containers are not my commit contents.

The outputs in my last reply were after the GitHub action completely successfully and containers were created and started. The CI/CD (docker-*) logic seems good, the GitHub action logs seems fine, but what is deployed is not my last commit that the action executed when I pushed it.

I thought something is happening on our side like caching issue. So before I reply, I cloned the repo on my personal machine then executed the action and was able to reproduce the same issue.

Any idea how to proceed?

Note: I’m new to GitHub actions and Docker in general, so the content of the files looks fine to me.

Thanks

@aalwaqfi,

I thought something is happening on our side like caching issue.

Yes, the problem should be caused by the Docker Cache.

In the same host machine, by default Docker will cache the results of the first build of a Dockerfile, allowing subsequent builds to be super fast.
However, the cache is used pretty aggressively and may cause issues when you want the updated output of a RUN or ENTRYPOINT instruction to make it into the new container. As it stands, unless the Dockerfile itself changes (especially the RUN or ENTRYPOINT instruction changes, and thus invalidates Docker’s on-host cache), Docker will reuse the previous results from cache. This is clearly disadvantageous when the RUN command is a source code checkout, for example a git clone that is RUN as the initial step of a project’s build.

To avoid the issues caused by the Docker Cache, you can try the following options of the ‘docker-compose up’ command (more details, see here):

Option Description
–force-recreate Recreate containers even if their configuration and image haven’t changed.
–build Build images before starting containers.
1 Like

@brightran

Thank you so much,

The –force-recreate didn’t work. Because I already executing docker-compose down then up, so this should recreate the containers.

The issue seems is in the cached images. So by Adding a new command to my github action solved this issue. So, my Github action looks like this now:

    - name: Stop & Remove Docker Compose Containers
      run: docker-compose down
    - name: Run Docker Compose
      run: docker-compose up --build -d

Thanks again,