Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

Gitlab CI upload only when merging not on merge request

I have a working pipeline but I noticed one thing that might be a bad choice of mine and that is the uploading part. When someone creates a merge request, my script will build the project, execute the tests and then upload everything to the server. I only want this last step (upload to server) to be done when the merge request is "accepted" so to speak. It is a bit strange to upload everything to the server while the merge request has not been accepted yet.

Can anyone tell me how I can do this?

My code below:

stages:
    - build
    - test
    - deploy

build project:
    stage: build
    image: node:11
    only:
        - merge_requests
    script:
        - npm install --progress=false
        - npm run build
    artifacts:
        paths:
            - ./dist

test build:
    stage: test
    image: alpine
    only:
        - merge_requests
    script:
        - cd dist
        - grep -q "<title>MySite</title>" index.html

deploy master:
    stage: deploy
    only:
        - merge_requests
    before_script:
        - 'which ssh-agent || ( apt-get install -qq openssh-client )'
        - eval $(ssh-agent -s)
        - ssh-add <(echo "$SSH_PRIVATE_KEY")
        - mkdir -p ~/.ssh
        - '[[ -f /.dockerenv ]] && echo -e "Host *
StrictHostKeyChecking no

" > ~/.ssh/config'

    script:
        - apt-get update -qq && apt-get install -y -qq lftp
        - lftp -c "set sftp:auto-confirm yes;set ssl:verify-certificate no;  open -u $SFTP_USER,$SFTP_PASSWORD sftp://$SFTP_HOST; mirror --reverse --verbose --delete --exclude api/ dist/ /var/www/mysite.com; bye";

so basically what I want is that the "build stage" only gets executed when the merge request has been accepted, because when it is accepted and we, ourselfs, click on the "merge" button or used the "merge automatically" button, then it should be uploaded.

Hope someone can help me out.

question from:https://stackoverflow.com/questions/65645662/gitlab-ci-upload-only-when-merging-not-on-merge-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Each of your stages is configured to run only on merge_request, which occurs "when a merge request is created or updated". See here for a list of all options and examples.

The following will only run your deploy stage when the target branch is master, which is the case when you press the merge button. Keep in mind that this will run the entire pipeline, including build and test, on merge to master. That's a good thing.

deploy master:
    stage: deploy
    only:
        refs:
          - master
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...