radicaled
radicaled

Reputation: 2669

Github actions only on first push

I'm working on a github action that creates an app on ArgoCD. The problem is that I want to execute it only once, the first time that it gets push with the k8s yamls.
Is there any way to restrict the github action to the first push on the repo?
I have been looking to the github triggers, but I was not able to find any relation.

This is a sample of the action:

on: push
name: deploy-argo-app
jobs:
  deploy-argo-app:
    name: Deploy new app on ArgoCD
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Install Argo Cli
      run: |
          VERSION=$(curl --silent "https://api.github.com/repos/argoproj/argo-cd/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
          sudo curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/download/$VERSION/argocd-linux-amd64
          sudo chmod +x /usr/local/bin/argocd
    - name: Create app
      run: | 
          argocd app create guestbook --repo https://github.com/argoproj/argocd-example-apps.git --path guestbook --dest-server https://kubernetes.default.svc --dest-namespace default

Upvotes: 1

Views: 1704

Answers (2)

McKynlee Westman
McKynlee Westman

Reputation: 91

I have been looking for something similar, but I needed to know the first push on any given branch.

First, you can see everything available to you on your Github context by running this worfklow:

      - name: Dump GitHub context
        id: github_context_step
        run: echo '${{ toJSON(github) }}'

I was able to see that on the first push to any branch the

github.event.before = 0000000000000000000000000000000000000000

and

github.event.created = true

whereas for any subsequent push to that same branch the github.event.before will give a numerical reference to the previous commit, and the github.event.created will be false.

Slightly related, but maybe still useful for people who find themselves on the same hunt I was on!

Upvotes: 5

radicaled
radicaled

Reputation: 2669

I found kind of a workaround.

on: push
name: deploy
jobs:
  deploy:
    if: github.run_number == 1

Basically "github.run_number" gives you the push number. It will work only on the first push and then it will be ignore.

Upvotes: 3

Related Questions