Reputation: 5082
I have a Github Action which consist to tag, build and deploy a docker image.
When there is a pull request I just do the build job with this file: build.yml
# This is a basic workflow to help you get started with Actions
name: Build
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
pull_request:
branches: [ master ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
docker:
# The type of runner that the job will run on
runs-on: ubuntu-latest
env:
DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: docker build
run: | # Tag the image with the commit hash
docker build -t $DOCKERHUB_REPOSITORY .
docker tag $DOCKERHUB_REPOSITORY:latest $DOCKERHUB_REPOSITORY:$(git log -1 --pretty=%h)
And for the deployment I have to build and deploy with this file: deploy.yml
# This is a basic workflow to help you get started with Actions
name: Deploy
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
docker:
# The type of runner that the job will run on
runs-on: ubuntu-latest
env:
DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: docker login
env:
DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
DOCKERHUB_ACCESS_TOKEN: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
run: |
docker login -u $DOCKERHUB_USER -p $DOCKERHUB_ACCESS_TOKEN
- name: docker build
run: | # Tag the image with the commit hash
docker build -t $DOCKERHUB_REPOSITORY .
docker tag $DOCKERHUB_REPOSITORY:latest $DOCKERHUB_REPOSITORY:$(git log -1 --pretty=%h)
- name: docker push
run: |
docker push $DOCKERHUB_REPOSITORY
For me there is a repetition with the build section but I did not found how can I use a dependency in jobs on different files. It is not working.
How can I tell github action that the deploy section depends on the build ?, with 2 different files.
Upvotes: 2
Views: 1838
Reputation: 1885
You can merge them into one CI/CD pipeline. This should be triggered by both push
es and pull_request
s in master. This has several advantages;
if: ${{ success() }}
!docker build
is only defined once.push
or pull_request
by using conditions:if: ${{ github.event_name == 'push' }} // OR
if: ${{ github.event_name == 'pull_request' }}
Upvotes: 1